mysql5.7 开启增强半同步复制

前提是主从异步复制环境要提前搭建好,然后再开启mysql增强半同步

环境:mysql5.7.26 主从异步复制早已部署好。

1.加载plugin插件

建议master和slave上全部执行(考虑到MHA的主从自动切换的环境)

在主库安装semisync_master.so和semisync_slave.so插件:

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

linux服务器上的master库执行:

root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (4.52 sec)
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.07 sec)

linux服务器上的slave库执行:

root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
ERROR 1125 (HY000): Function 'rpl_semi_sync_master' already exists
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
ERROR 1125 (HY000): Function 'rpl_semi_sync_slave' already exists
root@localhost [(none)]>

提示:slave上增强半同步的插件已经存在,所以报错。
原因:master上安装增强半同步的插件后自动同步到了slave库上

2.slave和master开启增强半同步参数

生产环境上建议先在slave库上的开启 增强半同步参数

set global rpl_semi_sync_slave_enabled =1;
stop  slave io_thread;start  slave io_thread;

此刻观察master的错误日志:提示开启了Semi-sync replication 复制

2019-06-16T20:31:28.923731+08:00 718 [Note] While initializing dump thread for slave with UUID <7659cf56-8e81-11e9-bcbd-842b2b5999d9>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(717).
2019-06-16T20:31:28.923838+08:00 717 [Note] Stop asynchronous binlog_dump to slave (server_id: 113306)
2019-06-16T20:31:28.923951+08:00 718 [Note] Start binlog_dump to master_thread_id(718) slave_server(113306), pos(, 4)
2019-06-16T20:31:28.923984+08:00 718 [Note] Start semi-sync binlog_dump to slave (server_id: 113306), pos(, 4)
2019-06-16T20:31:29.189007+08:00 718 [Note] Semi-sync replication switched ON at (mysql-bin.000005, 852045921)

然后master上再开启增强半同步参数


set global rpl_semi_sync_master_enabled =1
set global rpl_semi_sync_master_timeout        =1000

3.MySQL官网配置方法

官方要求master和slave的/etc/my.cnf要开启下面的参数:

rpl_semi_sync_master_enabled        =1                             #    0
rpl_semi_sync_slave_enabled         =1                             #    0
rpl_semi_sync_master_timeout        =1000                          #    1000(1 second) 同步复制中由于网络原因导致复制时间超过1s后,增强半同步复制就变成了异步复制了
rpl_semi_sync_master_wait_for_slave_count=N
plugin_load_add                     =semisync_master.so            #
plugin_load_add                     =semisync_slave.so             #

重启master和slave数据库。

4.实际生产环境配置方法

当然实际操作中还是不建议把参数rpl_semi_sync_master_enabled 和rpl_semi_sync_slave_enabled 直接写入配置文件的。
原因:把参数加入my.cnf配置文件时,在slave库挂掉,重新开启slave库或者是slave库重启后,会自动开启增强半同步复制。
如果在slave库断开master库时间较长时,最好是先开启mysql异步复制,让slave库追赶上master库后,然后再开启增强半同步复制。这样不会拖垮master库。要是直接开启增强半同步复制是会拖垮主库的。
rpl_semi_sync_master_timeout =1000 ##1s 这个转化为异步复制的超时参数是可以写入配置文件的。

5.演示增强半同步超时被关闭

演示增强半同步复制超过设定的时间被关闭:

在master上设置超时时间

mysql> set global rpl_semi_sync_master_timeout=20000;  ##为20s

在slave上停止io_thread 线程

mysql> stop slave io_thread;

然后在master库上删除一个测试库来演示增强半同步关闭:

mysql> drop database test02;  ##删除test02库卡盾超时20s 才执行成功。
Query OK, 0 rows affected (20.00 sec)

在卡盾的20s期间,在master库上执行show proccesslist,发现这期间,master库一直在等待slave的 semi-sync 的ack的响应:

mysql>  show processlist;
+-----+-----------------+-----------+------+---------+--------+--------------------------------------+----------------------+
| Id  | User            | Host      | db   | Command | Time   | State                                | Info                 |
+-----+-----------------+-----------+------+---------+--------+--------------------------------------+----------------------+
|   1 | event_scheduler | localhost | NULL | Daemon  | 185099 | Waiting on empty queue               | NULL                 |
| 700 | root            | localhost | NULL | Query   |      3 | Waiting for semi-sync ACK from slave | drop database test02 |
| 703 | root            | localhost | NULL | Query   |      0 | starting                             | show processlist     |
| 709 | root            | localhost | NULL | Sleep   |    999 |                                      | NULL                 |
+-----+-----------------+-----------+------+---------+--------+--------------------------------------+----------------------+
4 rows in set (0.00 sec)

此时查看master库的错误日志关闭了增强半同步复制:

2019-06-16T18:58:38.249020+08:00 0 [ERROR] ./mysqld: Got an error reading communication packets      ###这个报错是slave库 stop  slave io_thread导致的
2019-06-16T18:59:14.870329+08:00 711 [ERROR] Semi-sync master failed on net_flush() before waiting for slave reply
2019-06-16T18:59:14.870409+08:00 711 [Note] Stop semi-sync binlog_dump to slave (server_id: 113306)
2019-06-16T18:59:14.870469+08:00 711 [Note] Aborted connection 711 to db: 'unconnected' user: 'rept' host: '192.168.0.11' (Found net error)
2019-06-16T18:59:34.870253+08:00 703 [Warning] Timeout waiting for reply of binlog (file: mysql-bin.000004, pos: 422036), semi-sync up to file mysql-bin.000004, position 421882.
2019-06-16T18:59:34.870298+08:00 703 [Note] Semi-sync replication switched OFF.

