您好,登錄后才能下訂單哦!
說到半同步復(fù)制,就得先說說復(fù)制的三種方法:
異步復(fù)制:
對于異步復(fù)制而言,Master主機(jī)將事件寫入到binlog日志后,并不保證所有的事件都已經(jīng)復(fù)制到slave主機(jī),因此如果Master和Slave之間有網(wǎng)絡(luò)延遲,就會造成暫時的數(shù)據(jù)不一致的現(xiàn)象;如果Master出故障,而數(shù)據(jù)還沒有復(fù)制過去,則會造成數(shù)據(jù)丟失;但也有好處,效率較其他兩種復(fù)制方式高
半同步復(fù)制:
對于半同步復(fù)制而言,Master主機(jī)將事件發(fā)送給Slave主機(jī)后會觸發(fā)一個等待,直到其中一個Slave節(jié)點(diǎn)(如果有多個Slave)返回數(shù)據(jù)復(fù)制成功的信息給Master,由此增強(qiáng)了數(shù)據(jù)的一致性,但是因?yàn)镸aster主機(jī)的確認(rèn)開銷,會損耗一部分的性能;另外,半同步復(fù)制除了不需要等待所有Slave主機(jī)確認(rèn)事件的接收外,半同步數(shù)據(jù)復(fù)制并不要求那些事件完全地執(zhí)行,因此,仍有可能看到在Slave主機(jī)上數(shù)據(jù)復(fù)制延遲的發(fā)生,如果因?yàn)榫W(wǎng)絡(luò)延遲等原因造成Slave遲遲沒有返回復(fù)制成功的信息,超過了Master設(shè)置的超時時長(rpl_semi_sync_master_timeout),半同步復(fù)制就降級為異步復(fù)制方式,而后繼續(xù)數(shù)據(jù)復(fù)制
同步復(fù)制:
對于同步復(fù)制而言,Master主機(jī)將事件發(fā)送給Slave主機(jī)后會觸發(fā)一個等待,直到所有Slave節(jié)點(diǎn)(如果有多個Slave)返回數(shù)據(jù)復(fù)制成功的信息給Master。這種復(fù)制方式最安全,但是同時,效率也是最差的
小結(jié):
以上三種復(fù)制方法,沒有絕對的優(yōu)劣之分,根據(jù)自己的業(yè)務(wù)效率需求和對數(shù)據(jù)一致性、安全性的要求,自行選擇
半同步復(fù)制插件的安裝:(筆者線上MySQL使用的是5.6.16-log版本)
1、首先確認(rèn)是否支持動態(tài)加載插件:(半同步復(fù)制是通過MySQL提供的插件來實(shí)現(xiàn)的)
Master、Slave主機(jī)主機(jī)都要確認(rèn)
mysql> show variables like 'have_dynamic_loading';
+----------------------+-------+
| Variable_name??????? | Value |
+----------------------+-------+
| have_dynamic_loading | YES?? |
+----------------------+-------+
2、安裝插件:
Master主機(jī):
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.08 sec)
查看插件列表時,最后一個就是剛安裝的半同步復(fù)制插件:
mysql> show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name?????????????????????? | Status?? | Type?????????????? | Library??????????? | License |
+----------------------------+----------+--------------------+--------------------+---------+
| rpl_semi_sync_master?????? | ACTIVE?? | REPLICATION??????? | semisync_master.so | GPL???? |
+----------------------------+----------+--------------------+--------------------+---------+
Slave主機(jī):
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.07 sec)
mysql> show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name?????????????????????? | Status?? | Type?????????????? | Library??????????? | License |
+----------------------------+----------+--------------------+--------------------+---------+
| rpl_semi_sync_slave?????? | ACTIVE?? | REPLICATION??????? | semisync_slave.so | GPL???? |
+----------------------------+----------+--------------------+--------------------+---------+
注意:如果安裝插件時,不能安裝成功,先檢查下MySQL插件目錄下,是否有對應(yīng)的插件存在
mysql> show variables like '%plugin%';
+---------------+-------------------------------+
| Variable_name | Value???????????????????????? |
+---------------+-------------------------------+
| plugin_dir??? | /usr/local/mysql//lib/plugin/ |
+---------------+-------------------------------+
1 row in set (0.00 sec)
3、開啟半同步復(fù)制功能:
Master主機(jī):
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'rpl_semi_sync_master_enabled';
+------------------------------+-------+
| Variable_name??????????????? | Value |
+------------------------------+-------+
| rpl_semi_sync_master_enabled | ON??? |
+------------------------------+-------+
1 row in set (0.00 sec)
查看Master錯誤日志顯示半同步復(fù)制信息:
cat master.err
2019-05-14 15:50:50 1465 [Note] Semi-sync replication initialized for transactions.
2019-05-14 15:50:50 1465 [Note] Semi-sync replication enabled on the master.
Slave主機(jī):
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.01 sec)
mysql>? show global variables like 'rpl_semi_sync_slave_enabled';
+-----------------------------+-------+
| Variable_name?????????????? | Value |
+-----------------------------+-------+
| rpl_semi_sync_slave_enabled | ON??? |
+-----------------------------+-------+
1 row in set (0.00 sec)
注意:
為了防止這些動態(tài)的半同步復(fù)制參數(shù)在服務(wù)器重啟后失效,需要寫入到配置文件中,使其永久生效:
Master主機(jī)配置:
[mysqld]
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
Slave主機(jī)配置:
[mysqld]
rpl_semi_sync_slave_enabled = 1
4、在Master主機(jī)通過MySQL狀態(tài)變量來監(jiān)控半同步復(fù)制情況
mysql> show global status like 'rpl%';
+--------------------------------------------+-------+
| Variable_name????????????????????????????? | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients?????????????? | 0???? |
| Rpl_semi_sync_master_net_avg_wait_time???? | 4803? |
| Rpl_semi_sync_master_net_wait_time???????? | 4803? |
| Rpl_semi_sync_master_net_waits???????????? | 1???? |
| 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????? | 1975? |
| Rpl_semi_sync_master_tx_wait_time????????? | 1975? |
| 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.00 sec)
參數(shù)含義,具體參考MySQL官網(wǎng)
注意:
如果你配置好了半同步,在Master變更了數(shù)據(jù),Slave也能夠正常的同步數(shù)據(jù),但是在監(jiān)控半同步復(fù)制情況時,發(fā)現(xiàn)Rpl_semi_sync_master_status值為OFF,但rpl_semi_sync_master_enabled為ON,說明之前依然是采用的異步復(fù)制方式,此時需要重啟Slave,才能使用半同步復(fù)制方式生效
查看半同步復(fù)制相關(guān)的系統(tǒng)變量:
mysql> show global variables like 'rpl_semi%';
+------------------------------------+-------+
| Variable_name????????????????????? | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled?????? | ON??? |
| rpl_semi_sync_master_timeout?????? | 10000 |? Master提交事件后,等待Slave的確認(rèn)信息超時后,切換到異步復(fù)制模式的時間
| rpl_semi_sync_master_trace_level?? | 32??? |
| rpl_semi_sync_master_wait_no_slave | ON??? |
+------------------------------------+-------+
4 rows in set (0.00 sec)
參數(shù)含義,具體參考MySQL官網(wǎng)
半同步數(shù)據(jù)復(fù)制可以在等待確認(rèn)超時發(fā)生的時候降級為異步復(fù)制:
1、在Slave上模擬網(wǎng)絡(luò)出現(xiàn)了問題
ifdown eth0(網(wǎng)絡(luò)關(guān)掉后,第三方客戶端,如:Xshell等會斷)
2.1在Maser上更新一條數(shù)據(jù):
mysql> update edusoho_e.t1 set xname='MySQL' where id=39;
Query OK, 1 row affected (10.04 sec)?? 因?yàn)镸aster需要等待Slave的確認(rèn)信息,所以需要10s,這也說明半同步復(fù)制是成功的
Rows matched: 1? Changed: 1? Warnings: 0
2.2查看Master錯誤日志,可以看到半同步復(fù)制已經(jīng)降級了(不過數(shù)據(jù)還是能夠復(fù)制到Slave的)
cat master.err
2019-05-14 15:59:39 1465 [Warning] Timeout waiting for reply of binlog (file: mysql-bin.000004, pos: 2860), semi-sync up to file mysql-bin.000004, position 2606.
2019-05-14 15:59:39 1465 [Note] Semi-sync replication switched OFF
記得將Slave的網(wǎng)卡開啟
ifup eth0
卸載半同步復(fù)制插件:
如果不再需要半同步復(fù)制方式,可以將半同步復(fù)制插件卸載掉:
mysql> uninstall plugin rpl_semi_sync_master; (Slave主機(jī)是rpl_semi_sync_slave)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。