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;

演示到此處,歡迎留言一起交流學習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章