在Debian上部署MongoDB集群涉及多个步骤,包括安装MongoDB、配置副本集、配置分片、启动集群等。以下是一个基本的部署方案:
首先,在每台Debian服务器上安装MongoDB。可以使用以下命令:
sudo apt update
sudo apt install -y mongodb-org
在每台服务器上创建MongoDB配置文件,通常位于/etc/mongod.conf
。以下是一个基本的配置示例:
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
使用以下命令启动MongoDB服务:
sudo systemctl start mongod
sudo systemctl enable mongod
连接到任意一个MongoDB实例并初始化副本集:
mongo --host <IP_ADDRESS> --port 27017
在Mongo shell中执行:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "<IP_ADDRESS_1>:27017" },
{ _id: 1, host: "<IP_ADDRESS_2>:27017" },
{ _id: 2, host: "<IP_ADDRESS_3>:27017" }
]
})
在mongos实例上配置分片:
mongo --host <mongos_IP> --port 27017
启用分片并添加分片到集群:
sh.enableSharding("<database>")
sh.shardCollection("<database>.<collection>", { "<field>" : 1 })
使用以下命令检查集群状态:
sh.status()
bindIp
为0.0.0.0
或具体的IP地址。辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Debian Node.js安全吗