当超过master库上设定的20s超时时间后,增强半同步复制被关闭了。Waiting for semi-sync ACK from slave 消失

mysql>  show processlist;
+-----+-----------------+-----------+------+---------+--------+------------------------+------------------+
| Id  | User            | Host      | db   | Command | Time   | State                  | Info             |
+-----+-----------------+-----------+------+---------+--------+------------------------+------------------+
|   1 | event_scheduler | localhost | NULL | Daemon  | 185120 | Waiting on empty queue | NULL             |
| 700 | root            | localhost | NULL | Sleep   |      4 |                        | NULL             |
| 703 | root            | localhost | NULL | Query   |      0 | starting               | show processlist |
| 709 | root            | localhost | NULL | Sleep   |   1020 |                        | NULL             |
+-----+-----------------+-----------+------+---------+--------+------------------------+------------------+
4 rows in set (0.00 sec)

接着在master上删除test01库时不再出现卡盾,说明增强半同步复制已经被关闭了

mysql> drop database test01;
Query OK, 2 rows affected (0.00 sec)
此时在maser查看当前增强半同步复制中有几个slave client,发现已没有client链接

mysql> show global status like "Rpl_semi_sync_master_clients";
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| Rpl_semi_sync_master_clients | 0     |
+------------------------------+-------+
1 row in set (0.01 sec)

在slave上开启 io_thread

mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)

查看master库上的错误日志:

2019-06-16T19:10:46.656964+08:00 713 [Note] Start binlog_dump to master_thread_id(713) slave_server(113306), pos(, 4)
2019-06-16T19:10:46.657003+08:00 713 [Note] Start semi-sync binlog_dump to slave (server_id: 113306), pos(, 4)
2019-06-16T19:10:46.670872+08:00 0 [Note] Semi-sync replication switched ON at (mysql-bin.000004, 422366)

提示增强半同步有开启了。

master 查看半同步链接状态新增了1个:

mysql> show global status like "Rpl_semi_sync_master_clients";
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| Rpl_semi_sync_master_clients | 1     |
+------------------------------+-------+
1 row in set (0.00 sec)

mysql> 

6.观察master库错误日志的变化

6.1 slave库关闭io 线程,再关闭半同步复制,然后再开启 slave 的io_thread线程

mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)

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

mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
mysql> 

此刻观察master库的错误日志:

2019-06-16T20:35:14.463978+08:00 0 [ERROR] ./mysqld: Got an error reading communication packets
2019-06-16T20:35:44.309080+08:00 719 [Note] While initializing dump thread for slave with UUID <7659cf56-8e81-11e9-bcbd-842b2b5999d9>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(718).
2019-06-16T20:35:44.309252+08:00 718 [Note] Stop semi-sync binlog_dump to slave (server_id: 113306)
2019-06-16T20:35:44.309724+08:00 719 [Note] Start binlog_dump to master_thread_id(719) slave_server(113306), pos(, 4)
2019-06-16T20:35:44.309740+08:00 719 [Note] Start asynchronous binlog_dump to slave (server_id: 113306), pos(, 4)

6.2 slave库关闭io 线程,等待20s后然后再关闭半同步复制,然后再开启 slave 的io_thread线程

此刻观察master库的错误日志:

2019-06-16T20:39:18.806715+08:00 0 [ERROR] ./mysqld: Got an error reading communication packets

2019-06-16T20:39:58.854194+08:00 720 [Note] Stop semi-sync binlog_dump to slave (server_id: 113306)
2019-06-16T20:39:58.854305+08:00 720 [Note] Aborted connection 720 to db: 'unconnected' user: 'rept' host: '192.168.0.11' (failed on flush_net())

2019-06-16T20:41:06.636868+08:00 721 [Note] Start binlog_dump to master_thread_id(721) slave_server(113306), pos(, 4)
2019-06-16T20:41:06.636902+08:00 721 [Note] Start asynchronous binlog_dump to slave (server_id: 113306), pos(, 4)

发现6.1和6.2的错误日志还是有不同的地方的

7.案例

当线上运行的MySQL增强半同步复制架构中,当其中的一台slave库挂掉了应该如何正确重新添加到原先的增强半同步复制架构??

正确的做法如下:

1. 此2个参数rpl_semi_sync_master_enabled  和rpl_semi_sync_slave_enabled  不要直接写入到my.cnf配置文件开启。
2.在slave库上先 stop slave io_thread ;set global  rpl_semi_sync_slave_enabled=0 关闭此参数。然后start slave io_thread 或者start slave 开启异步复制,让slave库追赶上master库。
3.然后在slave库 set global  rpl_semi_sync_slave_enabled=1 ;stop  slave io_thread;start  slave  io_thread;

演示到此处,欢迎留言一起交流学习。

转载于:https://blog.51cto.com/wujianwei/2409754

你可能感兴趣的:(mysql5.7 开启增强半同步复制)