mysql实现主从复制

众所周知,MySQL是当今应用最广泛的的关系型数据库之一。其体积小、速度快、开源免费等特点深受互联网公司及广大开发者的喜爱。高可用架构对于互联网服务基本是标配,mysql主从复制便是一种基本高可用架构,一个master负责来自客户端写操作,多个slave应对客户端读压力。

一、原理简介

1、过程简述

mysql主从复制是基于其二进制文件(binary log file),在其中一台节点启用二进制日志,作为master,它的数据库中所有操作都会以“事件”的方式记录在二进制日志(binlog)中,master也会开启一个dump线程,负责响应slave的I/O线程请求。其它节点作为slave通过一个I/O线程与master保持通信,并监控master的binlog变化,一旦发现master的binlog发生变化,则会把binlog复制到自己的日志(relay-log)中,然后slave会开启一个SQL线程读取reply-log中事件,写入自己的数据库中。以此实现从数据库和主数据库的一致性,自然也就实现了主从复制。

2、一次复制过程

mysql实现主从复制_第1张图片

 

1、首次,在master创建账户、密码,用于slave连接

2、 在slave手动执行change master to 语句连接master,并指定user、password、port、ip等参数、启动start slave

3、这样slave的I/O线程和master的dump线程建立连接,slave的IO线程向master发起binlog的请求

4、master的dump线程根据slave I/O线程的请求,将本地binlog以events的方式发给slave I/O线程

5、slave的I/O线程接收binlog evnets,并存放到本地relay-log中,读取位置等信息,同时会记录到master.info中。

6、slave的SQL线执行relay-log进行数据更新到库,同时把读取位置等信息记录到relay-log.info

3、优缺点 

优点: 成本低,布署快速、简单方便;读写分离,还能通过增加从库来减少读库压力
缺点:主库单点故障;数据一致性问题(同步延迟造成),延迟问题在mysql5.7得到解决

二、分布一览

ip 主机 master slave
192.168.1.76 node06 1(write)  
192.168.1.77 node07   1(read)
192.168.1.78 node08   1(read)

三、mysql主从配置

1、mysql安装

mysql安装自行查找或参考之前文章https://blog.csdn.net/lzxlfly/article/details/81990234

2、配置master-slave

(1)master配置

编辑vim /etc/my.cnf 加入log_bin=mysqllog.bin启用二进制log记录,及server-id

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
log_bin=mysqllog.bin
server-id=01 #唯一,不与其它重复
expire_logs_days=15 #过期时间,默认0不自动删除,15天后自动删除

 (2)slave配置

编辑vim /etc/my.cnf ,slave只需配置server-id即可

server-id=02 #唯一,不与其它重复

read_only = 1 #1只读,0读写,可不配置,在客户端控制读写地址

(3)配置完成记得启动每个节点mysql服务 

3、master与slave建立连接

master与slave建立连接有两种方法,一种是执行sql语句连接,另一种是mysql-utilities来实现

3.1  执行sql(方法一)

1、在master上创建repl用户,用于授权slave同步数据

mysql> CREATE USER 'repl'@'192.168.1.%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%';
#或者创建、授权合并执行
mysql> grant replication slave  on *.* to repl@'192.168.1.%' identified by '123456';

mysql> flush privileges;   #刷新权限

2、查看masterf的Flie和Position

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysqllog.000001 |     3058 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3、连接master,启动slave

#每个slave都执行
mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.1.76',
    ->   MASTER_USER='repl',
    ->   MASTER_PASSWORD='123456',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysqllog.000001',
    ->   MASTER_LOG_POS=3058,
    ->   MASTER_CONNECT_RETRY=60;

  启动从库复制

mysql> start  slave;#每个slave都执行

4、检查slave同步状态

mysql>  show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.76
                  Master_User: rpel
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysqllog.000001
          Read_Master_Log_Pos: 3058
               Relay_Log_File: node07-relay-bin.000002
                Relay_Log_Pos: 2681
        Relay_Master_Log_File: mysqllog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 3058
              Relay_Log_Space: 2890
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 6
                  Master_UUID: 9763e2c6-f57e-11e8-af9c-000c2954ac12
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

 

3.2  mysql-utilities(方法二)

1、下载相关工具

 mysql主从复制需要使用mysql-utilities做主从复制,同时依赖mysql-connector-python-驱动连接

去https://downloads.mysql.com/archives/utilities/  下载:mysql-utilities-1.6.5-1.el6.noarch

去https://dev.mysql.com/downloads/connector/python/ 下载:mysql-connector-python-2.0.5-1.el6.noarch

mysql实现主从复制_第2张图片
 

mysql实现主从复制_第3张图片

 安装下载的mysql-connector-python-2.0.5-1.el6.noarch、mysql-utilities-1.6.5-1.el6.noarch 

rpm -ivh mysql-connector-python-2.0.5-1.el6.noarch.rpm
rpm -ivh mysql-utilities-1.6.5-1.el6.noarch.rpm

2、master与slaves连接

运用mysql-utilities的mysqlreplicate命令,在master上执行连接命令,将maste与slaves节点建立连接,

--rpl-user=rpel:"123456"表示在master创建用户repl,密码123456,允许从slaves节点登录master

