MySQL主從複製之半同步模式

   MySQL半同步介紹:         mysql

       通常狀況下MySQL默認複製模式爲異步,何爲異步?簡單的說就是主服務器上的I/O threads 將binlog寫入二進制日誌中就返回給客戶端一個結果,無需等待二進制日誌是否成功發送到從庫和從庫上是否成功完成relay log的寫入以及SQL threads從relay log中提取二進制日誌寫入本身binlog的過程,異步模式的缺點就是一旦主庫寫入binlog日誌後發生宕機,此時從庫還未完成對主庫傳送過來binlog的讀寫存儲操做,那麼從庫就會發生數據丟失的現象。因此這裏就出現了另一種複製模式,即半同步模式。linux

在半同步模式下,主庫將binlog日誌信息寫入到日誌中,而後等待從庫完成後面的接收讀取和存儲操做,並返回主庫一個明確信息,即從庫接收完主庫傳遞過來的binlog內容已經寫入到本身的relay log中,纔會通知主庫上的等待線程,該操做已經完成,此時主庫接收到從庫發過來的信息就會進入下一個同步操做動做,若是在此時等待超時,超過主庫中設置的超時時間限制(通常是rpl_semi_sync_master_timeout的值),則關閉半同步,並切換爲異步模式,直到至少有一臺從庫告訴主庫已經接收到主庫傳送過來的binlog信息爲止。sql

    MySQL半同步特色: 安全

一、從庫會在鏈接到主庫時告訴主庫,它是否是匹配了半同步服務器

二、若是半同步在主庫端是開啓了的,而且至少有一個半同步複製的從庫節點,那麼此時主庫的事務線程在提交時會被阻塞並等待,結果有兩種可能:要麼至少有一個從庫節點通知它已經收到了這個事務的binlog事件,要麼一直等待到超時直至到咱們配置的某一個時間點爲止,而此時,半同步複製將自行關閉,轉換爲異步複製模式session

三、從庫節點只有在接收到某一個事務的全部binlog將其寫入並Flash到Relay Log文件以後,纔會通知對應主庫上的等待線程。異步

四、若是在等待過程當中,主庫等待的時間已經超過了配置的超時時間,沒有任何一個從庫節點通知當前事務,那麼此時主庫會自動轉換爲異步複製模式,若是當前只要有一個同步從節點遇上來,主庫便會又自動轉換爲半同步複製方式。 spa

五、半同步模式必須在主庫和從庫上都開啓,若是主庫上沒打開,或者在主庫上開啓而從庫沒開啓,主庫都會使用異步模式複製操作系統

實驗環境:插件

操做系統:

cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
主從服務器IP地址規劃:

主:192.168.0.100

從:192.168.0.101

MySQL版本:

mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

半同步複製模式提升了主從之間數據的一致性,讓複製更加安全,從MySQL5.7版本之後,又增長了rpl_semi_rsyn_master_wait_point參數,用來控制半同步模式下主庫在返回給session事務成功以前的事務提交方式,該參數有兩個值:

a、AFTER_COMMIT

該值是MySQL5.6版本的默認值,主要意思是主庫將每一個事務寫入binlog,並傳遞給從庫,刷新到中繼日誌中,同時主庫提交事務。以後主庫開始等待從庫的反饋,只有收到從庫 的回覆,master纔將「commit OK」 的結果返回給客戶端。

b、AFTER_SYNC

該值的是MySQL5.7版本以後新增的,也是在MySQL5.7默認的半同步複製方式。主要意思是主庫將每一個事務寫入本身的binlog並傳遞給從庫,刷新到中繼日誌中,主庫開始等待從庫的反饋,當主庫接收到從庫的回覆以後,再提交事務並返回"commit Ok"結果給客戶端。

注:能夠經過rpl_semi_sync_master_wait_for_slave_count參數來控制主庫接收多少個從庫寫事務成功反饋,才返回成功給客戶端。生產環境中使用半同步複製,當從庫出現故障,等待超時的時間又很長,致使主庫沒法接收從庫信息而沒法正常寫入時,能夠經過該參數踢出故障從庫。

 

在after_sync模式下,即便主庫宕機,全部在主庫上已經提交的事務都能保證已經同步到從庫的中繼日誌中,不會丟失任何數據。

半同步複製的搭建環境很簡單,它是基於異步複製的基礎上,安裝不一樣步複製插件就能夠了。

