實現 MongoDB Cluster-to-Cluster 即叢集同步的工具是:mongosync
詳情可參考如下官方文件:
https://www.mongodb.com/zh-cn/docs/cluster-to-cluster-sync/current/quickstart/
以上這個地址的文件一看就是機器翻譯的,可能有不恰當的地方,但基本可參考使用。
以下是本次在某專案地配置叢集同步的簡要步驟,可參考使用。
一、首先在目的叢集安裝同步工具mongosync
本次使用的是版本匹配的1.5.0版本:
https://fastdl.mongodb.org/tools/mongosync/mongosync-rhel80-x86_64-1.5.0.tgz
以下是我習慣使用的安裝指令碼:
KDR=/u01
cd ${KDR}
TGZ=mongosync-rhel80-x86_64-1.5.0
#cp_unzip_chown_ln:
cp /u01/nfs/MongoDB/${TGZ}.tgz .
tar zxvf ${TGZ}.tgz
chown -R mongod:mongod ${TGZ}
ln -s ${KDR}/${TGZ}/bin/* /usr/local/bin/
二、叢集規劃資訊配置
在/etc/hosts檔案追加host定義(mongodb25~33),方便用於MongoDB叢集同步指令碼呼叫[以下程式碼已做脫敏處理]
cat >> /etc/hosts << EOF
# 生產叢集
2409:xx:1a mongodb25
2409:xx:1b mongodb26
2409:xx:1c mongodb27
# 容災叢集
2409:xx:1d mongodb28
2409:xx:1e mongodb29
2409:xx:1f mongodb30
# 歷史資料叢集
2409:xx:20 mongodb31
2409:xx:21 mongodb32
2409:xx:22 mongodb33
EOF
三、mongosync 執行
LOGFILE=mongosync.liking.`date +%Y%m%d-%H:%M`.log
mongosync \
--logPath /var/log/mongosync \
--cluster0 mongodb://admin:passwd@mongodb28:20000,mongodb29:20000,mongodb30:20000/?authSource=admin \
--cluster1 mongodb://admin:passwd@mongodb31:20000,mongodb32:20000,mongodb33:20000/?authSource=admin \
--verbosity INFO > ${LOGFILE} 2>&1 &
啟用同步服務後,可以在日誌目錄/var/log/mongosync檢視日誌資訊。
四、POST 同步命令 start
■ 同步整個叢集【本次使用】
curl "http://localhost:27182/api/v1/start" -XPOST --data '{
"source": "cluster0",
"destination": "cluster1"
}'
■ 同步指定的資料庫
curl "http://localhost:27182/api/v1/start" -XPOST --data '{
"source": "cluster0",
"destination": "cluster1"
"includeNameSpaces": [
{
"database": "flowtest"
}
]
}'
■ 同步指定的集合
curl "http://localhost:27182/api/v1/start" -XPOST --data '{
"source": "cluster0",
"destination": "cluster1",
"includeNameSpaces": [
{
"database": "flowtest",
"collections": [ "activityConfiguration", "history_task", "processConfiguration", "processDataObj", "snapshotResource" ]
}
]
}'
■ 同步時的運維操作
curl "http://localhost:27182/api/v1/progress" -XGET
curl "http://localhost:27182/api/v1/pause" -XPOST --data '{}'
curl "http://localhost:27182/api/v1/resume" -XPOST --data '{}'
五、全量同步日誌1
本次全量同步的資料規模如下:
[mongos] testdb> show databases
admin 252.00 KiB
config 35.56 MiB
flowtest 624.74 GiB
同步開始階段的重要資訊如下:
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","uri":"/api/v1/start","method":"POST","body":"{\n \"source\": \"cluster0\",\n \"destination\": \"cluster1\"\n}","clientIP":"::1","traceID":"fec8eb4b-8123-4a33-a1a5-d6cf57b431ea","time":"2024-04-18T16:47:49.511390067+08:00","message":"received request"}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","source":"cluster0","destination":"cluster1","reversible":false,"enableUserWriteBlocking":false,"time":"2024-04-18T16:47:49.511783755+08:00","message":"Start handlercalled"}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","sourceURI":"mongodb28:20000,mongodb29:20000,mongodb30:20000","dstURI":"mongodb31:20000,mongodb32:20000,mongodb33:20000","time":"2024-04-18T16:47:49.511812312+08:00","message":"Creating clients and connecting to clusters."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","clusterType":"src","operation description":"Pinging deployment at mongodb://admin:<REDACTED>@mongodb28:20000,mongodb29:20000,mongodb30:20000/?authSource=admin to establish initial connection.","attemptNumber":0,"durationSoFarSecs":0,"durationLimitSecs":10,"time":"2024-04-18T16:47:49.512143259+08:00","message":"Trying operation."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","clusterType":"dst","operation description":"Pinging deployment at mongodb://admin:<REDACTED>@mongodb31:20000,mongodb32:20000,mongodb33:20000/?authSource=admin to establish initial connection.","attemptNumber":0,"durationSoFarSecs":0,"durationLimitSecs":10,"time":"2024-04-18T16:47:49.530184069+08:00","message":"Trying operation."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","time":"2024-04-18T16:47:49.545274189+08:00","message":"Successfully initialized source and destination clients"}
全量同步完畢階段的重要日誌資訊如下:
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"Collection Copy","time":"2024-04-18T20:29:55.31864523+08:00","message":"All Collection Copy components have stopped."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"Collection Copy","time":"2024-04-18T20:29:55.318660079+08:00","message":"Completed writing 6487 / 6487 partitions to destination cluster"}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"CHECKPOINT","totalBytes":2717579843528,"copiedBytes":2717920929608,"time":"2024-04-18T20:29:55.318684889+08:00","message":"Finished collection copy phase."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"CHECKPOINT","time":"2024-04-18T20:29:55.330386515+08:00","message":"Waiting for collection copy phase to complete."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"CHECKPOINT","time":"2024-04-18T20:29:55.351147335+08:00","message":"Running change event application phase."}
{"level":"info","serverID":"bb927128","mongosyncID":"coordinator","componentName":"Change Event Application","time":"2024-04-18T20:29:55.365007664+08:00","message":"Starting index checker service which runs a cycle every 60 seconds."}
以上日誌可見:
620G資料,同步耗時3.5小時左右,效能比之前測試稍慢。
再次印證了mongodb本身的壓縮演算法較好,實際fs層面的資料大小如下:
"totalBytes":2717579843528,"copiedBytes":2717920929608,"time":"2024-04-18T20:29:55.318684889+08:00","message":"Finished collection copy phase."
六、全量同步日誌2
當晚實際生產叢集全量同步的資料規模如下:
flowtest 684.20 GiB
{"estimatedTotalBytes":2972595055214,"estimatedCopiedBytes":2973012240862}
實際耗時2h55m,可見同樣硬體、網路環境夜間效能稍好。
七、思考
透過以上實踐,顯而易見,MongoDB叢集同步技術使得我們後續遷移資料庫變得輕而易舉,實際割接時間可控制在幾分鐘以內。