master-node06与slave-node07建立连接

[root@node06 mysql] mysqlreplicate --master=root:"123456"@192.168.1.76:3306 --slave=root:"123456"@192.168.1.77:3306 --rpl-user=rpel:"123456"
WARNING: Using a password on the command line interface can be insecure.
# master on 192.168.1.76: ... connected.
# slave on 192.168.1.77: ... connected.
# Checking for binary logging on master...
# Setting up replication...
# ...done.

master-node06与slave-node08建立连接

[root@node06 mysql] mysqlreplicate --master=root:"123456"@192.168.1.76:3306 --slave=root:"123456"@192.168.1.78:3306 --rpl-user=rpel:"123456"
WARNING: Using a password on the command line interface can be insecure.
# master on 192.168.1.76: ... connected.
# slave on 192.168.1.78: ... connected.
# Checking for binary logging on master...
# Setting up replication...
# ...done.

用mysqlrplcheck 命令检查连接状态(可省略检查)   尾部-s表示同时显示对应slave的状态

[root@node06 ~] mysqlrplcheck --master=root:[email protected]:3306 --slave=root:[email protected]:3306 -s
WARNING: Using a password on the command line interface can be insecure.
# master on 192.168.1.76: ... connected.
# slave on 192.168.1.78: ... connected.
Test Description                                                     Status
---------------------------------------------------------------------------
Checking for binary logging on master                                [pass]
Are there binlog exceptions?                                         [pass]
Replication user exists?                                             [pass]
Checking server_id values                                            [pass]
Checking server_uuid values                                          [pass]
Is slave connected to master?                                        [pass]
Check master information file                                        [WARN]

Cannot read master information file from a remote machine.

Checking InnoDB compatibility                                        [pass]
Checking storage engines compatibility                               [pass]
Checking lower_case_table_names settings                             [pass]
Checking slave delay (seconds behind master)                         [pass]

#
# Slave status: 
#
                Slave_IO_State : Waiting for master to send event
                   Master_Host : 192.168.1.76
                   Master_User : rpel
                   Master_Port : 3306
                 Connect_Retry : 60
               Master_Log_File : mysqllog.000001
           Read_Master_Log_Pos : 1554
                Relay_Log_File : nginx02-relay-bin.000002
                 Relay_Log_Pos : 635
         Relay_Master_Log_File : mysqllog.000001
              Slave_IO_Running : Yes
             Slave_SQL_Running : Yes
               Replicate_Do_DB : 
           Replicate_Ignore_DB : 
            Replicate_Do_Table : 
        Replicate_Ignore_Table : 
       Replicate_Wild_Do_Table : 
   Replicate_Wild_Ignore_Table : 
                    Last_Errno : 0
                    Last_Error : 
                  Skip_Counter : 0
           Exec_Master_Log_Pos : 1554
               Relay_Log_Space : 844
               Until_Condition : None
                Until_Log_File : 
                 Until_Log_Pos : 0
            Master_SSL_Allowed : No
            Master_SSL_CA_File : 
            Master_SSL_CA_Path : 
               Master_SSL_Cert : 
             Master_SSL_Cipher : 
                Master_SSL_Key : 
         Seconds_Behind_Master : 0
 Master_SSL_Verify_Server_Cert : No
                 Last_IO_Errno : 0
                 Last_IO_Error : 
                Last_SQL_Errno : 0
                Last_SQL_Error : 
   Replicate_Ignore_Server_Ids : 
              Master_Server_Id : 6
                   Master_UUID : 9763e2c6-f57e-11e8-af9c-000c2954ac12
              Master_Info_File : /var/lib/mysql/master.info
                     SQL_Delay : 0
           SQL_Remaining_Delay : None
       Slave_SQL_Running_State : Slave has read all relay log; waiting for more updates
            Master_Retry_Count : 86400
                   Master_Bind : 
       Last_IO_Error_Timestamp : 
      Last_SQL_Error_Timestamp : 
                Master_SSL_Crl : 
            Master_SSL_Crlpath : 
            Retrieved_Gtid_Set : 
             Executed_Gtid_Set : 
                 Auto_Position : 0
          Replicate_Rewrite_DB : 
                  Channel_Name : 
            Master_TLS_Version : 
# ...done.

 

四、测试主从复制

利用插件连接node06、node07、node08,在master-node06建立tes库,然后刷新下,可以看到slave节点上也创建了对应的test库,master复制slave生效了。

mysql实现主从复制_第4张图片

在master的test库建立user表,之后刷新下两个slave,看到也出现了user表

mysql实现主从复制_第5张图片

接下来在master写入一条记录并保存

mysql实现主从复制_第6张图片

查看slave-node07的user表,看到也有数据了

mysql实现主从复制_第7张图片

查看slave-node08的user表,亦是如此

mysql实现主从复制_第8张图片

 这样从master到slaves的复制就生效了

参考:https://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html

参考:https://dev.mysql.com/doc/refman/5.7/en/change-master-to.html

参考:https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html

参考:https://www.cnblogs.com/clsn/p/8150036.htm

 

你可能感兴趣的:(mysql主从同步,MySQL,数据库)