Mongo集群备份及迁移

Mongo数据备份迁移

旧集群

备份数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ docker exec -it rs_mongos_server bash
$ mongo --host 172.22.140.200 --port 27017 -u qa -p 'CmccJt2020'
mongos> show dbs
admin 0.000GB
config 0.004GB
dangjian_prodb 0.188GB
jt_faq_prodb 0.033GB
jt_faq_testdb 0.523GB
largescreen_db 0.000GB
outbound 0.000GB

#备份各个库
mongodump --host 172.22.140.200 --port 27017 --username qa --password 'CmccJt2020' --authenticationDatabase admin --db dangjian_prodb --out /root/backup/dangjian_prodb --gzip
mongodump --host 172.22.140.200 --port 27017 --username qa --password 'CmccJt2020' --authenticationDatabase admin --db jt_faq_prodb --out /root/backup/jt_faq_prodb --gzip
mongodump --host 172.22.140.200 --port 27017 --username qa --password 'CmccJt2020' --authenticationDatabase admin --db jt_faq_testdb --out /root/backup/jt_faq_testdb --gzip

新集群

修改相关配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

# 修改启动文件里面的集群IP,替换成新集群
cat /apps/basedata/mongosvr/pro-mongo.yml
version: '3.7'
networks:
mongo-network:
external: false
services:
# 配置服务器configsvr
rs_config_server:
image: mongo:4.4
networks:
- mongo-network
container_name: rs_config_server
restart: always
ports:
- 27019:27019
command: --configsvr --replSet "rs_config_server" --keyFile "/data/mongodb/key.file" --bind_ip_all
volumes:
- ${PWD}/config/db:/data/db
- ${PWD}/config/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
environment:
- MONGO_INITDB_ROOT_USERNAME=qa
- MONGO_INITDB_ROOT_PASSWORD=CmccJt2020
- MONGO_INITDB_DATABASE=admin

# shard分片1
rs_shard_server_01:
image: mongo:4.4
networks:
- mongo-network
container_name: rs_shard_server_01
restart: always
ports:
- 27118:27018
command: --shardsvr --replSet "rs_shard_server_01" --keyFile "/data/mongodb/key.file" --bind_ip_all
volumes:
- ${PWD}/shard1/db:/data/db
- ${PWD}/shard1/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
environment:
- MONGO_INITDB_ROOT_USERNAME=qa
- MONGO_INITDB_ROOT_PASSWORD=CmccJt2020
- MONGO_INITDB_DATABASE=admin
depends_on:
- rs_config_server

# shard分片2
rs_shard_server_02:
image: mongo:4.4
networks:
- mongo-network
container_name: rs_shard_server_02
restart: always
ports:
- 27218:27018
command: --shardsvr --replSet "rs_shard_server_02" --keyFile "/data/mongodb/key.file" --bind_ip_all
volumes:
- ${PWD}/shard2/db:/data/db
- ${PWD}/shard2/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
environment:
- MONGO_INITDB_ROOT_USERNAME=qa
- MONGO_INITDB_ROOT_PASSWORD=CmccJt2020
- MONGO_INITDB_DATABASE=admin
depends_on:
- rs_config_server

# shard分片3
rs_shard_server_03:
image: mongo:4.4
networks:
- mongo-network
container_name: rs_shard_server_03
restart: always
ports:
- 27318:27018
command: --shardsvr --replSet "rs_shard_server_03" --keyFile "/data/mongodb/key.file" --bind_ip_all
volumes:
- ${PWD}/shard3/db:/data/db
- ${PWD}/shard3/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
environment:
- MONGO_INITDB_ROOT_USERNAME=qa
- MONGO_INITDB_ROOT_PASSWORD=CmccJt2020
- MONGO_INITDB_DATABASE=admin
depends_on:
- rs_config_server

# 配置路由 mongos
rs_mongos_server:
image: mongo:4.4
networks:
- mongo-network
container_name: rs_mongos_server
restart: always
ports:
- 27017:27017
entrypoint: mongos
command: --configdb rs_config_server/10.232.15.148:27019,10.232.15.153:27019,10.232.15.147:27019 --keyFile "/data/mongodb/key.file" --bind_ip_all
volumes:
- ${PWD}/mongos/db:/data/db
- ${PWD}/mongos/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
environment:
- MONGO_INITDB_ROOT_USERNAME=qa
- MONGO_INITDB_ROOT_PASSWORD=CmccJt2020
- MONGO_INITDB_DATABASE=admin
depends_on:
- rs_shard_server_01
- rs_shard_server_02
- rs_shard_server_03

