mongosync 工具可以在两个集群之间持续同步数据,适用于异地机房灾备。
https://www.mongodb.com/zh-cn/docs/cluster-to-cluster-sync/current/
工作原理:
- Mongosync 首先对源数据库进行完整的初始复制。- 全量同步
- Mongosync 会持续监听源数据库的变更流(Change Stream)- 增量同步
- 如果同步过程中断,它可以从上次保存的点继续,而不需要重新开始。- 断点续传
部署与实施:
1.配置【副本集A】到【副本集B】的同步
# 后台启动
# nohup ./mongosync --verbosity ERROR \
--logPath /var/log/mongosync --port 27182 \
--cluster0 "mongodb://admin:123456@127.0.0.1:27017" \
--cluster1 "mongodb://admin:123456@127.0.0.1:27018" > mongosync.log 2>&1 &
注:写Primary主节点IP。
2.开启 Mongosync 同步复制
# curl localhost:27182/api/v1/start -X POST --data '{ "source": "cluster0", "destination": "cluster1" }'
{"success":true}
3.检查 Mongosync 的状态
# curl localhost:27182/api/v1/progress -XGET | jq '.'
{
"progress": {
"state": "RUNNING",
"canCommit": true,
"canWrite": false,
"info": "change event application",
"lagTimeSeconds": 10,
"collectionCopy": {
"estimatedTotalBytes": 243,
"estimatedCopiedBytes": 243
},
"directionMapping": {
"Source": "cluster0: 127.0.0.1:27017",
"Destination": "cluster1: 127.0.0.1:27018"
},
"totalEventsApplied": 0,
"mongosyncID": "coordinator",
"coordinatorID": "coordinator"
},
"success": true
}
"state"状态值是: "RUNNING",代表同步正常。
4.暂停 Mongosync
# curl localhost:27182/api/v1/pause -XPOST --data '{ "source": "cluster0", "destination": "cluster1" }'
5.恢复 Mongosync
# curl localhost:27182/api/v1/resume -XPOST --data '{ "source": "cluster0", "destination": "cluster1" }'