mysql 主从同步-读写分离

Posted 北极菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 主从同步-读写分离相关的知识,希望对你有一定的参考价值。

一、       实验环境

Master                   centos 7.3              192.168.138.13

Slave                     centos 7.3               192.168.138.14

二、在master操作

  1. 安装并配置文件

[[email protected] ~]# yum install mariadb-server -y

[[email protected] ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=1                  //必须唯一

log-bin=mysql-bin          //开启binlog日志

character-set-server=utf8         //设置字符集

        2.启动mysql

[[email protected] ~]# systemctl start mariadb

        3. 授权root用户

MariaDB [(none)]> grant all on *.* to [email protected]‘localhost‘ identified by ‘123456‘;

MariaDB [(none)]> grant all on *.* to [email protected]‘%‘ identified by ‘123456‘;

         4. 刷新权限表

MariaDB [(none)]> flush privileges

5.查看master状态

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |    521 |                   |                  |

+------------------+----------+--------------+------------------+

           

注意:如果主库中已存在数据,则需要备份拷到从库保持数据一致性

1.锁定数据表,避免在备份过程中,表被更新

mysql>LOCK TABLES tbl_name READ;

为表增加一个写锁定:

mysql>LOCK TABLES tbl_name WRITE;

解锁 : mysql>UNLOCK TABLES;

2.备份数据

方法一

Mysqldump  -uroot  -p –B db1 –T tb1  | gzip  > /mysqlbackup/tb1.sql.gz

方法二

直接备份datadir=/var/lib/mysql

                                                                                        

三、       在slave上操作

  1. 安装并配置/etc/my,cnf

[[email protected] ~]# yum install mariadb-server -y

[[email protected] ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=1                  //必须唯一

character-set-server=utf8         //设置字符集

        2. 启动mysql

[[email protected] ~]# systemctl start mariadb

3. 配置同步参数

MariaDB [(none)]> change master to

 master_host=‘192.168.138.13‘,

master_user=‘root‘,

master_password=‘123456‘,

master_port=3306,

master_log_file=‘mysql-bin.000003‘,

master_log_pos=521;

4. 启动主从同步进程

          MariaDB [(none)]> start slave;

        5. 检查状态

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.138.13

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 521

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

看到两个yes说明配置成功

四、 测试

         1.在slave上查看

        MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

2. 在master创建数据库

MariaDB [(none)]> create database testdb1;

Query OK, 1 row affected (0.00 sec)

3.  在slave查看

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| testdb1            |

+--------------------+

4 rows in set (0.00 sec)

以上是关于mysql 主从同步-读写分离的主要内容,如果未能解决你的问题,请参考以下文章

mysql数据库读写分离,主从同步实现方法

MySQL主从同步+读写分离

mysql1.6(主从同步,数据读写分离,mysql优化)

Mysql高级面试题-读写分离主从复制主从同步延时处理

MySQL主从复制与读写分离的理论+实操(配有详细图释)

MySQL的读写分离与主从同步数据一致性