1.创建配置文件

www/server/mongodb/rs0_27017/config.conf

  • data
  • log

www/server/mongodb/rs0_27018/config.conf

www/server/mongodb/rs0_27019/config.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
net:
bindIp: localhost,192.168.152.133
port: 27017
processManagement:
fork: true
pidFilePath: /www/server/mongodb/rs0_27017/log/configsvr.pid
security:
authorization: disabled
javascriptEnabled: false
storage:
dbPath: /www/server/mongodb/rs0_27017/data
directoryPerDB: true
systemLog:
destination: file
logAppend: true
path: /www/server/mongodb/rs0_27017/log/config.log
replication:
replSetName: rs0

注意,每个都需要先新建出 data, log目录

2.初始化

  1. 分别启动三个副本

    /www/server/mongodb/rs0_27017/config.conf

    /www/server/mongodb/rs0_27018/config.conf

    /www/server/mongodb/rs0_27019/config.conf

    查看是否成功

    ps aux | grep mongod

  2. 进入mongo

    cd /www/server/mongodb/bin

    mongosh

    image-20201013181712107

  3. init 并添加节点

    rs.initiate()

    rs.add(“192.168.152.133:27018”)

    rs.add(“192.168.152.133:27019”,true) //仲裁 rs.addArb(“192.168.152.133:27019”)

    如果报 Reconfig attempted to install a config that would change the implicit default write concern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again 执行

    db.adminCommand({
    “setDefaultRWConcern” : 1,
    “defaultWriteConcern” : {
    “w” : 2
    }
    })

    image-20201013181712107

  4. 完成,查询状态

    rs.status()

  5. 常用命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 查看集群状态
    rs.status()
    # 初始化集群
    rs.initiate()
    # 添加节点 新增从节点
    rs.add(“ip:port”)
    # 添加节点 新增仲裁节点
    rs.addArb(“ip:port”)
    # 删除一个节点
    rs.remove(“ip:port”)
    # 查看副本集的配置信息
    rs.config()
    或者
    rs.conf()
    # 副本集角色切换(不要随便操作)
    rs.stepDown()