操作系统 : CentOS release 6.8 (Final)
Master IP : 10.4.19.83
Arbiter IP : 10.104.27.126
Slave IP : 10.104.27.126
安装文件 : mongodb-linux-x86_64-rhel62-4.4.9.tgz
依次在10.104.27.126\10.4.19.83机器上执行下面命令,准备基础安装目录:
tar -xf mongodb-linux-x86_64-rhel62-4.4.9.tgz
cp -R mongodb-linux-x86_64-rhel62-4.4.9 /opt/mongo
rm -rf mongodb-linux-x86_64-rhel62-4.4.9
创建Master节点(10.4.19.83:27017)相关目录
mkdir -p /opt/mongo/master/{data,conf,logs}
创建Slave节点(10.104.27.126:27017)相关目录
mkdir -p /opt/mongo/slave/{data,conf,logs}
创建Arbiter节点(10.104.27.126:27018)相关目录
mkdir -p /opt/mongo/arbiter/{data,conf,logs}
创建Master节点(10.4.19.83:27017)配置文件
cat << \EOF > /opt/mongo/master/conf/master.conf
#数据文件存放目录
dbpath = /opt/mongo/master/data
#日志文件存放目录
logpath = /opt/mongo/master/logs/mongodb.log
# 记录进程ID
pidfilepath = /opt/mongo/master/master.pid
#开启日志追加添加日志
logappend = true
#以守护程序的方式启用,即在后台运行
fork = true
#监听IP
bind_ip = 10.4.19.83,127.0.0.1
#端口,默认27017,可以自定义
port = 27017
#复制集名称
replSet = centalineMongo
#oplog大小上限,超过后会进行覆盖,若不设置默认为磁盘大小的5%
oplogSize = 10240
# 开启日志切割
logRotate = reopen
EOF
创建Slave节点(10.104.27.126:27017)配置文件
cat << \EOF > /opt/mongo/slave/conf/slave.conf
#数据文件存放目录
dbpath = /opt/mongo/slave/data
#日志文件存放目录
logpath = /opt/mongo/slave/logs/mongodb.lo
# 记录进程ID
pidfilepath = /opt/mongo/slave/slave.pid
#开启日志追加添加日志
logappend = true
#以守护程序的方式启用,即在后台运行
fork = true
#监听IP
bind_ip = 10.104.27.126,127.0.0.1
#端口,默认27017,可以自定义
port = 27017
#复制集名称
replSet = centalineMongo
#oplog大小上限,超过后会进行覆盖,若不设置默认为磁盘大小的5%
oplogSize = 10240
# 开启日志切割
logRotate = reopen
EOF
创建Arbiter节点(10.104.27.126:27018)配置文件
cat << \EOF > /opt/mongo/arbiter/conf/arbiter.conf
#数据文件存放目录
dbpath = /opt/mongo/arbiter/data
#日志文件存放目录
logpath = /opt/mongo/arbiter/logs/arbiter.lo
# 记录进程ID
pidfilepath = /opt/mongo/arbiter/arbiter.pid
#开启日志追加添加日志
logappend = true
#以守护程序的方式启用,即在后台运行
fork = true
#监听IP
bind_ip = 10.104.27.126,127.0.0.1
#端口,默认27017,可以自定义
port = 27018
#复制集名称
replSet = centalineMongo
#oplog大小上限,超过后会进行覆盖,若不设置默认为磁盘大小的5%
oplogSize = 10240
# 开启日志切割
logRotate = reopen
EOF
启动服务,启动顺序依次是【master,slave,arbiter】,不能错
/opt/mongo/bin/mongod -f /opt/mongo/master/conf/master.conf
/opt/mongo/bin/mongod -f /opt/mongo/slave/conf/slave.conf
/opt/mongo/bin/mongod -f /opt/mongo/arbiter/conf/arbiter.conf
初始化副本集
集当中的 _id:"centalineMongo"
,一定要与conf文件当中的replSet保持一致。
# 连接
/opt/mongo/bin/mongo 10.4.19.83:27017
# 切换至admin库,初始化副本
use admin;
rs.initiate(
{_id:"centalineMongo",members:[
? ? {_id: 0,host: "10.4.19.83:27017"},
? ? {_id: 1,host: "10.104.27.126:27017"},
? ? {_id: 2,host: "10.104.27.126:27018",arbiterOnly: true}
]}
);
关闭服务顺序:
停止Slave节点(10.104.27.126:27017):
/opt/mongo/bin/mongo --port 27017 admin --eval "db.shutdownServer()"
停止Arbiter节点(10.104.27.126:27018):
/opt/mongo/bin/mongo --port 27018 admin --eval "db.shutdownServer()"
停止Master节点(10.4.19.83:27017):
/opt/mongo/bin/mongo --port 27017 admin --eval "db.shutdownServer()"