简述MySQL半同步复制—semisync
MySQL的半同步复制-semisync是基于默认的异步复制和完全同步复制之间,它是在master在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个slave收到并写到relay log中才返回给客户端。相对于异步复制,semisync提高了数据的安全性,但是又比完全同步性能好,所以master和slave之间的时间一定要一致,以免造成semisync失败。MySQL的半同步复制的插件是由Google免费提供的在MySQL的$basedir/lib/plugin下有相应的动态库文件,linux中是.so,DOS系统中是.dll,在默认情况下MySQL是没有装载semisync的插件,所以要制作semisync要手动在MySQL的shell中装载插件,下面就简单的说一说MySQL的semisync配置,这里要注意要使用MySQL的super用户来配置,并且MySQL要5.5及以上版本才有,并且要在有异步复制的基础上才可以做配置
#master中
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.00 sec)
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 |
+------------------------------------+-------+
4 rows in set (0.01 sec)
mysql> set global rpl_semi_sync_master_enabled = 1;#开启master上的semisync,设置超时时间,可以通过设置rpl_semi_sync_master_timeout变量来修改,默认单位为毫秒
Query OK, 0 rows affected (0.00 sec)
mysql> show status like '%Rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 565 |
| Rpl_semi_sync_master_net_wait_time | 3391 |
| Rpl_semi_sync_master_net_waits | 6 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 12 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 588 |
| Rpl_semi_sync_master_tx_wait_time | 3528 |
| Rpl_semi_sync_master_tx_waits | 6 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 6 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
#slave中
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
mysql> show global 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)
mysql> set global rpl_semi_sync_slave_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave IO_THREAD;start slave IO_THREAD;#重启slave上的IO线程
Query OK, 0 rows affected (0.09 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show status like '%Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
这样简单的semisync就配置好了,这里要注意的是如果master超过超时时间没有接收到slave的消息时会自动转为异步复制,当然如果不需要semisync可以直接uninstall相应的插件
mysql> uninstall plugin rpl_semi_sync_master;
Query OK, 0 rows affected (0.00 sec)
mysql> uninstall plugin rpl_semi_sync_slave;
Query OK, 0 rows affected (0.00 sec)