MySQL半同步的原理是主庫只需要確認從庫接收到了事物即可,無需等待從庫應用,相比異步復制,半同步提高了數(shù)據(jù)完整性的保障,但會增加主庫的響應時間。
1、安裝Mysql并配置主從
參考http://blog.itpub.net/28536251/viewspace-2138854/分別在兩節(jié)點安裝Mysql。
參考http://blog.itpub.net/28536251/viewspace-2138928/或者http://blog.itpub.net/28536251/viewspace-2139007/配置主從。
2、在master節(jié)點加載半同步插件
(root@localhost)[(none)] install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.16 sec)
3、在slave節(jié)點加載半同步插件
(root@localhost)[(none)] install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.45 sec)
4、在master節(jié)點設置以下變量
(root@localhost)[(none)] set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.05 sec)
#rpl_semi_sync_master_enabled用于控制是否在master節(jié)點啟用半同步復制,為1表示啟動。
(root@localhost)[(none)] set global rpl_semi_sync_master_timeout=3000;
Query OK, 0 rows affected (0.00 sec)
#rpl_semi_sync_master_timeout用于指定master節(jié)點等待slave響應的事件,單位是毫秒,默認是10000即10秒鐘,這里設置為3秒。若超出指定時間slave節(jié)點仍無響應,那么當前復制環(huán)境就臨時被轉換為異步復制。
5、在slave節(jié)點設置以下變量
(root@localhost)[(none)] set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.09 sec)
#rpl_semi_sync_slave_enabled用來控制slave節(jié)點是否啟用半同步復制。
以上3個變量雖然可以動態(tài)修改,但強烈建議將所有配置的變量都保存在初始化參數(shù)文件中,這樣在每次啟動mysql服務時就無需再手動配置了。
6、在slave節(jié)點重啟io_thread線程
slave節(jié)點重新連接master節(jié)點,注冊為半同步身份。
(root@localhost)[(none)] stop slave io_thread;
Query OK, 0 rows affected (0.12 sec)
(root@localhost)[(none)] start slave io_thread;
Query OK, 0 rows affected (0.02 sec)
7、測試
主庫插入數(shù)據(jù):
(root@localhost)[(none)] use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
(root@localhost)[test] show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
+----------------+
1 row in set (0.00 sec)
(root@localhost)[test] select * from tb1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.03 sec)
(root@localhost)[test] insert into tb1 values(2);
Query OK, 1 row affected (0.10 sec)
(root@localhost)[test] select * from tb1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.02 sec)
(root@localhost)[test] show status like 'rpl_semi_sync%';
+--------------------------------------------+-------+
| 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 | 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 | 52899 |
| Rpl_semi_sync_master_tx_wait_time | 52899 |
| 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.05 sec)
其中:
rpl_semi_sync_master_clients為1表示處于半同步模式的slave節(jié)點有1個。
rpl_semi_sync_master_status為ON表示master節(jié)點啟用了半同步模式。
rpl_semi_sync_master_no_tx為0表示還沒有未成功發(fā)送到slave節(jié)點的事物數(shù)量。
rpl_semi_sync_master_yes_tx為1表示已成功發(fā)送到slave節(jié)點的事物數(shù)量為1。
備庫查看:
(root@localhost)[test] select * from tb1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
當前題目:Mysql半同步配置
當前路徑:
http://weahome.cn/article/jcphdo.html