同步工具:mongo-shake
介紹:
MongoShake是一個以golang語言進行編寫的通用的平台型服務,通過讀取MongoDB集群的Oplog操作日志,對MongoDB的數據進行復制,后續通過操作日志實現特定需求。日志可以提供很多場景化的應用,為此,我們在設計時就考慮了把MongoShake做成通用的平台型服務。通過操作日志,我們提供日志數據訂閱消費PUB/SUB功能,可通過SDK、Kafka、MetaQ等方式靈活對接以適應不同場景(如日志訂閱、數據中心同步、Cache異步淘汰等)。集群數據同步是其中核心應用場景,通過抓取oplog后進行回放達到同步目的,實現災備和多活的業務場景。
詳情介紹:https://developer.aliyun.com/article/603329
本次同步環境:mongodb集群分片模式
環境
主機 | 角色 | 備注 |
---|---|---|
源:10.100.5.167(自建) | 分片一:monogd:20000,mongos:40000,config:30000,arbiter:50000 | |
源:10.100.5.168(自建) | 分片二:monogd:20001,mongos:40000,config:30000,arbiter:50000 | |
源:10.100.5.169(自建) | 分片三:monogd:20002,mongos:40000,config:30000,arbiter:50000 | |
目:10.7.3.134(pass服務) | 集群統一端口:27017 |
程序下載
https://github.com/alibaba/MongoShake/releases
登錄地址,選擇最新的版本進行下載
配置
配置文件在:mongo-shake-v2.4.10目錄下,示例配置為:collector.conf
本次配置
conf.version = 5 #不修改
id = mongoshake #不修改
master_quorum = false
full_sync.http_port = 9101 #全量監控端口
incr_sync.http_port = 9100 #增量監控端口
system_profile_port = 9200 #golang監控端口
log.level = debug #日志級別:info,error,debug
log.dir = #日志目錄,默認為mongo-shake目錄的logs
log.file = collector.log #生成的日志文件名稱
log.flush = false
sync_mode = incr # 同步方式,all:全量+增量,full:全量,incr:增量
mongo_urls = mongodb://jkpro:********@10.100.5.167:20000,10.100.5.168:20000;mongodb://jkpro:********@10.100.5.168:20001,10.100.5.169:20001;mongodb://jkpro:********@10.100.5.169:20002,10.100.5.167:20002 #源地址的mongod分片節點
mongo_cs_url = 10.100.5.167:30000,10.100.5.168:30000,10.100.5.169:30000 #源地址的config節點
mongo_s_url = mongodb://jkpro:********@10.100.5.167:40000,10.100.5.168:40000,10.100.5.169:40000 #源地址的mongos節點
tunnel = direct # 通道模式
tunnel.address = mongodb://mongouser:*********@10.7.3.134:27017 #目的的地址鏈接配置
tunnel.message = raw
mongo_connect_mode = secondaryPreferred
filter.namespace.black = # 同步數據的黑名單,配置庫名,用逗號隔開,不同步的數據庫,黑白名單只能配置一個
filter.namespace.white = location-platform-eye;location-platform-changcheng # 同步數據的白名單,配置庫名,用逗號隔開,只同步的數據庫
filter.pass.special.db =
filter.ddl_enable = false
checkpoint.storage.url =
checkpoint.storage.db = mongoshake # 增量同步的斷點續傳記錄庫名,默認在源集群中創建庫
checkpoint.storage.collection = ckpt_default
checkpoint.start_position = 2007-01-01T00:00:00Z #增量同步的時間校驗節點
transform.namespace =
full_sync.reader.collection_parallel = 6
full_sync.reader.write_document_parallel = 12
full_sync.reader.document_batch_size = 256
full_sync.reader.read_document_count = 0
full_sync.collection_exist_drop = false # 如果目的庫中存在和源相同的數據庫,是否刪除,false不刪除,true刪除
full_sync.create_index = none
full_sync.executor.insert_on_dup_update = true # 如果_id存在在目的庫,是否將insert語句修改為update語句。
full_sync.executor.filter.orphan_document = false
full_sync.executor.majority_enable = false
incr_sync.mongo_fetch_method = oplog
incr_sync.change_stream.watch_full_document = false
incr_sync.oplog.gids =
incr_sync.shard_key = collection
incr_sync.worker = 12 # 同步程序的資源配置,如果資源足夠,可以適當調整
incr_sync.worker.oplog_compressor = none
incr_sync.target_delay = 0
incr_sync.worker.batch_queue_size = 64
incr_sync.adaptive.batching_max_size = 1024
incr_sync.fetcher.buffer_capacity = 256
incr_sync.executor.upsert = true
incr_sync.executor.insert_on_dup_update = false
incr_sync.conflict_write_to = none
incr_sync.executor.majority_enable = false
incr_sync.change_stream.watch_full_document = false
注意事項:
-
在分片集群同步的時候,默認會創建分片和索引的信息,但是在實際操作過程中,發現,同步數據目的端必須先分片,和創建索引,否則同步會失敗,配置需要full_sync.collection_exist_drop = false
-
配置目的和源地址的時候,源地址需要是mongod的分片配置,同時設置的賬戶,要在mongod的分片中授權,即在命令行可以正常登陸,否則同步會失敗
-
源數據庫中,數據的時間戳最好是自動生成的,如果是傳入的,最好不要亂序,否則會出現增量同步失敗的情況,原因是時間戳混亂
-
啟動多個同步實例的時候,修改程序的端口不一致即可
監控相關
參考官網:https://github.com/alibaba/MongoShake/wiki/如何監控和管理MongoShake的運行狀態?
全量
curl -s http://127.0.0.1:9101/progress | python -m json.tool
可以看到全量同步的具體情況,每個庫每個表的同步進度
增量
curl -s http://127.0.0.1:9100/repl | python -m json.tool
可以看到每個庫每個表,每批次抓取多少數據,同步更新成功多少數據
增量還可以通過命令
./mongoshake-stat --port 8100
|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|
| log_size_avg | log_size_max | logs_get/sec | logs_repl/sec | logs_success/sec | lsn.time | lsn_ack.time | lsn_ckpt.time | now.time | replset | tps/sec |
|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|
| 284.00B | 3.21KB | none | none | none | 1970-01-01 08:00:00 | 1970-01-01 08:00:00 | 2020-09-15 10:51:48 | 2020-09-15 10:54:13 | mongocluster01 | none |
| 257.00B | 255.14KB | none | none | none | 2020-09-10 14:07:48 | 2020-09-10 14:07:48 | 2020-09-15 10:53:21 | 2020-09-15 10:54:13 | mongocluster02 | none |
| 295.00B | 255.17KB | none | none | none | 2020-09-10 20:19:07 | 2020-09-10 20:19:07 | 2020-09-15 10:54:04 | 2020-09-15 10:54:13 | mongocluster03 | none |
|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|
| 259.00B | 3.21KB | 35 | 0 | 0 | 1970-01-01 08:00:00 | 1970-01-01 08:00:00 | 2020-09-15 10:51:48 | 2020-09-15 10:54:14 | mongocluster01 | 0 |
| 252.00B | 255.14KB | 42 | 0 | 0 | 2020-09-10 14:07:48 | 2020-09-10 14:07:48 | 2020-09-15 10:53:21 | 2020-09-15 10:54:14 | mongocluster02 | 0 |
| 262.00B | 255.17KB | 41 | 0 | 0 | 2020-09-10 20:19:07 | 2020-09-10 20:19:07 | 2020-09-15 10:54:04 | 2020-09-15 10:54:14 | mongocluster03 | 0 |
|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|