天天看點

MySQL半同步複制

主從複制存在三種類型:異步複制、同步複制以及半同步複制,下面根據手冊上解釋逐一說明一下。

異步複制:

主庫将更新的事件寫入binlog,準備好的從庫擷取這些binlong進行回放。這無法保證所有從庫都接到這些事件。

<code>With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.</code>

同步複制:

主庫送出一個事務,在傳回結果給用戶端前,需要等待所有從庫都送出了這個事務。缺點就是主庫在完成一個事務前存在很多延遲

<code>With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.</code>

半同步複制:

異步複制和同步複制之間的折中。主庫在commit的時候,隻需要等待至少一個從庫确認收到主庫更新,并将其記錄到relay-log中。

不需要等待所有從庫都确認,隻需要一個就可以。也不需要這些更新的事件在從庫上完成執行,隻需要記錄到relay-log中(重新整理到磁盤上)

<code>Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits after commit only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.</code>

5.1 插件式安裝

安裝前提:

1、隻适合在MySQL5.5或者更高的版本中安裝

2、MySQL的server必須有自動加載功能,通過參數have_dynamic_loading變量進行檢查

3、主從的異步複制已經正常工作

關于<code>plugin_name</code>和<code>shared_library_name</code>對應關系可以參照

5.2 主庫

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>mysql&gt; INSTALL PLUGIN rpl_semi_sync_master SONAME</code><code>‘semisync_master.so‘</code><code>;</code>

<code>mysql&gt; show variables</code><code>like</code>

<code>‘%rpl%‘</code><code>;</code>

<code>+</code><code>------------------------------------+----------+</code>

<code>| Variable_name                      | Value    |</code>

<code>| rpl_semi_sync_master_enabled       |</code><code>ON</code>      

<code>|</code>

<code>| rpl_semi_sync_master_timeout       | 10000    |</code>

<code>| rpl_semi_sync_master_trace_level   | 32       |</code>

<code>| rpl_semi_sync_master_wait_no_slave |</code><code>OFF</code>     

<code>| rpl_stop_slave_timeout             | 31536000 |</code>

 修改主庫配置檔案:

<code>[mysqld]</code>

<code>rpl_semi_sync_master_enabled=1</code>

<code>rpl_semi_sync_master_timeout=1000 # 1 second</code>

<code>sync_log = 1</code>

5.3 從庫

<code>mysql&gt; INSTALL PLUGIN rpl_semi_sync_slave SONAME</code><code>‘semisync_slave.so‘</code><code>;</code>

<code>mysql&gt; show slave status\G</code>

修改從庫的配置檔案:

<code>rpl_semi_sync_slave_enabled = 1</code>

盡管主從之間的server-id不同,但是在開啟同步出現server-uuid沖突問題,主要原因在于從庫的mysql是直接從主庫上拷貝過來的(包括二進制以及資料等所有檔案),在data目錄下存在auto.cnf檔案:

<code>[root@typhoeus81 ice_test_2 data]# pwd</code>

<code>/data1/guosong/mysql5616/data</code>

<code>[root@typhoeus81 ice_test_2 data]# more auto.cnf</code>

<code>[auto]</code>

<code>server-uuid=b9d59578-df02-11e3-b112-001e4f1f39cf</code>

 手動将該檔案清理後,重新開機mysql會根據server-id重新産生一個檔案

5.4 相關配置以及參數

主要配置參數

<code>*rpl_semi_sync_master_enabled</code>

<code>主庫是否開啟半同步配置</code>

<code>*rpl_semi_sync_master_timeout</code>

<code>主庫等待從庫的确認逾時時間,預設是10s,逾時之後改為異步模式</code>

<code>*rpl_semi_sync_slave_enabled</code>

<code>從庫是否開啟半同步配置</code>

可以用于監控半同步狀态的主要狀态參數:

<code>*Rpl_semi_sync_master_clients</code>

<code>設定半同步從庫的個數</code>

<code>*Rpl_semi_sync_master_status</code>

<code>主庫是否開啟半同步的标志</code>

<code>*Rpl_semi_sync_master_no_tx</code>

<code>從庫未成功确認的commits個數</code>

<code>*Rpl_semi_sync_master_yes_tx</code>

<code>從庫成功确認的commits個數</code>

<code>*Rpl_semi_sync_slave_status</code>

<code>從庫是否開啟半同步的标志</code>

6.1 逾時機制,半同步自動切換為異步

測試:

和參數<code>rpl_semi_sync_master_timeout有關,</code>預設值為10s太長,文檔中建議修改為1s

6.2