一:前提条件主从复制已经做好(见前文)
二、实现半同步复制的前提条件
1.安装plugin插件
MySQL默认是不支持半同步的,是用plugin插件插件来支持的。不论是二进制还是编译安装的,
都会默认生成这个插件,在使用之前必须安装这个插件。
2.安装需要分别在主从端开启该服务
三、环境准备:检查系统环境
[[email protected] ~]# uname -r
2.6.32-431.el6.x86_64
[[email protected] ~]# uname -n
master
[[email protected] ~]#cd /usr/local/mysql/lib/plugin 查看插件的路径
[[email protected] plugin]# ls
adt_null.so debug qa_auth_server.so
auth.so libdaemon_example.so semisync_master.so
auth_socket.so mypluglib.so semisync_slave.so
auth_test_plugin.so qa_auth_client.so validate_password.so
daemon_example.ini qa_auth_interface.so
这两个文件必须存在:semisync_master.so semisync_slave.so
2.主从复制搭建必须成功
3.主库Master上面配置半同步
(1)[[email protected] ~]#/usr/local/mysql/lib/plugin 插件的位置
(2)查看数据库是否支持动态载入插件,该项必须yes:
mysql> show global variables like ‘have_dynamic_loading‘;
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.00 sec)
mysql>
(3)mysql> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘;
Query OK, 0 rows affected (0.02 sec) 安装插件成功
mysql>
(4)开启半同步插件服务
mysql> set global rpl_semi_sync_master_enabled = 1;
Query OK, 该项参数0和1表示,1表示开启,0表示关闭。
注意:这种设置方式是临时生效,永久生效写的my.cnf 且重启服务
mysql> set global rpl_semi_sync_master_timeout = 5000;
Query OK, 0 rows affected (0.00 sec) 设置超时时间为5S,表示5s未收到从的响应,自动转换为异步模式。
mysql>
[mysqld] 永久生效
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000
4.从库Slave上配置半同步
(1)[[email protected] ~]#/usr/local/mysql/lib/plugin 插件的位置
(2)查看数据库是否支持动态载入插件,该项必须yes:
mysql> show global variables like ‘have_dynamic_loading‘;
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.00 sec)
mysql>
(3)安装半同步插件
mysql> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;
Query OK, 0 rows affected (0.04 sec)
(4)开启半同步插件服务
mysql> set global rpl_semi_sync_slave_enabled = 1; 临时生效
Query OK, 0 rows affected (0.00 sec)
[mysqld]
rpl_semi_sync_slave_enabled=1 需要重启服务
四、安装完成,检查配置
(1)mysql> select * from information_schema.plugins\G 检查安装成功与否的状态
(2)mysql> show status like "%semi%"; 查看半同步的复制状态
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 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 |表示没有成功接收slave提交的次数
| Rpl_semi_sync_master_status | ON |表示当前是异步模式还是半同步模式,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 | 当前有多少个session因为slave的回复而造成等待
| Rpl_semi_sync_master_yes_tx | 0 |成功接受到slave事物回复的次数
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
mysql>
(3)创建库,试试看看Rpl_semi_sync_master_yes_tx是否发生变化,变化表示成功采用半同步复制
mysql> create database cisco;
Query OK, 1 row affected (0.03 sec)
mysql> show status like "%semi%";
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 225 |
| Rpl_semi_sync_master_net_wait_time | 225 |
| 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 | 253 |
| Rpl_semi_sync_master_tx_wait_time | 253 |
| 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***变化了 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
mysql>
(4)把超时时间设置为0测试,看它是否转换为异步状态
mysql> set global rpl_semi_sync_master_timeout = 0;
mysql> show status like "%semi%";
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 223 |
| Rpl_semi_sync_master_net_wait_time | 446 |
| Rpl_semi_sync_master_net_waits | 2 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 1 |超时为0,Rpl_semi_sync_master_no_tx,参数变化,代表未走半同步的机制,走的异步。
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 253 |
| Rpl_semi_sync_master_tx_wait_time | 253 |
| 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 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
mysql>
(5)总结;只有事务提交才会记录Rpl_semi_sync_master_yes_tx 1 值;查询不会记录
五、取消半同步复制的插件
192.168.11.34上:
# mysql > uninstall plugin rpl_semi_sync_master;
# mysql > show status like "%semi%"
192.168.11.35上:
# mysql > uninstall plugin rpl_semi_sync_slave;
# mysql > show status like "%semi%"