說(shuō)到半同步復(fù)制,就得先說(shuō)說(shuō)復(fù)制的三種方法:
成都服務(wù)器托管,創(chuàng)新互聯(lián)建站提供包括服務(wù)器租用、光華機(jī)房服務(wù)器托管、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、空間域名等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:18982081108
異步復(fù)制:
對(duì)于異步復(fù)制而言,Master主機(jī)將事件寫(xiě)入到binlog日志后,并不保證所有的事件都已經(jīng)復(fù)制到slave主機(jī),因此如果Master和Slave之間有網(wǎng)絡(luò)延遲,就會(huì)造成暫時(shí)的數(shù)據(jù)不一致的現(xiàn)象;如果Master出故障,而數(shù)據(jù)還沒(méi)有復(fù)制過(guò)去,則會(huì)造成數(shù)據(jù)丟失;但也有好處,效率較其他兩種復(fù)制方式高
半同步復(fù)制:
對(duì)于半同步復(fù)制而言,Master主機(jī)將事件發(fā)送給Slave主機(jī)后會(huì)觸發(fā)一個(gè)等待,直到其中一個(gè)Slave節(jié)點(diǎn)(如果有多個(gè)Slave)返回?cái)?shù)據(jù)復(fù)制成功的信息給Master,由此增強(qiáng)了數(shù)據(jù)的一致性,但是因?yàn)镸aster主機(jī)的確認(rèn)開(kāi)銷,會(huì)損耗一部分的性能;另外,半同步復(fù)制除了不需要等待所有Slave主機(jī)確認(rèn)事件的接收外,半同步數(shù)據(jù)復(fù)制并不要求那些事件完全地執(zhí)行,因此,仍有可能看到在Slave主機(jī)上數(shù)據(jù)復(fù)制延遲的發(fā)生,如果因?yàn)榫W(wǎng)絡(luò)延遲等原因造成Slave遲遲沒(méi)有返回復(fù)制成功的信息,超過(guò)了Master設(shè)置的超時(shí)時(shí)長(zhǎng)(rpl_semi_sync_master_timeout),半同步復(fù)制就降級(jí)為異步復(fù)制方式,而后繼續(xù)數(shù)據(jù)復(fù)制
同步復(fù)制:
對(duì)于同步復(fù)制而言,Master主機(jī)將事件發(fā)送給Slave主機(jī)后會(huì)觸發(fā)一個(gè)等待,直到所有Slave節(jié)點(diǎn)(如果有多個(gè)Slave)返回?cái)?shù)據(jù)復(fù)制成功的信息給Master。這種復(fù)制方式最安全,但是同時(shí),效率也是最差的
小結(jié):
以上三種復(fù)制方法,沒(méi)有絕對(duì)的優(yōu)劣之分,根據(jù)自己的業(yè)務(wù)效率需求和對(duì)數(shù)據(jù)一致性、安全性的要求,自行選擇
半同步復(fù)制插件的安裝:(筆者線上MySQL使用的是5.6.16-log版本)
1、首先確認(rèn)是否支持動(dòng)態(tài)加載插件:(半同步復(fù)制是通過(guò)MySQL提供的插件來(lái)實(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)
查看插件列表時(shí),最后一個(gè)就是剛安裝的半同步復(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???? |
+----------------------------+----------+--------------------+--------------------+---------+
注意:如果安裝插件時(shí),不能安裝成功,先檢查下MySQL插件目錄下,是否有對(duì)應(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、開(kāi)啟半同步復(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錯(cuò)誤日志顯示半同步復(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)
注意:
為了防止這些動(dòng)態(tài)的半同步復(fù)制參數(shù)在服務(wù)器重啟后失效,需要寫(xiě)入到配置文件中,使其永久生效:
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ī)通過(guò)MySQL狀態(tài)變量來(lá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ù)制情況時(shí),發(fā)現(xiàn)Rpl_semi_sync_master_status值為OFF,但rpl_semi_sync_master_enabled為ON,說(shuō)明之前依然是采用的異步復(fù)制方式,此時(shí)需要重啟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)信息超時(shí)后,切換到異步復(fù)制模式的時(shí)間
| 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)超時(shí)發(fā)生的時(shí)候降級(jí)為異步復(fù)制:
1、在Slave上模擬網(wǎng)絡(luò)出現(xiàn)了問(wèn)題
ifdown eth0(網(wǎng)絡(luò)關(guān)掉后,第三方客戶端,如:Xshell等會(huì)斷)
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,這也說(shuō)明半同步復(fù)制是成功的
Rows matched: 1? Changed: 1? Warnings: 0
2.2查看Master錯(cuò)誤日志,可以看到半同步復(fù)制已經(jīng)降級(jí)了(不過(guò)數(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)卡開(kāi)啟
ifup eth0
卸載半同步復(fù)制插件:
如果不再需要半同步復(fù)制方式,可以將半同步復(fù)制插件卸載掉:
mysql> uninstall plugin rpl_semi_sync_master; (Slave主機(jī)是rpl_semi_sync_slave)