MongoDB Cluster
一、硬件与系统要求
1.1 硬件要求
- CPU:至少 4 核
- 内存:至少 8GB
- 磁盘:至少 100GB 可用空间
- 网络:1Gbps 以上
1.2 系统要求
- Linux:RHEL 7/8/9,Ubuntu 18.04/20.04/22.04
- Windows:Windows Server 2016/2019/2022
1.3 节点规划
本指南将创建一个包含 3 个节点的 MongoDB 复制集(Replica Set):
更多学习教程公众号风哥教程itpux_com from:www.itpux.com 学习交流加群风哥QQ113257174
- 节点 1:192.168.1.10(主节点)
- 节点 2:192.168.1.11(从节点)
- 节点 3:192.168.1.12(从节点)
二、安装 MongoDB
在所有节点上安装 MongoDB,参考 MongoDB 安装指南。
学习交流加群风哥QQ113257174 更多学习教程公众号风哥教程itpux_com
三、配置复制集
3.1 修改配置文件
在所有节点上修改 MongoDB 配置文件:
from:www.itpux.com 更多学习教程公众号风哥教程itpux_com
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: "rs0"
3.2 重启服务
sudo systemctl restart mongod
3.3 初始化复制集
在主节点上执行:
学习交流加群风哥微信: itpux-com 更多学习教程公众号风哥教程itpux_com 学习交流加群风哥微信: itpux-com
mongo
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "192.168.1.10:27017" },
{ _id: 1, host: "192.168.1.11:27017" },
{ _id: 2, host: "192.168.1.12:27017" }
]
})
3.4 查看复制集状态
rs.status()
四、MongoDB Sharded Cluster(分片集群)
4.1 节点规划
- 配置服务器(3 个):192.168.1.20, 192.168.1.21, 192.168.1.22
- 分片 1(3 个节点):192.168.1.30, 192.168.1.31, 192.168.1.32
- 分片 2(3 个节点):192.168.1.40, 192.168.1.41, 192.168.1.42
- Mongos 路由(2 个):192.168.1.50, 192.168.1.51
4.2 配置服务器
4.2.1 修改配置文件
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/config.log
storage:
dbPath: /var/lib/mongodb/config
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/config.pid
net:
port: 27019
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: "configRS"
4.2.2 启动配置服务器
mongod --config /etc/mongod-config.conf
4.2.3 初始化配置服务器复制集
mongo --port 27019
rs.initiate({
_id: "configRS",
configsvr: true,
members: [
{ _id: 0, host: "192.168.1.20:27019" },
{ _id: 1, host: "192.168.1.21:27019" },
{ _id: 2, host: "192.168.1.22:27019" }
]
})
4.3 分片服务器
4.3.1 修改配置文件(分片 1)
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/shard1.log
storage:
dbPath: /var/lib/mongodb/shard1
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/shard1.pid
net:
port: 27018
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: "shard1RS"
4.3.2 启动分片服务器
mongod --config /etc/mongod-shard1.conf
4.3.3 初始化分片复制集
mongo --port 27018
rs.initiate({
_id: "shard1RS",
members: [
{ _id: 0, host: "192.168.1.30:27018" },
{ _id: 1, host: "192.168.1.31:27018" },
{ _id: 2, host: "192.168.1.32:27018" }
]
})
4.4 配置 Mongos 路由
4.4.1 启动 Mongos
mongos --configdb configRS/192.168.1.20:27019,192.168.1.21:27019,192.168.1.22:27019 --bind_ip 0.0.0.0 --port 27017
4.4.2 添加分片
mongo
sh.addShard("shard1RS/192.168.1.30:27018,192.168.1.31:27018,192.168.1.32:27018")
sh.addShard("shard2RS/192.168.1.40:27018,192.168.1.41:27018,192.168.1.42:27018")
4.4.3 启用分片
sh.enableSharding("testdb")
sh.shardCollection("testdb.users", { "_id": "hashed" })
五、安全配置
5.1 创建集群管理员
mongo --port 27017
use admin
db.createUser({
user: "clusterAdmin",
pwd: "yourpassword",
roles: ["clusterAdmin", "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]
})
5.2 启用认证
在所有节点的配置文件中添加:
from:www.itpux.com 学习交流加群风哥QQ113257174 学习交流加群风哥QQ113257174 from:www.itpux.com
security:
authorization: enabled
5.3 重启服务
sudo systemctl restart mongod
六、监控与维护
6.1 查看集群状态
mongo -u clusterAdmin -p yourpassword --authenticationDatabase admin
sh.status()
rs.status()
6.2 查看分片状态
db.adminCommand({ listShards: 1 })
6.3 备份集群
mongodump --uri="mongodb://clusterAdmin:yourpassword@192.168.1.50:27017" --out=/backup/mongodb
6.4 恢复集群
mongorestore --uri="mongodb://clusterAdmin:yourpassword@192.168.1.50:27017" /backup/mongodb
七、故障排查
7.1 复制集故障
- 查看复制集状态:
rs.status() - 查看复制延迟:
rs.printSlaveReplicationInfo() - 重新添加节点:
rs.add("192.168.1.11:27017")
7.2 分片集群故障
- 查看分片状态:
sh.status() - 检查配置服务器:
db.adminCommand({ configsvrState: 1 }) - 检查 Mongos 状态:
db.adminCommand({ mongosInfo: 1 })
提示:生产环境中建议使用至少 3 个节点的复制集,以确保高可用性。
警告:分片集群配置复杂,建议在测试环境中充分测试后再部署到生产环境。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
