在Linux环境下配置MongoDB集群,通常需要以下几个步骤:
确保你已经安装了MongoDB,并且每个节点都有独立的IP地址和端口。
为每个节点创建一个配置文件。以下是一个基本的配置文件示例:
sharding:
clusterRole: "mongos"
net:
port: 27017
storage:
dbPath: /var/lib/mongodb/shard1
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos.log
sharding:
clusterRole: "mongos"
net:
port: 27018
storage:
dbPath: /var/lib/mongodb/shard2
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos.log
sharding:
clusterRole: "configsvr"
net:
port: 27019
storage:
dbPath: /var/lib/mongodb/configdb
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/configsvr.log
sharding:
clusterRole: "shardsvr"
net:
port: 27017
storage:
dbPath: /var/lib/mongodb/shard1
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/shard1.log
在每个配置服务器节点上启动MongoDB,并使用配置文件启动:
mongod --config /path/to/config.conf
在每个mongos节点上启动MongoDB,并使用配置文件启动:
mongos --config /path/to/mongos.conf
连接到其中一个mongos节点,并添加配置服务器:
mongo --port 27017
在mongo shell中执行:
sh.addShard("configsvr1:27019")
sh.addShard("configsvr2:27019")
sh.addShard("configsvr3:27019")
选择一个数据库和集合,启用分片:
sh.enableSharding("yourDatabase")
sh.shardCollection("yourDatabase.yourCollection", { "shardKey": 1 })
在mongo shell中执行以下命令,验证集群状态:
sh.status()
通过以上步骤,你可以在Linux环境下成功配置一个MongoDB集群。