前提是主從異步複製環境要提早搭建好,而後再開啓mysql加強半同步mysql
環境:mysql5.7.26 主從異步複製早已部署好。linux
建議master和slave上所有執行(考慮到MHA的主從自動切換的環境)sql
在主庫安裝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庫上架構
生產環境上建議先在slave庫上的開啓 加強半同步參數異步
set global rpl_semi_sync_slave_enabled =1; stop slave io_thread;start slave io_thread;
此刻觀察master的錯誤日誌:提示開啓了Semi-sync replication 複製async
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上再開啓加強半同步參數ide
set global rpl_semi_sync_master_enabled =1 set global rpl_semi_sync_master_timeout =1000
官方要求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數據庫。
固然實際操做中仍是不建議把參數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 這個轉化爲異步複製的超時參數是能夠寫入配置文件的。
演示加強半同步複製超過設定的時間被關閉:
在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.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的錯誤日誌仍是有不一樣的地方的
當線上運行的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;
演示到此處,歡迎留言一塊兒交流學習。