mongodb副本集數據同步的踩坑


一、故事

最近隨着搞活動比較頻繁導致數據庫出現了波動,后端日志總是報數據庫連接和讀取的問題。由於我設置的是讀寫分離(偽的,通過設置副本集的讀取策略實現的,設置的db.getMongo().setReadPref(),共有如下幾個參數:

One of the following read preference modes: primaryprimaryPreferredsecondarysecondaryPreferred, or nearest.詳細的介紹可以進鏈接了解。我們采用的是secondaryPreferred,優先讀取second節點,只有從節點不可讀是才會去master節點讀取)。當然最好的讀寫分離需要使用shared分片集實現,我們也已經切換稱分片集了,這里主要是為了本篇博客將要介紹的知識點,所以會以副本集進行講解。繼續故事啊,看監控數據庫狀態沒有什么問題啊,查看副本集狀態的時候發現了點貓膩:rs.status(),發現有一個節點的syncingTo竟然不是master,而是另外的一個secondary節點,這就很奇怪了,其他的都是master的地址。都知道mongodb之間同步數據是通過oplog的回訪實現的,和mysql的binlog很像,於是帶着這個疑問就到官網查看一番。

 

二、官網游歷

直達車:https://docs.mongodb.com/manual/reference/method/js-replication/、https://docs.mongodb.com/manual/tutorial/manage-chained-replication/

先來看一下官網的描述:

Starting in version 2.0, MongoDB supports chained replication. A chained replication occurs when a secondarymember replicates from another secondary member instead of from the primary. This might be the case, for example, if a secondary selects its replication target based on ping time and if the closest member is another secondary.

Chained replication can reduce load on the primary. But chained replication can also result in increased replication lag, depending on the topology of the network.

You can use the settings.chainingAllowed setting in Replica Set Configuration to disable chained replication for situations where chained replication is causing lag.

從mongodb2.0開始支持鏈式復制,並且默認是開啟的,是根據second節點之間的ping time和網絡距離進行選擇那個second作為數據的同步節點,鏈式復制的優點:可以減少master的資源消耗,減少負載。缺點:節點之間同步數據本來就不可避免會有數據的延遲,執行鏈式復制的過程會使這個時間增大,該second節點的數據就會比其他的更落后於master,所以在讀取數據的時候就會有一些問題,比如讀取數據讀不到,后端服務就會拋錯,導致用戶能感知到,非常不好,當然可以通過降低數據延遲來緩解,保證節點見的網絡帶寬流暢、io等。

 

三、方案

了解了副本集之間的復制方式,接下來就開始着手解決這個數據延遲的問題,從官網來看有兩種方式:使用rs.syncFrom()設置同步源、禁用掉鏈式復制

1、rs.syncFrom

官網對於這個命令的介紹:

Provides a wrapper around the replSetSyncFrom, which allows administrators to temporarily override the default sync target for the current member. Specify the name of the member you want to replicate from in the form of [hostname]:[port].

Changed in version 3.2: MongoDB 3.2 replica set members with vote cannot sync from members with 0votes

>rs.syncFrom("myHost:27017");

2、禁用掉鏈式復制

Disable Chained Replication

To disable chained replication, set the settings.chainingAllowed field in Replica Set Configuration to false.

If chained replication is disabled, you still can use replSetSyncFrom to specify that a secondary replicates from another secondary. But that configuration will last only until the secondary recalculates which member to sync from.(禁用了鏈式復制以后,還是可以通過replSetSyncFrom指定復制源為second,但是必須要有投票權

You can use the following sequence of commands to set settings.chainingAllowed to false:

cfg = rs.config()

cfg.settings.chainingAllowed = false
rs.reconfig(cfg)

Re-enable Chained Replication

To re-enable chained replication, set settings.chainingAllowed to true. You can use the following sequence of commands:

cfg = rs.config()
cfg.settings.chainingAllowed = true
rs.reconfig(cfg)

 

四、一些思考

排查問題時使用的幾個命令:

mongostat

mongotop

rs.status()

rs.printSlaveReplicationInfo()  打印數據同步延遲信息

rs.printReplicationInfo()  打印oplog信息

具體遇到數據同步延遲的,需要具體分析當時的情況,不能盲目的修改。首先考慮節點服務器的負載情況和當時的網絡環境是否正常,有的時候可能是網絡環境導致的,排除這些原因后,再去考慮修改同步源。這樣要做好讀寫分離,否則master的壓力會非常大。如果master的壓力太大就要做一些處理,比如切換一下msater升級資源或使用replSetSyncFrom切換同步源到second上。

 

參考:

https://www.cnblogs.com/cuishuai/p/8034851.html

https://docs.mongodb.com/manual/tutorial/manage-chained-replication/

https://docs.mongodb.com/manual/reference/method/rs.syncFrom/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM