搭建MongoDB复制集

主节点:

28001.conf

port=28001
bind_ip=192.168.0.1
logpath=/usr/local/mongodb/log/28001.log
dbpath=/usr/local/mongodb/data/28001/
logappend=true
pidfilepath=/usr/local/mongodb/data/28001/28001.pid
fork=true
oplogSize=1024
replSet=RS0

28002.conf

port=28002
bind_ip=192.168.0.2
logpath=/usr/local/mongodb/log/28002.log
dbpath=/usr/local/mongodb/data/28002/
logappend=true
pidfilepath=/usr/local/mongodb/data/28002/28002.pid
fork=true
oplogSize=1024
replSet=RS0

28003.conf arbiter投票节点

port=28003
bind_ip=192.168.0.3
logpath=/usr/local/mongodb/log/28003.log
dbpath=/usr/local/mongodb/data/28003/
logappend=true
pidfilepath=/usr/local/mongodb/data/28003/28003.pid
fork=true
oplogSize=1024
replSet=RS0

启动实例:

mongod -f /usr/local/mongodb/conf/28001.conf

mongod -f /usr/local/mongodb/conf/28002.conf

mongod -f /usr/local/mongodb/conf/28003.conf

配置:
mongo 192.168.0.1:28001/admin

config = {
    _id: "RS0",
    members: [
        {_id: 0, host: "192.168.0.1:28001"},
        {_id: 1, host: "192.168.0.2:28002"},
        {_id: 2, host: "192.168.0.3:28003"},
    ]
}

// 设置 arbiter 节点
config.members[2] = {"_id": 2, "host": "192.168.0.3:28003", "arbiterOnly": true}

rs.initiate(config)  //初始化
rs.status //查看状态

标签: mongodb

添加新评论