mongodb 4.0.4 副本集搭建

mongo-1配置文件:

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo-1/logs/mongodb.log

storage:
  dbPath: /opt/mongo-1/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongo-1/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 9797
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0

mongo-2配置文件

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo-2/logs/mongodb.log

storage:
  dbPath: /opt/mongo-2/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongo-2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 9798
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0

分别进入mongo-1 mongo-2的bin目录,然后运行:

./mongod -f 配置文件路径

成功则如图所示


image.png

然后随便进入一个节点运行

./mongo --port 端口号

然后在控制台输入:

rsconf = {
  _id: "rs0",
  members: [
    {
     _id: 0,
     host: "47.99.216.139:9797"
    },
    {
     _id: 1,
     host: "47.99.216.139:9798"
    }
   ]
};

随后运行

rs.initiate( rsconf )

使用rs.status()查看状态

image.png

搭建成功

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容