1. 程式人生 > >Mysql 半同步複製和非同步複製

Mysql 半同步複製和非同步複製

mysql 半同步複製和非同步複製

-- 在主庫中安裝半同步外掛,開啟半同步複製功能

install plugin rpl_semi_sync_master soname 'semisync_master.so';
set global rpl_semi_sync_master_enabled=on;
show plugins;


mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.02 sec)

mysql>

mysql> set global rpl_semi_sync_master_enabled=on;
Query OK, 0 rows affected (0.00 sec)

mysql>

mysql> show variables like '%semi%';
+------------------------------------+-------+
| 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    |
+------------------------------------+-------+
4 rows in set (0.00 sec)

mysql>

-- 在從庫安裝半同步複製外掛,開啟半同步複製功能

install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
set global rpl_semi_sync_slave_enabled=on;

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.04 sec)

mysql> set global rpl_semi_sync_slave_enabled=on;
Query OK, 0 rows affected (0.00 sec)

mysql>

-- 重啟從庫的IO執行緒,啟用半同步複製

stop slave io_thread;
start slave io_thread;


mysql> stop slave io_thread;
Query OK, 0 rows affected (0.03 sec)

mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)

mysql>

-- 在主庫上檢視半同步複製是否正常執行

mysql> show global status like '%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              | 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>

-- 在從庫上檢視半同步狀態

mysql> show global status like '%semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql>

-- 半同步複製和非同步模式的切換

-- 檢視半同步方式的預設的timeout的值,預設是10秒

mysql> show variables like '%semi_sync_master_timeout%';
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| rpl_semi_sync_master_timeout | 10000 |
+------------------------------+-------+
1 row in set (0.00 sec)

mysql>

-- 把從庫的IO thread關閉,並檢視從庫的半同步狀態

stop slave io_thread;
show global status like '%semi%';

mysql> stop slave io_thread;
Query OK, 0 rows affected (0.09 sec)
mysql> show global status like '%semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF   |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql>

-- 檢視主庫的半同步狀態,是開啟的,並且有從庫連線

show global status like '%semi%';
mysql> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 2953  |
| Rpl_semi_sync_master_net_wait_time         | 64966 |
| Rpl_semi_sync_master_net_waits             | 22    |
| 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      | 4409  |
| Rpl_semi_sync_master_tx_wait_time          | 44093 |
| Rpl_semi_sync_master_tx_waits              | 10    |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 22    |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

mysql>

-- 然後在主庫上插入語句,發現執行這條語句很慢,主庫一直在等待從庫的回覆,知道超過了預設的10秒

insert into t1 values('b');
mysql> insert into t1 values('b');
Query OK, 1 row affected (10.11 sec)

mysql>

-- 這個時候,再次檢視主庫的半同步複製狀態,發現主庫的半同步模式是OFF。說明已經從半同步複製自動切換到了非同步複製模式了

mysql> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 2953  |
| Rpl_semi_sync_master_net_wait_time         | 64966 |
| Rpl_semi_sync_master_net_waits             | 22    |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 4409  |
| Rpl_semi_sync_master_tx_wait_time          | 44093 |
| Rpl_semi_sync_master_tx_waits              | 10    |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 22    |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

mysql>

-- 再次從非同步複製模式切換為半同步模式,則需要重啟從庫的IO thread .

start slave io_therad;
mysql> show global status like '%semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql>
mysql> show global status like '%semi%';
+--------------------------------------------+--------+
| Variable_name                              | Value  |
+--------------------------------------------+--------+
| Rpl_semi_sync_master_clients               | 1      |
| Rpl_semi_sync_master_net_avg_wait_time     | 7618   |
| Rpl_semi_sync_master_net_wait_time         | 175232 |
| Rpl_semi_sync_master_net_waits             | 23     |
| Rpl_semi_sync_master_no_times              | 1      |
| 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      | 4409   |
| Rpl_semi_sync_master_tx_wait_time          | 44093  |
| Rpl_semi_sync_master_tx_waits              | 10     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0      |
| Rpl_semi_sync_master_wait_sessions         | 0      |
| Rpl_semi_sync_master_yes_tx                | 22     |
+--------------------------------------------+--------+
14 rows in set (0.00 sec)

mysql>

END