IP架構(gòu)
主:172.17.100.106????3306
從1:172.17.100.107????3306
從2:172.17.100.107? ? 3307
半同步靠的是插件來實(shí)現(xiàn)的苍狰,首先需要對(duì)插件進(jìn)行安裝
通過show plugins可以看到加載的插件
主庫
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]>root@localhost [(none)]>show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name? ? ? ? ? ? ? ? ? ? ? | Status? | Type? ? ? ? ? ? ? | Library? ? ? ? ? ? | License |
+----------------------------+----------+--------------------+--------------------+---------+
...
| rpl_semi_sync_master? ? ? | ACTIVE? | REPLICATION? ? ? ? | semisync_master.so | GPL? ? |
+----------------------------+----------+--------------------+--------------------+---------+
45 rows in set (0.00 sec)
也可以通過show variables來查看
root@localhost [(none)]>show variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name? ? ? ? ? ? ? ? ? ? ? ? ? ? | Value? ? ? |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled? ? ? ? ? ? ? | OFF? ? ? ? |
| rpl_semi_sync_master_timeout? ? ? ? ? ? ? | 10000? ? ? |? ?(單位是毫秒驶悟,也就是10秒)
| 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)
從庫
(product)root@localhost [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
(product)root@localhost [(none)]> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name? ? ? ? ? ? ? ? ? | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled? ? | OFF? |
| rpl_semi_sync_slave_trace_level | 32? ? |
+---------------------------------+-------+
2 rows in set (0.00 sec)
完成安裝之后,主從庫的半同步均為關(guān)閉狀態(tài)憎兽,需要在主庫和從庫上分別開啟半同步
主:
root@localhost [(none)]>SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
從(雙實(shí)例上分別執(zhí)行冷离,這里和后面都只演示一個(gè)):
(product)root@localhost [(none)]> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.01 sec)
重啟一下從庫的io_thread
(product)root@localhost [(none)]> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)
(product)root@localhost [(none)]> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
檢查一下從庫的狀態(tài)
show slave status \G
確認(rèn)io和sql線程均為yes
檢查一下主從上的半同步狀態(tài)
主:
root@localhost [(none)]>show variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name? ? ? ? ? ? ? ? ? ? ? ? ? ? | Value? ? ? |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled? ? ? ? ? ? ? | ON? ? ? ? |
| 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)
從:
(product)root@localhost [(none)]> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name? ? ? ? ? ? ? ? ? | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled? ? | ON? ? |
| rpl_semi_sync_slave_trace_level | 32? ? |
+---------------------------------+-------+
2 rows in set (0.00 sec)
再來看一下主庫的error.log
2018-05-17T12:10:21.580190Z 28 [Note] While initializing dump thread for slave with UUID <5cbb6cc8-5287-11e8-b786-005056ba0d70>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(8).
2018-05-17T12:10:21.580267Z 8 [Note] Stop asynchronous binlog_dump to slave (server_id: 1073306)
2018-05-17T12:10:21.580529Z 28 [Note] Start binlog_dump to master_thread_id(28) slave_server(1073306), pos(, 4)
2018-05-17T12:10:21.580566Z 28 [Note] Start semi-sync binlog_dump to slave (server_id: 1073306), pos(, 4)
2018-05-17T12:10:29.166518Z 29 [Note] While initializing dump thread for slave with UUID <5d6b91e1-5418-11e8-9657-005056ba0d70>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(9).
2018-05-17T12:10:29.166610Z 29 [Note] Start binlog_dump to master_thread_id(29) slave_server(1073307), pos(, 4)
2018-05-17T12:10:29.166638Z 9 [Note] Stop asynchronous binlog_dump to slave (server_id: 1073307)
2018-05-17T12:10:29.589118Z 29 [Note] Start semi-sync binlog_dump to slave (server_id: 1073307), pos(, 4)
---------------------------------------------------------------------------------------
小測(cè)試
把從庫的2個(gè)實(shí)例都關(guān)閉,然后在主庫上執(zhí)行建庫操作
root@localhost [(none)]>create database qimo;
Query OK, 1 row affected (10.00 sec)
整整花費(fèi)10秒纯命,剛好timeout的設(shè)置時(shí)間
再看主庫上的error.log西剥,無法收到從庫反饋的ack消息,直接報(bào)錯(cuò)亿汞,提示通信錯(cuò)誤
最后一句顯示已經(jīng)從半同步模式切換成了異步模式
2018-05-17T12:41:19.859710Z 0 [ERROR] /usr/local/mysql/bin/mysqld: Got an error reading communication packets
2018-05-17T12:41:28.011673Z 0 [ERROR] /usr/local/mysql/bin/mysqld: Got an error reading communication packets
2018-05-17T12:41:59.179369Z 28 [Note] Stop semi-sync binlog_dump to slave (server_id: 1073306)
2018-05-17T12:41:59.595612Z 29 [Note] Stop semi-sync binlog_dump to slave (server_id: 1073307)
2018-05-17T12:42:00.046895Z 28 [Note] Aborted connection 28 to db: 'unconnected' user: 'repl' host: '172.17.100.107' (failed on flush_net())
2018-05-17T12:42:00.047057Z 0 [ERROR] /usr/local/mysql/bin/mysqld: Got an error reading communication packets
2018-05-17T12:42:01.048156Z 29 [Note] Aborted connection 29 to db: 'unconnected' user: 'repl' host: '172.17.100.107' (failed on flush_net())
2018-05-17T12:42:15.491920Z 31 [Warning] Timeout waiting for reply of binlog (file: mysql-bin.000006, pos: 353), semi-sync up to file , position 4.
2018-05-17T12:42:15.491993Z 31 [Note] Semi-sync replication switched OFF.