260210-MongoDBHA-MongoDB集群-重置绑定关系-(Replica Set)副本集集群搭建

这是一个涉及数据库运维的操作,需要谨慎处理。

MongoDB 集群配置删除与重新绑定

1. 理解 MongoDB 集群配置存储位置

MongoDB 集群配置主要存储在 Config Servers 中:

  • Config Database (plain复制 config): 包含分片集群的元数据
  • Local Database: 每个节点存储的副本集配置

2. 删除集群配置的方法

场景 A: 完全重置分片集群

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
# 1. 停止所有 mongos 路由进程
killall mongos # 或使用 systemctl stop mongos

# 2. 停止所有分片 mongod 实例
# 在每个分片节点上执行
killall mongod

# 3. 停止 Config Server 副本集
# 在所有 config server 节点上执行
killall mongod

# 4. 清除 Config Server 数据目录(⚠️ 危险操作)
# 假设数据目录为 /data/configdb
rm -rf /data/configdb/*

# 5. 重新初始化 Config Server 副本集
mongod --configsvr --replSet configRS --dbpath /data/configdb --port 27019

# 连接主节点初始化
rs.initiate({
_id: "configRS",
configsvr: true,
members: [
{ _id: 0, host: "cfg1.example.com:27019" },
{ _id: 1, host: "cfg2.example.com:27019" },
{ _id: 2, host: "cfg3.example.com:27019" }
]
})

场景 B: 仅移除分片(保留集群框架)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 连接到 mongos
mongo--
host
mongos.example.com--
port
27017

// 1. 查看当前分片
sh.status()

// 2. 移除分片(会迁移数据)
use
admin
db.runCommand({removeShard: "shard0000"})

// 3. 检查移除进度,直到状态为 "completed"
db.runCommand({removeShard: "shard0000"})

// 4. 如果需要强制移除(数据已备份)
db.runCommand({removeShard: "shard0000", drop: true})

场景 C: 重置副本集配置(非分片集群)

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
// 连接到副本集成员
mongo--
host
mongod.example.com--
port
27017

// 1. 查看当前配置
rs.conf()

// 2. 强制重新配置(适用于节点变更)
rs.reconfig({
_id: "myReplicaSet",
members: [
{_id: 0, host: "newhost1:27017"},
{_id: 1, host: "newhost2:27017"},
{_id: 2, host: "newhost3:27017"}
]
}, {force: true}) // force: true 用于强制重新配置

// 3. 或者完全重置(先删除 local 数据库的配置)
use
local
db.system.replset.remove({}) // ⚠️ 仅在确定要重置时执行
// 然后重启 mongod,重新执行 rs.initiate()

3. 重新绑定新配置

步骤 1: 重新配置 Config Server

1
2
3
4
5
6
7
8
9
10
// 初始化新的 Config Server 副本集
rs.initiate({
_id: "configRS",
configsvr: true,
members: [
{_id: 0, host: "new-cfg1:27019", priority: 2},
{_id: 1, host: "new-cfg2:27019"},
{_id: 2, host: "new-cfg3:27019", arbiterOnly: true}
]
})

步骤 2: 启动 mongos 并配置分片

1
2
# 启动 mongos,指向新的 config server
mongos --configdb configRS/new-cfg1:27019,new-cfg2:27019,new-cfg3:27019 --port 27017
1
2
3
4
5
6
7
8
9
// 连接到 mongos 添加分片
sh.addShard("shardRS1/shard1-node1:27018,shard1-node2:27018")
sh.addShard("shardRS2/shard2-node1:27018,shard2-node2:27018")

// 启用数据库分片
sh.enableSharding("myDatabase")

// 对集合进行分片
sh.shardCollection("myDatabase.myCollection", {shardKey: 1})

4. 关键注意事项

操作 风险等级 说明
删除 Config Server 数据 🔴 极高 会丢失所有分片元数据
强制 rs.reconfig 🟡 中等 可能导致脑裂
removeShard 🟢 较低 数据会自动迁移
停止 mongos 🟢 低 仅影响路由,不丢数据

5. 安全操作建议

1
2
3
4
# 操作前务必备份
mongodump --host configRS/cfg1:27019 --db config --out /backup/config_$(date +%Y%m%d)

# 或者使用文件系统快照(如果使用 WiredTiger)

6. 常见问题排查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 检查 Config Server 状态
use
admin
db.runCommand({getShardMap: 1})

// 查看分片连接状态
sh.status()

// 检查 balancer 状态
sh.getBalancerState()

// 如果配置不一致,强制刷新
use
admin
db.adminCommand({flushRouterConfig: 1})

⚠️ 重要提醒

  • 在生产环境执行前,务必在测试环境验证
  • 确保有完整的数据备份
  • 操作期间会有服务中断,选择维护窗口执行
  • 如果是云托管 MongoDB (Atlas),请使用提供商的控制台操作,不要手动修改