主庫上操做:

 1 root@db 02:23:  [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so'
 2     -> ;
 3 Query OK, 0 rows affected (0.03 sec)
 4 
 5 root@db 02:24:  [(none)]> set global rpl_semi_sync_master_enabled=on;
 6 Query OK, 0 rows affected (0.00 sec)
 7 
 8 root@db 02:24:  [(none)]> show variables like '%semi%';
 9 +-------------------------------------------+------------+
10 | Variable_name                             | Value      |
11 +-------------------------------------------+------------+
12 | rpl_semi_sync_master_enabled              | ON         |
13 | rpl_semi_sync_master_timeout              | 10000      |
14 | rpl_semi_sync_master_trace_level          | 32         |
15 | rpl_semi_sync_master_wait_for_slave_count | 1          |
16 | rpl_semi_sync_master_wait_no_slave        | ON         |
17 | rpl_semi_sync_master_wait_point           | AFTER_SYNC |
18 +-------------------------------------------+------------+
19 6 rows in set (0.01 sec)
20 
21 root@db 02:24:  [(none)]> 

 

注:還有一個比較重要的參數就是rpl_semi_sync_master_timeout,單位是毫秒。它表示若是主庫等待從庫回覆消息的時間超過該值,就自動切換爲異步複製模式。建議不要取默認值10s,該值能夠調整得很大,禁止向異步複製切換來保證數據複製的安全性。MySQL5.7半同步複製默認的方式就是after_sync模式。

 1 root@db 02:24:  [(none)]> show plugins;
 2 +----------------------------+----------+--------------------+--------------------+---------+
 3 | Name                       | Status   | Type               | Library            | License |
 4 +----------------------------+----------+--------------------+--------------------+---------+
 5 | binlog                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
 6 | mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL               | GPL     |
 7 | sha256_password            | ACTIVE   | AUTHENTICATION     | NULL               | GPL     |
 8 | InnoDB                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
 9 | INNODB_TRX                 | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
10 | INNODB_LOCKS               | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
11 | INNODB_LOCK_WAITS          | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
12 | INNODB_CMP                 | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
13 | INNODB_CMP_RESET           | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
14 | INNODB_CMPMEM              | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
15 | INNODB_CMPMEM_RESET        | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
16 | INNODB_CMP_PER_INDEX       | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
17 | INNODB_CMP_PER_INDEX_RESET | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
18 | INNODB_BUFFER_PAGE         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
19 | INNODB_BUFFER_PAGE_LRU     | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
20 | INNODB_BUFFER_POOL_STATS   | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
21 | INNODB_TEMP_TABLE_INFO     | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
22 | INNODB_METRICS             | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
23 | INNODB_FT_DEFAULT_STOPWORD | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
24 | INNODB_FT_DELETED          | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
25 | INNODB_FT_BEING_DELETED    | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
26 | INNODB_FT_CONFIG           | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
27 | INNODB_FT_INDEX_CACHE      | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
28 | INNODB_FT_INDEX_TABLE      | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
29 | INNODB_SYS_TABLES          | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
30 | INNODB_SYS_TABLESTATS      | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
31 | INNODB_SYS_INDEXES         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
32 | INNODB_SYS_COLUMNS         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
33 | INNODB_SYS_FIELDS          | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
34 | INNODB_SYS_FOREIGN         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
35 | INNODB_SYS_FOREIGN_COLS    | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
36 | INNODB_SYS_TABLESPACES     | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
37 | INNODB_SYS_DATAFILES       | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
38 | INNODB_SYS_VIRTUAL         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
39 | MyISAM                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
40 | MRG_MYISAM                 | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
41 | MEMORY                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
42 | CSV                        | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
43 | PERFORMANCE_SCHEMA         | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
44 | BLACKHOLE                  | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
45 | partition                  | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
46 | ARCHIVE                    | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
47 | FEDERATED                  | DISABLED | STORAGE ENGINE     | NULL               | GPL     |
48 | ngram                      | ACTIVE   | FTPARSER           | NULL               | GPL     |
49 | rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL     |
50 +----------------------------+----------+--------------------+--------------------+---------+
51 45 rows in set (0.00 sec)
52 
53 root@db 02:28:  [(none)]> 

 

而後再在從庫中安裝半同步複製插件和開啓半同步複製功能;

root@db 02:38: [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)

root@db 02:39: [(none)]> set global rpl_semi_sync_slave_enabled=on;
Query OK, 0 rows affected (0.00 sec)

root@db 02:39: [(none)]> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.01 sec)

root@db 02:40: [(none)]>

 

經過show plugins確認插件已經加載成功

 1 root@db 02:40:  [(none)]> show plugins;
 2 +----------------------------+----------+--------------------+-------------------+---------+
 3 | Name                       | Status   | Type               | Library           | License |
 4 +----------------------------+----------+--------------------+-------------------+---------+
 5 | binlog                     | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
 6 | mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL              | GPL     |
 7 | sha256_password            | ACTIVE   | AUTHENTICATION     | NULL              | GPL     |
 8 | InnoDB                     | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
 9 | INNODB_TRX                 | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
