阅读量:3
在CentOS上搭建MongoDB分片集群涉及多个步骤,包括配置服务器、配置分片、配置副本集等。以下是一个基本的指南:
准备工作
- 安装MongoDB:
sudo yum install -y mongodb-org
- 启动MongoDB服务:
sudo systemctl start mongod
sudo systemctl enable mongod
配置服务器
- 配置文件示例 (
/etc/mongod.conf):
sharding:
clusterRole: shardsvr
net:
port: 27018
bindIp: 服务器IP地址
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
- 启动每个服务器:
sudo mongod --config /etc/mongod.conf
配置副本集
- 连接到MongoDB:
mongo --port 27018
- 初始化副本集:
rs.initiate({_id : "shard1", members : [{_id : 0 , host : "shard1_ip:27018"}, {_id : 1 , host : "shard1_ip:27019"}, {_id : 2 , host : "shard1_ip:27020"}]})
重复上述步骤为 shard2 和 shard3 配置副本集。
配置配置服务器
- 启动配置服务器:
mongod --configsvr --replSet configReplSet --dbpath /data/configdb --port 27019
- 初始化配置服务器副本集:
mongo --port 27019
rs.initiate({_id : "configReplSet", configsvr : true, members : [{_id : 0 , host : "config_server_ip:27019"}]})
配置分片
- 连接到mongos:
mongos --configdb configReplSet/config_server_ip:27019 --port 27017
- 添加分片:
sh.addShard("shard1/shard1_ip:27018,shard1_ip:27019,shard1_ip:27020")
sh.addShard("shard2/shard2_ip:27018,shard2_ip:27019,shard2_ip:27020")
sh.addShard("shard3/shard3_ip:27018,shard3_ip:27019,shard3_ip:27020")
启用数据库和集合分片
- 启用数据库和集合的分片:
sh.enableSharding("yourDatabaseName")
sh.shardCollection("yourDatabaseName.yourCollectionName", { "shardKey" : 1 })
验证配置
- 验证分片集群:
sh.status()
注意事项
- 确保所有服务器之间的网络连接正常。
- 配置文件中的IP地址和端口号要正确。
- 根据实际需求调整副本集和分片的数量。
通过以上步骤,你可以在CentOS上搭建一个基本的MongoDB分片集群。根据实际需求,可能还需要进行更多的配置和优化。
以上就是关于“MongoDB分片集群CentOS如何搭建”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm