開啓半同步複製mysql
#在有的高可用架構下,master和slave需同時啓動,以便在切換後能繼續使用半同步複製sql
/etc/my.cnf
架構
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" rpl-semi-sync-master-enabled = 1 rpl-semi-sync-slave-enabled = 1
查看半同步是否在運行異步
master:
mysql> show status like 'Rpl_semi_sync_master_status';
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON |
+-----------------------------+-------+測試
slave:
mysql> show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+spa
重啓slave上的IO線程使半同步生效線程
mysql> stop slave io_thread;code
mysql> start slave io_thread;
#若是沒有重啓,則默認仍是異步複製,重啓後,slave會在master上註冊爲半同步複製的slave角色。blog
半同步複製測試同步
master: mysql> create database db; mysql> use db Database changed mysql> create table t1(id int); mysql> insert into t1 values(1); mysql> select * from t1; +------+| id |+------+| 1 |+------+1 row in set (0.00 sec) slave: mysql> select * from db.t1; +------+| id |+------+| 1 |+------+1 row in set (0.00 sec) #能夠看到數據很快同步到了從庫上,下面關閉io_thread測試 slave: mysql> stop slave io_thread; Query OK, 0 rows affected (0.00 sec)
master: mysql> insert into t1 values(2); #此處有一個10s的超時等待時間,超時後轉爲異步插入 Query OK, 1 row affected (10.11 sec) mysql> show status like 'Rpl_semi_sync_master_status'; #半同步已失效 +-----------------------------+-------+| Variable_name | Value | +-----------------------------+-------+| Rpl_semi_sync_master_status | OFF | +-----------------------------+-------+
slave: mysql> show status like 'Rpl_semi_sync_slave_status'; #從庫的半同步也失效 +----------------------------+-------+| Variable_name | Value | +----------------------------+-------+| Rpl_semi_sync_slave_status | OFF | +----------------------------+-------+1 row in set (0.01 sec)
slave: mysql> start slave io_thread; #從庫開啓io線程 Query OK, 0 rows affected (0.00 sec)
master: mysql> show status like 'Rpl_semi_sync_master_status'; #又從新轉爲半同步複製 +-----------------------------+-------+| Variable_name | Value | +-----------------------------+-------+| Rpl_semi_sync_master_status | ON | +-----------------------------+-------+1 row in set (0.00 sec)
slave: mysql> select * from db.t1; #從庫上數據已同步 +------+| id |+------+| 1 || 2 |+------+2 rows in set (0.00 sec)