IP架构
主:172.17.100.106 3306
从1:172.17.100.107 3306
从2:172.17.100.107 3307
半同步靠的是插件来实现的,首先需要对插件进行安装
通过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)
完成安装之后,主从库的半同步均为关闭状态,需要在主库和从库上分别开启半同步
主:
root@localhost [(none)]>SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
从(双实例上分别执行,这里和后面都只演示一个):
(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)
检查一下从库的状态
show slave status \G
确认io和sql线程均为yes
检查一下主从上的半同步状态
主:
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)
---------------------------------------------------------------------------------------
小测试
把从库的2个实例都关闭,然后在主库上执行建库操作
root@localhost [(none)]>create database qimo;
Query OK, 1 row affected (10.00 sec)
整整花费10秒,刚好timeout的设置时间
再看主库上的error.log,无法收到从库反馈的ack消息,直接报错,提示通信错误
最后一句显示已经从半同步模式切换成了异步模式
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.