#创建key.file文件,并授予正确权限,每个节点启动目录都要有
cat > "${PWD}/key.file" <<'EOF'
t2bTm8m5ShkerYnpiDrUUaqZYXoRajQ1jedf7fMkvJGljCLdq9hVclE+NBKiWdkv
MDd4u1yH1p/YIsziMoIpULFdS7bQz7ZiP8mranxFk+I8npYUuq3eOcue/jGyljZY
ZKMzbfPL7JvSgAbD/UY7Bhc1FJ1pchWkVcd/bT454eQ1Zm2b+HGDEisabHha9mxr
CrHE+gqk5DhbCgBfOM7pDge3MFHD2Ddwf1piomcwe5o2zxnKR/hNldOKhGOcVfRj
dr4Q/2Ez+jHTXM1LH6KrxMANcU5hnUAs+ev6mfkfzB1J16NiEDQK8HXL7TwrjH3r
GOl68RtpGa5GA9US3EF5EAraFuA25EH5INZ+kJC3VYS0/5WxS1zZoiI2VXQ988Ja
DCoO9G29sPgSSv7RQKh2sOmdTsfwyvBvdKaaIfVokDNasQuQuj9a8uTNZL+pMgh9
eWzq+7FxCwEAa9cGqoEtwHSB0ujPly2o9XviQheKVMUknR2ZoBNK9LoJNKFx56ho
CF2sAkk+icINW09j3DbLFjx6FbNxeTQvhuAt3xlf9E4P3j+kzXUNdVzO6T8GeTMm
MtC06/Y6n8a1tEwaFgBMbhj0Fek+4v2Qd5ILpKEda4Dp37MmJTZl2P39yGITeH1e
L4HxMUFZVcdjjE7nmF0oRaDkfjulN+kl5y+xjrNeyr+QNhaP6oqRmyUV9AZ06Vcy
EnTE7m02D11m2ZDnEq+OVELYtqcpjn98unk6/HlLpweneQtbw38/ROrs8Zvxiczv
9rnBQlFc6kYYttNQNVoZ6mQ/n3+0PK8XLhuI23Hmp3Zndsek6xXHgV1CoCT9oYVm
2x6kiZS3JqyutdQQVhaK+vVs6Cv/owouAURNcHf9xSEA3TijNQdvc8jznE6HiyHc
sINaLKZHvAB8YsezQMGniSRDGJ3febb0wtkHVbpUfaPdUJkJeQo+m7ZRG9zGoSlv
Rszvoq1VNbmkWVs+b4K2GkknZHwOPRRiNev/8HuOpVjbZv2N
EOF

chmod 600 "${PWD}/key.file" && chown 999:999 "${PWD}/key.file"

docker-compose -f pro-mongo.yml up -d

shard节点初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
docker exec -it rs_shard_server_01 bash
mongo --port 27018 -u qa -p CmccJt2020 --authenticationDatabase admin
> rs.status()
{
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
}
> rs.initiate({
... _id: "rs_shard_server_01",
... members: [
... { _id: 0, host: "10.232.15.148:27118" },
... { _id: 1, host: "10.232.15.153:27118" },
... { _id: 2, host: "10.232.15.147:27118" }
... ]
... })
{ "ok" : 1 }
rs_shard_server_01:SECONDARY>

docker exec -it rs_shard_server_02 bash
mongo --port 27018 -u qa -p CmccJt2020 --authenticationDatabase admin
> rs.status()
{
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
}
> rs.initiate({
... _id: "rs_shard_server_02",
... members: [
... { _id: 0, host: "10.232.15.148:27218" },
... { _id: 1, host: "10.232.15.153:27218" },
... { _id: 2, host: "10.232.15.147:27218" }
... ]
... })
{ "ok" : 1 }
rs_shard_server_02:SECONDARY>

docker exec -it rs_shard_server_03 bash
mongo --port 27018 -u qa -p CmccJt2020 --authenticationDatabase admin
{
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
}
> rs.initiate({
... _id: "rs_shard_server_03",
... members: [
... { _id: 0, host: "10.232.15.148:27318" },
... { _id: 1, host: "10.232.15.153:27318" },
... { _id: 2, host: "10.232.15.147:27318" }
... ]
... })
{ "ok" : 1 }
rs_shard_server_03:SECONDARY>

