溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL 5.7半同步復(fù)制after sync和after commit的示例分析

發(fā)布時(shí)間:2021-11-08 09:34:17 來(lái)源:億速云 閱讀:325 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章主要介紹了MySQL 5.7半同步復(fù)制after sync和after commit的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

如果你的生產(chǎn)庫(kù)開(kāi)啟了半同步復(fù)制,那么對(duì)數(shù)據(jù)的一致性會(huì)要求較高,但在MySQL5.5/5.6里,會(huì)存在數(shù)據(jù)不一致的風(fēng)險(xiǎn)。有這么一個(gè)場(chǎng)景,客戶端提交了一個(gè)事務(wù),master把binlog發(fā)送給slave,在發(fā)送的期間,網(wǎng)絡(luò)出現(xiàn)波動(dòng),此時(shí)Binlog Dump線程發(fā)送就會(huì)卡住,要等待slave把binlog寫(xiě)到本地的relay-log里,然后給master一個(gè)反饋,等待的時(shí)間以rpl_semi_sync_master_timeout參數(shù)為準(zhǔn),默認(rèn)為10秒。在這等待的10秒鐘里,在其他會(huì)話里,查看剛才的事務(wù)是可以看見(jiàn)的,此時(shí)一旦master發(fā)生宕機(jī),由于binlog沒(méi)有發(fā)送給slave,前端app切到slave查看,就會(huì)發(fā)現(xiàn)剛才已提交的事務(wù)不見(jiàn)了。


為了解決這種問(wèn)題,MySQL5.7 改善了半同步復(fù)制這個(gè)缺陷。通過(guò)rpl_semi_sync_master_wait_point這個(gè)參數(shù)加以控制,默認(rèn)是AFTER_SYNC,官方推薦用這個(gè),它的工作原理是:master把binlog發(fā)送給slave,只有在slave把binlog寫(xiě)到本地的relay-log里,才提交到存儲(chǔ)引擎層,然后把請(qǐng)求返回給客戶端,客戶端才可以看見(jiàn)剛才提交的事務(wù)。如果slave未保存到本地的relay-log里,客戶端是看不見(jiàn)剛才的事務(wù)的,這樣就不會(huì)造成上述那個(gè)場(chǎng)景發(fā)生。另一個(gè)值是AFTER_COMMIT,這個(gè)值是采用老式的MySQL5.5/5.6半同步復(fù)制工作。


AFTER_SYNC (the default): The master writes each transaction to its binary log and the slave, and syncs the binary log to disk. The master waits for slave acknowledgment of transaction receipt after the sync. Upon receiving acknowledgment, the master commits the transaction to the storage engine and returns a result to the client, which then can proceed.
主庫(kù)把每一個(gè)事務(wù)寫(xiě)到二進(jìn)制日志并保存磁盤上,且發(fā)送給從庫(kù)。主庫(kù)在等待從庫(kù)寫(xiě)到自己的relay-log里確認(rèn)信息。在接到確認(rèn)信息后,主數(shù)據(jù)庫(kù)把事務(wù)寫(xiě)到存儲(chǔ)引擎里并把相應(yīng)結(jié)果反饋給客戶端,客戶端將在那時(shí)進(jìn)行處理。


AFTER_COMMIT: The master writes each transaction to its binary log and the slave, syncs the binary log, and commits the transaction to the storage engine. The master waits for slave acknowledgment of transaction receipt after the commit. Upon receiving acknowledgment, the master returns a result to the client, which then can proceed. 
主庫(kù)把每一個(gè)事務(wù)寫(xiě)到二進(jìn)制日志并保存磁盤上,且發(fā)送給從庫(kù),并把事務(wù)寫(xiě)到存儲(chǔ)引擎里。主庫(kù)在等待從庫(kù)寫(xiě)到自己的relay-log里確認(rèn)信息。在接到確認(rèn)信息后,主庫(kù)把相應(yīng)結(jié)果反饋給客戶端,客戶端將在那時(shí)進(jìn)行處理。


The replication characteristics of these settings differ as follows:
這兩個(gè)參數(shù)不同之處在于:


With AFTER_SYNC, all clients see the committed transaction at the same time: After it has been acknowledged by the slave and committed to the storage engine on the master.。Thus, all clients see the same data on the master.
在設(shè)置為AFTER_SYNC參數(shù),所有的客戶端可以同時(shí)看到提交的數(shù)據(jù):在得到從庫(kù)寫(xiě)到自己的relay-log里的確認(rèn)信息后,并把事務(wù)寫(xiě)到存儲(chǔ)引擎里。這樣,所有的客戶端都可以在主庫(kù)上看到同樣的數(shù)據(jù)。


In the event of master failure, all transactions committed on the master have been replicated to the slave (saved to its relay log). A crash of the master and failover to the slave is lossless because the slave is up to date.
主庫(kù)報(bào)錯(cuò),所有已經(jīng)寫(xiě)到從庫(kù)的事務(wù)都已經(jīng)保存到了relay log里。主庫(kù)的崩潰,HA切換到從庫(kù),不會(huì)帶來(lái)任何損失,因?yàn)閺膸?kù)的relay-log的數(shù)據(jù)是最新的。


With AFTER_COMMIT, the client issuing the transaction gets a return status only after the server commits to the storage engine and receives slave acknowledgment. After the commit and before slave acknowledgment, other clients can see the committed transaction before the committing client.
在設(shè)置為AFTER_COMMIT 參數(shù),發(fā)起事務(wù)的客戶端僅在服務(wù)器向存儲(chǔ)引擎寫(xiě)入數(shù)據(jù)并接受從庫(kù)得到確認(rèn)之后才返回狀態(tài)。在寫(xiě)入數(shù)據(jù)后和得到從庫(kù)確認(rèn)之前,其他的客戶端可以看到在這一事務(wù)。


If something goes wrong such that the slave does not process the transaction, then in the event of a master crash and failover to the slave, it is possible that such clients will see a loss of data relative to what they saw on the master. 
如果出現(xiàn)了某種錯(cuò)誤,比如說(shuō)從庫(kù)的sql_thread線程沒(méi)有執(zhí)行,那么主庫(kù)崩潰和故障轉(zhuǎn)移給從服務(wù)器的前提下,有可能這個(gè)客戶端會(huì)丟失那些他們?cè)?jīng)在主庫(kù)上看到的信息。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“MySQL 5.7半同步復(fù)制after sync和after commit的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI