- 半同步复制:
- 5.5集成到MySQL,以插件的形式存在,需要单独安装
- 确保事务提交后bnog至少传输到一个从库
- 不保证从库应用完这个事务的binlog
- 性能有一定的降低,响应时间会更长
- 网络异常或从库宕机,卡住主库直到超时或从库恢复
异步复制的逻辑
半同步复制的逻辑
安装
主库上
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
mysql> show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
...
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
从库上
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)
mysql> show plugins;
+----------------------------+----------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
...
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
查看semi-sync的参数
#主库
mysql> 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_no_slave | ON |
+------------------------------------+-------+
4 rows in set (0.00 sec)
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
#从库
mysql> 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)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
重启主从复制
#从库
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show global status like '%semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.01 sec)
#主库
mysql> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |#表示有一个semi—sync从库在进行复制
| 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 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
测试
#主库
mysql> select * from test;
+------+------+
| id | name |
+------+------+
| 1 | m1 |
| 2 | m2 |
| 3 | m3 |
| 4 | m4 |
+------+------+
4 rows in set (0.00 sec)
mysql> insert into test values (5,'m5');
Query OK, 1 row affected (0.00 sec)
#从库
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
#主库
mysql> insert into test values (6,'m6');
Query OK, 1 row affected (10.00 sec)#可以看到,主库插入用了10秒钟
修改超时参数,默认的10秒钟太长了,如果从库出问题的话,那么有可能引起雪崩效应
#从库
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
#主库
mysql> set global rpl_semi_sync_master_timeout=100;
Query OK, 0 rows affected (0.00 sec)
#从库
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
#主库
mysql> insert into test values (7,'m7');
Query OK, 1 row affected (0.10 sec)
半同步复制状态