以下是在Debian上配置MongoDB集群的步驟:
更新系統,添加MongoDB官方倉庫并安裝:
sudo apt update && sudo apt upgrade -y
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update
sudo apt install -y mongodb-org
在每個節點上編輯/etc/mongod.conf
:
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
# 可選:配置密鑰文件用于節點間認證
# security:
# keyFile: /path/to/mongodb-keyfile
storage:
dbPath: /data/db
重啟服務:
sudo systemctl restart mongod
在主節點上初始化副本集:
mongo --host <主節點IP> --port 27017
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "<主節點IP>:27017" },
{ _id: 1, host: "<從節點1IP>:27017" },
{ _id: 2, host: "<從節點2IP>:27017" }
]
})
在其他節點上使用rs.add
命令添加節點。
若要配置分片集群,需設置配置服務器、分片服務器和路由服務器,步驟如下:
mongos
實例并連接到配置服務器。mongos
shell添加分片到集群。以上步驟可根據實際需求調整,如配置仲裁節點、設置分片鍵等。配置完成后,可使用rs.status()
等命令驗證集群狀態。