【MySQL】MySQL半同步复制

默认情况下,MySQL的复制是异步的,主库执行完Commit提交操作后,在主库写入BInlog后即可成功返回给客户端,无需等待BInlog传给从库。当在主库上写入一个事务并提交成功,而从库尚未得到主库推送的Binlog时,主库宕机了,从而从库可能损失该事务,造成主从库的不一致。

为了解决这个问题,MySQL引入了半同步复制。半同步复制保证了主库上的每一个Binlog都能可靠的复制到从库上,主库在每次事务提交时,并不及时反馈给前端用户,而是等待其中一个从库也接收到Binlog并成功写入中继日志后,主库才返回Commit操作成功给客户端。此时,至少有两份日志记录,一份在主库的Binlog上,另一份在至少一个从库的中继日志上,从而保证了数据的完整性。
半同步复制通过使用插件来实现,插件安装成功后,通过使用与其相关的系统变量来控制它。使用半同步复制,需要满足下面的条件:
  • MySQL 5.5或以上版本;
  • MySQL服务器支持动态加载插件的能力,可通过have_dynamic_loading来判断;
  • 复制必须已经工作;
  • 不能配置多个复制通道,半同步复制只与默认的复制通道兼容;
配置半同步复制,使用下面的命令即可:Install plugin,set global,stop slave 和start slave,下面具体演示:
本篇基于前篇:MySQL搭建主从复制环境

1、软件环境
mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.21-log |
+------------+
1 row in set (0.00 sec)
2、判断MySQL服务器是否支持动态加载插件
mysql> select @@have_dynamic_loading;
+------------------------+
| @@have_dynamic_loading |
+------------------------+
| YES                    |
+------------------------+
1 row in set (0.00 sec)
3、安装主库和从库的半同步插件
主库插件:/usr/local/mysql/lib/plugin/semisync_master.so
从库插件:/usr/local/mysql/lib/plugin/semisync_slave.so
主库上安装插件semisync_master.so:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.16 sec)

mysql> show variables like '%rpl_semi_sync%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | OFF        |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)
mysql> select *from mysql.plugin;
+----------------------+--------------------+
| name                 | dl                 |
+----------------------+--------------------+
| rpl_semi_sync_master | semisync_master.so |
+----------------------+--------------------+
1 row in set (0.00 sec)
从库上安装插件semisync_slave.so:
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.11 sec)

mysql> show variables like '%rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | OFF   |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.03 sec)
mysql> select *from mysql.plugin;
+---------------------+-------------------+
| name                | dl                |
+---------------------+-------------------+
| rpl_semi_sync_slave | semisync_slave.so |
+---------------------+-------------------+
1 row in set (0.00 sec)
4、分别在主从库打开半同步,默认是不打开的
主库配置:
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)

mysql> set global rpl_semi_sync_master_timeout=50000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%semi_sync%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | ON         |
| rpl_semi_sync_master_timeout              | 50000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)
从库配置:
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%semi_sync%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)
注意:由于之前是异步复制,故需要重启下从库的I/O线程,如果是全新配置半同步则不需要:
mysql> stop slave io_thread;start slave io_thread;
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)
插入一条记录,查看主库的状态:
mysql> insert into t_mm (name) values('Test Semi');
Query OK, 1 row affected (0.03 sec)

mysql> select *from t_mm;
+----+-----------+---------------------+
| id | name      | cdate               |
+----+-----------+---------------------+
|  2 | China     | 2018-02-28 17:31:57 |
|  4 | Japon     | 2018-02-28 17:31:57 |
|  6 | USA       | 2018-02-28 17:31:57 |
|  7 | Alen      | 2018-02-28 17:32:07 |
|  9 | Jack      | 2018-02-28 17:32:07 |
| 11 | Summer    | 2018-02-28 17:32:07 |
| 12 | UK        | 2018-02-28 17:32:42 |
| 14 | CN        | 2018-03-01 08:54:24 |
| 15 | Lilei     | 2018-03-01 08:55:07 |
| 17 | Hanmeimei | 2018-03-01 09:11:55 |
| 19 | Test Semi | 2018-03-01 16:28:26 |
+----+-----------+---------------------+
11 rows in set (0.00 sec)

mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 1     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 877   |
| Rpl_semi_sync_master_tx_wait_time          | 877   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
状态值分析:
Rpl_semi_sync_master_status:值为ON,表示半同步目前是打开状态;
Rpl_semi_sync_master_yes_tx:值为1,表示主库当前是有一个事务是通过半同步复制到从库;
Rpl_semi_sync_master_no_tx:值为0,表示当前没有不是半同步模式下从库及时响应的。
5、模拟网络故障,半同步转为异步复制模式
1)半同步复制在主库等待50秒超时
mysql> show variables like '%semi_sync%timeout%';
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| rpl_semi_sync_master_timeout | 50000 |
+------------------------------+-------+
1 row in set (0.01 sec)
2)模拟从库故障
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
3)主库执行一个事务并提交,主库的提交操作会阻塞50秒
mysql> insert into t_mm (name) values('Block');
新开一个窗口检查当前主库线程,发现提交操作在等待从库半同步操作相应:
mysql> show processlist\G;
*************************** 1. row ***************************
     Id: 19
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 18
  State: Waiting for semi-sync ACK from slave
   Info: insert into t_mm (name) values('Block')
*************************** 2. row ***************************
     Id: 22
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: starting
   Info: show processlist
2 rows in set (0.00 sec)

ERROR: 
No query specified
4)主库上等待50秒超时
mysql> insert into t_mm (name) values('Block');
Query OK, 1 row affected (50.09 sec)
检查半同步状态值:
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 1     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 877   |
| Rpl_semi_sync_master_tx_wait_time          | 877   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
状态值分析:
Rpl_semi_sync_master_status :值为OFF,表示半同步关闭,目前复制模式是异步复制;
Rpl_semi_sync_master_yes_tx :值为1,表示刚才的事务不是通过半同步完成的,不累加;
Rpl_semi_sync_master_no_tx :值为1 ,表示半同步模式下,从库没有及时响应的事务增加1个。
6、模拟异步复制自动切换为半同步模式
1)从库正常
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
2)检查从库状态,则数据同步到从库
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.24.33.186
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000013
          Read_Master_Log_Pos: 2892
               Relay_Log_File: strong-relay-bin.000141
                Relay_Log_Pos: 739
        Relay_Master_Log_File: mysql-bin.000013
             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: 2892
              Relay_Log_Space: 1536
              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: 2a3e3fd9-0587-11e8-bdb8-0800272325a8
             Master_Info_File: /usr/local/mysql-5.7.21-el7-x86_64/data/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: 2a3e3fd9-0587-11e8-bdb8-0800272325a8:19-36
            Executed_Gtid_Set: 2a3e3fd9-0587-11e8-bdb8-0800272325a8:1-36,
fb42b433-06fc-11e8-b91a-0800275b8da0:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified
3)检查主库半同步复制状态,表示自动切换为半同步模式
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 2     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 877   |
| Rpl_semi_sync_master_tx_wait_time          | 877   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.01 sec)
4)在主库测试当前模式是半同步模式
mysql> insert into t_mm (name) values('Semi Mode');
Query OK, 1 row affected (0.03 sec)
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 3     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 1314  |
| Rpl_semi_sync_master_tx_wait_time          | 2628  |
| Rpl_semi_sync_master_tx_waits              | 2     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 2     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
提交一个事务后,Rpl_semi_sync_master_yes_tx 由1变为2,表示刚才的操作是通过半同步模式完成的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值