加入分片集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

$ docker exec -it rs_mongos_server bash
mongos> sh.addShard("rs_shard_server_01/10.232.15.148:27118,10.232.15.153:27118,10.232.15.147:27118")
{
"shardAdded" : "rs_shard_server_01",
"ok" : 1,
"operationTime" : Timestamp(1766630551, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1766630551, 3),
"signature" : {
"hash" : BinData(0,"uiDCIROMO9ZKiV3somyunSOafMQ="),
"keyId" : NumberLong("7587612589459243030")
}
}
}
mongos> sh.addShard("rs_shard_server_02/10.232.15.148:27218,10.232.15.153:27218,10.232.15.147:27218")
{
"shardAdded" : "rs_shard_server_02",
"ok" : 1,
"operationTime" : Timestamp(1766630555, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1766630555, 3),
"signature" : {
"hash" : BinData(0,"csAnXW5Bi/ZPTbHwEj5+p9bQap0="),
"keyId" : NumberLong("7587612589459243030")
}
}
}
mongos> sh.addShard("rs_shard_server_03/10.232.15.148:27318,10.232.15.153:27318,10.232.15.147:27318")
{
"shardAdded" : "rs_shard_server_03",
"ok" : 1,
"operationTime" : Timestamp(1766630560, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1766630560, 4),
"signature" : {
"hash" : BinData(0,"cha6zsYAqiA8EaOJ2m8uA2RLoM0="),
"keyId" : NumberLong("7587612589459243030")
}
}
}

恢复数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

$ docker exec -it rs_mongos_server bash
$ mongorestore -uqa -pCmccJt2020 --port 27017 --authenticationDatabase admin -d jt_faq_prodb /root/backup/jt_faq_prodb/jt_faq_prodb --gzip
...
2025-12-25T02:44:55.637+0000 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"botid_1_status_1", "v":2}, Key:primitive.D{primitive.E{Key:"botid", Value:1}, primitive.E{Key:"status", Value:1}}, PartialFilterExpression:primitive.D(nil)}
2025-12-25T02:44:56.109+0000 118713 document(s) restored successfully. 0 document(s) failed to restore

$ mongorestore -uqa -pCmccJt2020 --port 27017 --authenticationDatabase admin -d dangjian_prodb /root/backup/dangjian_prodb/dangjian_prodb --gzip
...
2025-12-25T02:46:30.083+0000 129291 document(s) restored successfully. 0 document(s) failed to restore.

$ mongorestore -uqa -pCmccJt2020 --port 27017 --authenticationDatabase admin -d jt_faq_testdb /root/backup/jt_faq_testdb/jt_faq_testdb --gzip
...
2025-12-25T02:49:15.210+0000 no indexes to restore for collection jt_faq_testdb.default_skill_geo
2025-12-25T02:49:23.924+0000 2055262 document(s) restored successfully. 0 document(s) failed to restore

检查数据一致性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ docker exec -it rs_mongos_server bash
$ mongo --host 10.232.15.148 --port 27017 -u qa -p CmccJt2020 --authenticationDatabase admin
mongos> show dbs
admin 0.000GB
config 0.003GB
dangjian_prodb 0.176GB
jt_faq_prodb 0.033GB
jt_faq_testdb 0.473GB
mongos> use dangjian_prodb
switched to db dangjian_prodb
mongos> show tables
dangjian_hotword
image_personal_data_test
ipa_task
kg_data
kg_mind_map
kg_search_history
news_vec
qa_downvote_reason
qa_like_statistic
qa_notice
search_hot_query
statistic_op
user_info
mongos> db.dangjian_hotword.find({ "hotword": "习近平总书记关于党的建设的重要思想" })
{ "_id" : ObjectId("660295a0421aa9862a4c1405"), "hotword" : "习近平总书记关于党的建设的重要思想", "hotnum" : 39360, "is_new" : false, "updatetime" : "2025-08-28", "is_hot" : false }
{ "_id" : ObjectId("66029604421aa98815551003"), "hotword" : "习近平总书记关于党的建设的重要思想", "updatetime" : "2024-03-26", "hotnum" : 37349, "is_new" : false, "is_hot" : false }


Mongo集群备份及迁移
http://example.com/2025/12/25/MongoDB Cluster Backup and Migration Guide/
作者
种田人
发布于
2025年12月25日
许可协议