阅读量:2
以下是在 Debian 上搭建 MongoDB 集群的指南:
安装 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
配置 MongoDB 实例
在每个节点上创建或编辑 /etc/mongod.conf,示例配置如下:
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
storage:
dbPath: /data/db
journal:
enabled: true
启动并启用服务:
sudo systemctl start mongod
sudo systemctl enable mongod
创建副本集
在其中一个节点上初始化副本集,以一主两副本为例:
mongo --host --port 27017
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: ":27017" },
{ _id: 1, host: ":27017" },
{ _id: 2, host: ":27017" }
]
})
在其他节点上使用 rs.add 命令添加节点。
搭建分片集群(可选)
若要搭建分片集群,需配置 Config Server 和 mongos:
- Config Server:在每个 Config Server 节点上配置
sharding.clusterRole: configsvr等参数,初始化副本集,如rs.initiate({_id: "configRs", members: [...]})。 - Shard 分片:每个分片节点配置为
sharding.clusterRole: shardsvr的副本集,初始化后通过sh.addShard添加到集群,再用sh.enableSharding启用分片。
验证集群状态
使用 sh.status() 命令查看集群状态。
以上就是关于“MongoDB 集群搭建在 Debian 上的指南”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm