一、实验目的:
MySQL在主从复制还支持半同步复制,mysql复制是异步的,因为同步性能非常差。主库分发事件以后必须等待从库复制数据结束并收到从库正常响应以后,才能进行下一步操作。异步模式导致从库落后主库时,主库无从得知。因此mysql5.5后引入google补丁半同步复制,2个插件:semisync_master.so与semisync_slave.so。半同步:一主多从架构中,主库只等待一台从库复制完成数据并返回正常响应,就认为同步完成进行下一步操作,这样即有异步的高速,又有同步的安全。一旦主库等待从库响应超时,半同步复制自动降级为异步复制。半同步复制前提至少有一台从库与主库在同一机房,保证足够带宽,数据复制也足够快。因为对写性能影响极大又需要同步确认等待。
MySQL半同步复制(Semi-synchronous Replication)
二、实验步骤
1.mysql支持的模块/插件
[root@master~]# ls -t /usr/local/mysql/lib/plugin/
debug semisync_slave.so mypluglib.so qa_auth_interface.so qa_auth_client.so adt_null.so daemon_example.ini semisync_master.so libdaemon_example.so qa_auth_server.so auth_test_plugin.so auth_socket.so auth.so
2.半同步模块安装
2.1主库
mysql>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%'; 服务器变量
+------------------------------------+-------+
|Variable_name |Value |
+------------------------------------+-------+
|rpl_semi_sync_master_enabled |OFF |
|rpl_semi_sync_master_timeout |10000 | 主从库连接超时过值,自动降为异步。毫秒
|rpl_semi_sync_master_trace_level |32 |
|rpl_semi_sync_master_wait_no_slave | ON | 没有从库也要等一下
+------------------------------------+-------+
mysql>SET GLOBAL rpl_semi_sync_master_enabled=1;
mysql>SET GLOBAL rpl_semi_sync_master_timeout=1000;
mysql>SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
|Variable_name | Value |
+--------------------------------------------+-------+
|Rpl_semi_sync_master_clients | 1 | 从节点1个
|Rpl_semi_sync_master_net_avg_wait_time | 0 |
|Rpl_semi_sync_master_net_wait_time | 0 |
|Rpl_semi_sync_master_net_waits | 0 |
|Rpl_semi_sync_master_no_times | 0 |
|Rpl_semi_sync_master_no_tx | 0 |
|Rpl_semi_sync_master_status | ON |
|Rpl_semi_sync_master_timefunc_failures | 0 |
|Rpl_semi_sync_master_tx_avg_wait_time | 0 |
|Rpl_semi_sync_master_tx_wait_time | 0 |
|Rpl_semi_sync_master_tx_waits | 0 |
|Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
|Rpl_semi_sync_master_wait_sessions | 0 |
|Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
mysql>USE school;
mysql>CREATE TABLE tb2(name CHAR(20));
mysql>SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
|Variable_name | Value |
+--------------------------------------------+-------+
|Rpl_semi_sync_master_clients | 1 |
|Rpl_semi_sync_master_net_avg_wait_time | 800 | 等待从服返回报告时间
|Rpl_semi_sync_master_net_wait_time | 800 |
|Rpl_semi_sync_master_net_waits | 1 |
|Rpl_semi_sync_master_no_times | 0 |
|Rpl_semi_sync_master_no_tx | 0 |
|Rpl_semi_sync_master_status | ON |
|Rpl_semi_sync_master_timefunc_failures | 0 |
|Rpl_semi_sync_master_tx_avg_wait_time | 715 | 等待从服事务返回报告时间
|Rpl_semi_sync_master_tx_wait_time | 715 |
|Rpl_semi_sync_master_tx_waits | 1 |
|Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
|Rpl_semi_sync_master_wait_sessions | 0 |
|Rpl_semi_sync_master_yes_tx | 1 |
+--------------------------------------------+-------+
mysql>INSERT INTO tb2 VALUES ('tom'),('jerry'),('jack');
mysql>SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
|Variable_name | Value |
+--------------------------------------------+-------+
|Rpl_semi_sync_master_clients | 1 |
|Rpl_semi_sync_master_net_avg_wait_time | 1518 |
|Rpl_semi_sync_master_net_wait_time | 3037 |
|Rpl_semi_sync_master_net_waits | 2 |
|Rpl_semi_sync_master_no_times | 0 |
|Rpl_semi_sync_master_no_tx | 0 |
|Rpl_semi_sync_master_status | ON |
|Rpl_semi_sync_master_timefunc_failures | 0 |
|Rpl_semi_sync_master_tx_avg_wait_time | 715 |
|Rpl_semi_sync_master_tx_wait_time | 715 |
|Rpl_semi_sync_master_tx_waits | 1 |
|Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
|Rpl_semi_sync_master_wait_sessions | 0 |
|Rpl_semi_sync_master_yes_tx | 2 |
+--------------------------------------------+-------+
从库
mysql>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
+---------------------------------+-------+
|Variable_name | Value |
+---------------------------------+-------+
|rpl_semi_sync_slave_enabled |OFF |
|rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
mysql>SET GLOBAL rpl_semi_sync_slave_enabled=1;
mysql>STOP SLAVE;
mysql>START SLAVE;
mysql>SHOW GLOBAL STATUS LIKE '%semi%';
+----------------------------+-------+
|Variable_name | Value |
+----------------------------+-------+
|Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
永久配置在Master和Slave的my.cnf中编辑:
# OnMaster
[root@master~]# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000# 1 second
验证
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
+------------------------------------+-------+
|Variable_name | Value |
+------------------------------------+-------+
|rpl_semi_sync_master_enabled |ON |
|rpl_semi_sync_master_timeout |1000 |
|rpl_semi_sync_master_trace_level |32 |
|rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
# OnSlave
[root@slave~]# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_slave_enabled=1
验证
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
+---------------------------------+-------+
|Variable_name | Value |
+---------------------------------+-------+
|rpl_semi_sync_slave_enabled | ON |
|rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+