MySQL半同步复制实践
半同步复制
MySQL的主从复制在5.5版本以前只支持异步复制,也就是说主库在执行一些事务后,是不管从库备库的进度的,这种方式最大的好处是速度快、效率高;缺点就是在主库宕机后,不能确保从库和主库的数据一致性。
半同步复制的好处就是,主库在每执行一次事务后,会等待备库接受日志后才返回给客户端,如果做的是小事务,两台主机的延迟较小,则可以实现在损失很小的性能的情况下保证零数据丢失。
原理
1)主库每执行一次事务都会先让备库读取日志,确保至少有一台备库上面的数据和自己完整
2)如果在一个timeout超时时间内主库仍旧没有收到备库的应答,则自动q切换回异步模式,保证业务运行
3)半同步复制只能保证从库读取了日志,并不能保证数据写入到了从库数据库中,还得看从库sql进程执行情况
4)如果有从库进度追赶上了主库,模式继续切换回半同步状态
mysql> show variables like 'plugin_dir';
+---------------+--------------------------------+
| Variable_name | Value |
+---------------+--------------------------------+
| plugin_dir | /application/mysql/lib/plugin/ | #插件位置
+---------------+--------------------------------+
1 row in set (0.00 sec)
主库安装插件
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> show variables like '%rpl_semi_sync%'; #查看配置状态
+---------------------------------+-------+
| 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_master_enabled = 1; #开启Semi-sync功能
mysql> set global rpl_semi_sync_master_timeout = 10000; #设置超时时间为10秒
mysql> show variables like '%rpl_semi_sync%';
+------------------------------------+-------+
| 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_no_slave | ON |
+------------------------------------+-------+
加入配置文件my.cnf中
[mysqld]
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 10000
从库
安装插件
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> show variables like '%rpl_semi_sync%';
+---------------------------------+-------+
| 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; #开启插件
mysql> stop slave; #重启从库IO进程
mysql> start slave;
此时在主库这边可以查看半同步复制状态
mysql> show global status like '%rpl_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 | 2 |
| Rpl_semi_sync_master_no_tx | 1 |
| 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 |
+--------------------------------------------+-------+
14 rows in set (0.01 sec)
做几个简单的测试:主库插入一行数据
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | 小明 |
+----+--------+
1 row in set (0.00 sec)
mysql> insert into test values(2,'test');
Query OK, 1 row affected (0.00 sec) #可以看见延迟几乎可以忽略
从库这边,数据正常说明成功了~
12345678 mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | 小明 |
| 2 | test |
+----+--------+
2 rows in set (0.00 sec)
模拟下从库故障
mysql> stop slave IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
我们再在主从插入数据
mysql> insert into test values(3,'test');
Query OK, 1 row affected (10.01 sec) #插入时间很久,10秒钟,10秒超时后,自动切换回异步复制了
mysql> show global status like '%rpl_semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 498 |
| Rpl_semi_sync_master_net_wait_time | 1495 |
| Rpl_semi_sync_master_net_waits | 3 |
| Rpl_semi_sync_master_no_times | 3 |
| Rpl_semi_sync_master_no_tx | 2 |
| Rpl_semi_sync_master_status | OFF | #关闭状态
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 799 |
| Rpl_semi_sync_master_tx_wait_time | 1599 |
| Rpl_semi_sync_master_tx_waits | 2 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 2 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
恢复IO线程
mysql> start slave IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> show global status like '%rpl_semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 4526 |
| Rpl_semi_sync_master_net_wait_time | 18107 |
| Rpl_semi_sync_master_net_waits | 4 |
| Rpl_semi_sync_master_no_times | 3 |
| Rpl_semi_sync_master_no_tx | 2 |
| Rpl_semi_sync_master_status | ON | #重新开启了
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 799 |
| Rpl_semi_sync_master_tx_wait_time | 1599 |
| Rpl_semi_sync_master_tx_waits | 2 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 2 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)