在Debian上配置MongoDB的副本集涉及几个步骤。以下是一个基本的指南,帮助你设置一个三节点的副本集。
如果你还没有安装MongoDB,可以使用以下命令安装:
sudo apt update
sudo apt install -y mongodb-org
编辑MongoDB配置文件 /etc/mongod.conf
,确保每个节点的配置如下:
节点1(Primary)
net:
port: 27017
bindIp: 0.0.0.0, <节点1的IP地址>
replication:
replSetName: rs0
节点2(Secondary)
net:
port: 27017
bindIp: 0.0.0.0, <节点2的IP地址>
replication:
replSetName: rs0
节点3(Secondary)
net:
port: 27017
bindIp: 0.0.0.0, <节点3的IP地址>
replication:
replSetName: rs0
在每个节点上重启MongoDB服务以应用配置更改:
sudo systemctl restart mongod
连接到任意一个MongoDB实例(例如节点1),并初始化副本集:
mongo --host <节点1的IP地址> --port 27017
在mongo shell中执行以下命令:
rs.initiate(
{
_id: "rs0",
members: [
{ _id: 0, host: "<节点1的IP地址>:27017" },
{ _id: 1, host: "<节点2的IP地址>:27017" },
{ _id: 2, host: "<节点3的IP地址>:27017" }
]
}
)
在mongo shell中执行以下命令来验证副本集的状态:
rs.status()
你应该看到所有节点都列在副本集中,并且有一个节点被标记为Primary。
/etc/mongod.conf
文件中的配置是否正确,特别是 bindIp
和 replSetName
。通过以上步骤,你应该能够在Debian上成功配置MongoDB的副本集。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>