MySQL复制原理

Posted ChavinKing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL复制原理相关的知识,希望对你有一定的参考价值。

mysql从3.23开始提供复制功能,复制指将主库的ddl和dml操作通过binlog文件传送到从库上执行,从而保持主库和从库数据同步。mysql支持一台主库同时向多台从库复制,从库同时也可以作为其他从库的主库,从而实现级联复制功能。mysql复制功能相当于oracle数据库的逻辑dg功能。

mysql复制原理大致如下:

1)mysql主库事务提交时会把数据变更作为event记录在binlog文件中,mysql主库的sync_binlog参数控制binlog日志刷新到磁盘。

2)主库推送binlog中的event到从库的中继日志relay log中,之后从库根据中继日志重做数据变更操作,通过逻辑复制来达到与主库同步的目的。

mysql通过3个线程完成复制功能:其中binlog dump线程跑在主库上,io线程和sql线程跑在从库上。当从库启动复制(start slave)时,首先创建io线程连接主库,主库随后创建binlog dump线程读取event发送给io线程,io线程获取到event后更新到从库中继日志relay log中,之后从库sql线程根据relay log内容在从库从做操作。从而完成mysql复制功能。mysql复制结构如图:

wpsBD89.tmp

主库通过show processlist命令可以看到binlog dump进程,如下:

mysql> show processlist \\G;

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

     Id: 125

   User: root

   Host: localhost

     db: test

Command: Query

   Time: 0

  State: init

   Info: show processlist

*************************** 2. row ***************************

     Id: 127

   User: rep1

   Host: 192.168.80.136:44889

     db: NULL

Command: Binlog Dump

   Time: 16991

  State: Master has sent all binlog to slave; waiting for binlog to be updated

   Info: NULL

2 rows in set (0.00 sec)

ERROR:

No query specified

mysql>

从库通过show processlist命令可以看到io线程和sql线程,如下:

mysql> show processlist \\G;

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

     Id: 4

   User: root

   Host: localhost

     db: test

Command: Query

   Time: 0

  State: init

   Info: show processlist

*************************** 2. row ***************************

     Id: 7

   User: system user

   Host:

     db: NULL

Command: Connect

   Time: 17079

  State: Waiting for master to send event

   Info: NULL

*************************** 3. row ***************************

     Id: 8

   User: system user

   Host:

     db: NULL

Command: Connect

   Time: 17107

  State: Slave has read all relay log; waiting for the slave I/O thread to update it

   Info: NULL

3 rows in set (0.00 sec)

ERROR:

No query specified

mysql复制涉及到两种文件:binlog和relay log文件。其中根据binlog的不同设置,mysql复制分为3中模式。

复制的3中常见架构:一主多从、级联复制、双主架构

1)mysql一主多从架构:解决主库读请求压力解决方案。

wpsBD8A.tmp

2)mysql级联架构:解决一主多从架构中主库io和网络压力,缺点时额外增加了主库到从库的应用延迟,其中master2可以设置为blackhold(黑洞)模式来缓解复制延迟。

wpsBD8B.tmp

3)双主复制/dual master架构:

wpsBD8C.tmp

4)双主级联复制架构:

wpsBD8D.tmp

通过show master status查看主库状态:

mysql> show master status \\G;

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

             File: dbking-bin.000004

         Position: 1729

     Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

ERROR:

No query specified

通过show slave status查看从库状态:

mysql> show slave status \\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.80.133

                  Master_User: rep1

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: dbking-bin.000004

          Read_Master_Log_Pos: 1729

               Relay_Log_File: chavinking-relay-bin.000010

                Relay_Log_Pos: 714

        Relay_Master_Log_File: dbking-bin.000004

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: 1729

              Relay_Log_Space: 1056

              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: 1

                  Master_UUID: 9b92b2a8-b7e0-11e6-81e4-000c29fa5a95

             Master_Info_File: /usr/local/software/mysql-5.6.24-linux-glibc2.5-x86_64/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           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

1 row in set (0.00 sec)

以上是关于MySQL复制原理的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制+读写分离原理及配置实例

深度探索MySQL主从复制原理

MySQL主从复制-GTID原理

mysql主从复制原理介绍

[MySQL] 主从复制原理

MySQL深入浅出主从复制数据同步原理