mongo数据迁移操作

30 阅读2分钟

一、准备

  1. 确定mongo版本

本次使用cpos的一次操作作为展示用例

  1. 工具:mongodumpmongorestore

在DBA跳板机上有,直接使用即可。

  1. DBA跳板机:172.25.243.96

  2. 本次操作将cpos的一套生产mongo集群数据同步为例

生产cpos的一个mongo集群(172.25.162.142)的一个集合operation_sellout_1 同步到测试环境的cpos mongo集群(172.20.61.25)。




二、操作

  1. 查看源mongo 集群信息

# 连接源mongo集群
mongo --port 27017 --authenticationDatabase admin -u admin -p 'admin'  --host 172.25.162.142

# 临时解决secondary节点不可读的问题
db.getMongo().setSlaveOk()

# 查询表的大小
db.getCollection("operation_sellout_1").dataSize()

# 备份
mongodump -h 172.25.162.142 -d menu -c operation_sellout_1 -o /data/data --authenticationDatabase admin -u admin -p 'admin'

2. ## 查看目的mongo集群信息

# 连接目的mongo集群
mongo --port 27017 --authenticationDatabase admin -u admin -p 'admin' --host 172.20.61.25



# 恢复,备份的文件放在 /data/mongodb/data/menu/operation_sellout_1.bson
mongorestore -h 172.20.61.25 -d cpos -c operation_sellout_1 /data/mongodb/data/menu/operation_sellout_1.bson --authenticationDatabase admin  -u admin -p 'admin'

3. ## 备份源集群中一个集合

mongodump -h 172.25.162.142 -d menu -c operation_sellout_1 -o /data/data --authenticationDatabase admin -u admin -p 'admin'

说明:

· -d 表示为库

· -o 表示备份保存的路径

· -c 表示需要备份的集合名

  1. 将备份恢复到目标mongo集群的cpos库下

# 开始恢复
mongorestore -h 172.20.61.25 -d cpos -c operation_sellout_1 /data/mongodb/data/menu/operation_sellout_1.bson --authenticationDatabase admin 
 -u admin -p 'admin'    
 

说明:

恢复的速度由表的大小决定。

  1. 等待完成

  2. 在目标mongo集群中检查该集合

[root@KSSYYVI01352 temp]# mongo --port 27017 --authenticationDatabase admin -u admin -p 'admin' --host 172.20.61.25                                                      

# 查询表的大小
db.getCollection("operation_sellout_1").dataSize()

  1. 完成




三、注意

  1. 目前mongo4.0.14版本的tar包安装后带有dump和store 两个工具,其他版本的未知。

  2. mongo只有主节点能写。

  3. 恢复的时候出现的报错信息:

[root@KSSYYVI01352 mongodb]# mongorestore -h 172.20.61.25 -d menu -c operation_sellout_1 /data/mongodb/data/menu --authenticationDatabase admin  -u admin -p 'admin'     
2023-11-02T11:39:48.696+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the f
uture; use --nsInclude instead                                                                                                                                           
2023-11-02T11:39:48.696+0800    checking for collection data in /data/mongodb/data/menu                                                                                  
2023-11-02T11:39:48.696+0800    Failed: error scanning filesystem: file /data/mongodb/data/menu is a directory, not a bson file  

说明:

指定恢复集合的时候,需要指定使用BSON文件。

  1. 耗时(10G的数据量)

· 备份时间 11:09 -- 11:16,耗时7分钟

· 恢复时间 ??? 忘记统计