同步工具: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 |
|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|---------------------|