在CentOS上配置MongoDB復制集(Replica Set)可以提供數據冗余和高可用性。以下是一個基本的步驟指南,幫助你在CentOS上配置MongoDB復制集。
在所有節點上啟動MongoDB服務:
sudo systemctl start mongod
編輯MongoDB配置文件(通常位于/etc/mongod.conf),確保以下配置項正確設置:
net:
port: 27017
bindIp: <節點IP地址>
replication:
replSetName: <復制集名稱>
例如,在節點1上:
net:
port: 27017
bindIp: 192.168.1.101
replication:
replSetName: rs0
在節點2上:
net:
port: 27017
bindIp: 192.168.1.102
replication:
replSetName: rs0
在節點3上:
net:
port: 27017
bindIp: 192.168.1.103
replication:
replSetName: rs0
連接到任意一個MongoDB實例(例如節點1),并初始化復制集:
mongo --host 192.168.1.101 --port 27017
在mongo shell中執行以下命令:
rs.initiate(
{
_id: "rs0",
members: [
{ _id: 0, host: "192.168.1.101:27017" },
{ _id: 1, host: "192.168.1.102:27017" },
{ _id: 2, host: "192.168.1.103:27017" }
]
}
)
在mongo shell中執行以下命令來驗證復制集的狀態:
rs.status()
你應該看到所有節點都列在members數組中,并且狀態為SECONDARY或PRIMARY。
replSetName值相同。通過以上步驟,你應該能夠在CentOS上成功配置MongoDB復制集。如果有任何問題,請檢查日志文件(通常位于/var/log/mongodb/mongod.log)以獲取更多信息。