Mysql半同步配置

stonebox1122發表於2017-05-15
Mysql半同步的原理是主庫只需要確認從庫接收到了事物即可,無需等待從庫應用,相比非同步複製,半同步提高了資料完整性的保障,但會增加主庫的響應時間。
1、安裝Mysql並配置主從
參考http://blog.itpub.net/28536251/viewspace-2138854/分別在兩節點安裝Mysql。
參考http://blog.itpub.net/28536251/viewspace-2138928/或者http://blog.itpub.net/28536251/viewspace-2139007/配置主從。

2、在master節點載入半同步外掛
(root@localhost)[(none)] install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.16 sec)

3、在slave節點載入半同步外掛
(root@localhost)[(none)] install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.45 sec)

4、在master節點設定以下變數
(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節點啟用半同步複製,為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節點等待slave響應的事件,單位是毫秒,預設是10000即10秒鐘,這裡設定為3秒。若超出指定時間slave節點仍無響應,那麼當前複製環境就臨時被轉換為非同步複製。

5、在slave節點設定以下變數
(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節點是否啟用半同步複製。

以上3個變數雖然可以動態修改,但強烈建議將所有配置的變數都儲存在初始化引數檔案中,這樣在每次啟動mysql服務時就無需再手動配置了。

6、在slave節點重啟io_thread執行緒
slave節點重新連線master節點,註冊為半同步身份。
(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、測試
主庫插入資料:
(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節點有1個。
rpl_semi_sync_master_status為ON表示master節點啟用了半同步模式。
rpl_semi_sync_master_no_tx為0表示還沒有未成功傳送到slave節點的事物數量。
rpl_semi_sync_master_yes_tx為1表示已成功傳送到slave節點的事物數量為1。

備庫檢視:
(root@localhost)[test] select * from tb1;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2139097/,如需轉載,請註明出處,否則將追究法律責任。

相關文章