在CentOS上配置MongoDB分片集群涉及多个步骤,包括设置配置服务器、分片服务器和路由服务器(mongos)。以下是一个基本的指南:
确保你已经安装了MongoDB,并且所有节点都可以通过网络互相通信。
配置服务器存储集群的元数据。
启动配置服务器:
mongod --configsvr --replSet configReplSet --dbpath /data/configdb --port 27019
初始化配置服务器副本集: 连接到其中一个配置服务器:
mongo --port 27019
在mongo shell中执行:
rs.initiate(
{
_id: "configReplSet",
configsvr: true,
members: [
{ _id : 0, host : "config1.example.com:27019" },
{ _id : 1, host : "config2.example.com:27019" },
{ _id : 2, host : "config3.example.com:27019" }
]
}
)
分片服务器存储实际的数据。
启动分片服务器:
mongod --shardsvr --replSet shardReplSet --dbpath /data/shard1 --port 27018
重复上述命令,为每个分片创建一个副本集。
初始化分片副本集: 连接到其中一个分片服务器:
mongo --port 27018
在mongo shell中执行:
rs.initiate(
{
_id: "shardReplSet",
members: [
{ _id : 0, host : "shard1.example.com:27018" },
{ _id : 1, host : "shard2.example.com:27018" },
{ _id : 2, host : "shard3.example.com:27018" }
]
}
)
路由服务器是应用程序与分片集群之间的接口。
mongos --configdb configReplSet/config1.example.com:27019,config2.example.com:27019,config3.example.com:27019 --port 27017
连接到mongos:
mongo --port 27017
在mongo shell中执行:
sh.addShard("shardReplSet/shard1.example.com:27018,shard2.example.com:27018,shard3.example.com:27018")
选择要分片的数据库和集合:
sh.enableSharding("mydatabase")
sh.shardCollection("mydatabase.mycollection", { "shardKey": 1 })
检查集群状态:
sh.status()
通过以上步骤,你应该能够在CentOS上成功配置MongoDB分片集群。