10 | INNODB_LOCKS               | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
11 | INNODB_LOCK_WAITS          | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
12 | INNODB_CMP                 | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
13 | INNODB_CMP_RESET           | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
14 | INNODB_CMPMEM              | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
15 | INNODB_CMPMEM_RESET        | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
16 | INNODB_CMP_PER_INDEX       | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
17 | INNODB_CMP_PER_INDEX_RESET | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
18 | INNODB_BUFFER_PAGE         | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
19 | INNODB_BUFFER_PAGE_LRU     | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
20 | INNODB_BUFFER_POOL_STATS   | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
21 | INNODB_TEMP_TABLE_INFO     | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
22 | INNODB_METRICS             | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
23 | INNODB_FT_DEFAULT_STOPWORD | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
24 | INNODB_FT_DELETED          | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
25 | INNODB_FT_BEING_DELETED    | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
26 | INNODB_FT_CONFIG           | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
27 | INNODB_FT_INDEX_CACHE      | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
28 | INNODB_FT_INDEX_TABLE      | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
29 | INNODB_SYS_TABLES          | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
30 | INNODB_SYS_TABLESTATS      | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
31 | INNODB_SYS_INDEXES         | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
32 | INNODB_SYS_COLUMNS         | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
33 | INNODB_SYS_FIELDS          | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
34 | INNODB_SYS_FOREIGN         | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
35 | INNODB_SYS_FOREIGN_COLS    | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
36 | INNODB_SYS_TABLESPACES     | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
37 | INNODB_SYS_DATAFILES       | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
38 | INNODB_SYS_VIRTUAL         | ACTIVE   | INFORMATION SCHEMA | NULL              | GPL     |
39 | MyISAM                     | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
40 | MRG_MYISAM                 | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
41 | MEMORY                     | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
42 | CSV                        | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
43 | PERFORMANCE_SCHEMA         | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
44 | BLACKHOLE                  | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
45 | partition                  | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
46 | ARCHIVE                    | ACTIVE   | STORAGE ENGINE     | NULL              | GPL     |
47 | FEDERATED                  | DISABLED | STORAGE ENGINE     | NULL              | GPL     |
48 | ngram                      | ACTIVE   | FTPARSER           | NULL              | GPL     |
49 | rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL     |
50 +----------------------------+----------+--------------------+-------------------+---------+
51 45 rows in set (0.00 sec)
52 
53 root@db 02:41:  [(none)]> 

 

注:爲了之後能夠開機自啓動半同步複製功能,咱們能夠把rpl_semi_sync_slave_enabled=on、rpl_semi_sync_master_enabled=on兩個參數加載到my.cnf配置文件中

由於之前已經開始了異步複製模式,如今只須要加載半同步複製的插件和啓動半同步複製功能便可,因此接下來重啓I/O threads,激活半同步複製。

1 root@db 02:41:  [(none)]> stop slave io_thread;
2 Query OK, 0 rows affected (0.00 sec)
3 
4 root@db 02:44:  [(none)]> start slave io_thread;
5 Query OK, 0 rows affected (0.01 sec)
6 
7 root@db 02:44:  [(none)]> 

主庫上查看半同步複製是否正常運行:

 1 root@db 02:37:  [(none)]> show global status like '%semi%';
 2 +--------------------------------------------+-------+
 3 | Variable_name                              | Value |
 4 +--------------------------------------------+-------+
 5 | Rpl_semi_sync_master_clients | 1     |
 6 | Rpl_semi_sync_master_net_avg_wait_time     | 0     |
 7 | Rpl_semi_sync_master_net_wait_time         | 0     |
 8 | Rpl_semi_sync_master_net_waits             | 0     |
 9 | Rpl_semi_sync_master_no_times              | 0     |
10 | Rpl_semi_sync_master_no_tx                 | 0     |
11 | Rpl_semi_sync_master_status | ON    |
12 | Rpl_semi_sync_master_timefunc_failures     | 0     |
13 | Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
14 | Rpl_semi_sync_master_tx_wait_time          | 0     |
15 | Rpl_semi_sync_master_tx_waits              | 0     |
16 | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
17 | Rpl_semi_sync_master_wait_sessions         | 0     |
18 | Rpl_semi_sync_master_yes_tx                | 0     |
19 +--------------------------------------------+-------+
20 14 rows in set (0.04 sec)
21 
22 root@db 02:46:  [(none)]>

 

Rpl_semi_sync_master_clients參數表明已經有一個從庫鏈接到了主庫,而且是半同步複製方式。

Rpl_semi_sync_master_status參數是ON(開啓)狀態,表明已是半同步複製模式了。

Rpl_semi_sync_master_no_tx:表明沒有成功接收slave提交的數據

Rpl_semi_sync_master_yes_tx:表明成功接收slave事務回覆的次數

 

在從庫上查看半同步複製狀態:

root@db 02:44:  [(none)]> show global status like '%semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

root@db 02:50:  [(none)]> 

 

Rpl_semi_sync_slave_status參數等於on表明從庫也開啓了半同步複製模式,至此MySQL半同步複製搭建成功。

相關文章
相關標籤/搜索