MongDB Replica Set
测试环境
os:centos 6.7
MongoDB:mongodb-linux-x86_64-3.0.5.tgz
ip地址:192.168.59.215 (主)
192.168.59.217 (备)
192.168.59.218 (仲裁)
分别在各台服务器关闭防火墙和slinux
[root@feng ~]# setenforce 0
[root@feng ~]# service iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
分别在每台服务器解压缩MongoDB包
[root@feng ~]# tar zxf mongodb-linux-x86_64-3.0.5.tgz
[root@feng ~]# mv mongodb-linux-x86_64-3.0.5 /usr/local/mongodb
[root@feng ~]# cd /usr/local/mongodb/
[root@feng mongodb]# ls
bin GNU-AGPL-3.0 README THIRD-PARTY-NOTICES
建立数据文件夹
[root@feng ~]# mkdir -p /data/mongodb/master #主节点
[root@feng ~]# mkdir -p /data/mongodb/slaver #从节点
[root@feng ~]# mkdir -p /data/mongodb/arbiter #仲裁节点
分别在服务器中建立配置文件
[root@feng ~]# cd /usr/local/mongodb
[root@feng ~]# mkdir conf #创建配置文件目录
[root@feng ~]#mkdir /data/mongodb/logs #创建日志路径
主配置文件
[root@feng ~]# vim /usr/local/mongodb/conf/master.conf
dbpath=/data/mongodb/master #数据存放路径
logpath=/data/mongodb/logs/master.log #日志存放路径
pidfilepath=/data/mongodb/master.pid #进程文件 方便停止mongdb
directoryperdb=true #为每一个数据库按照数据库名建立文件夹存放
logappend=true #以追加的方式记录日志
replSet=ytk #replica set的名字
port=27017 #进程所使用的端口号,默认为27017
oplogSize=10000 #操作日志文件的最大大小
fork=true #以后台方式运行进程
从配置文件
[root@feng ~]# vim /usr/local/mongodb/conf/slaver.conf
dbpath=/data/mongodb/slaver
logpath=/data/mongodb/logs/slaver.log
pidfilepath=/data/mongodb/slaver.pid
directoryperdb=true
logappend=true
replSet=ytk
port=27017
oplogSize=10000
fork=true
noprealloc=true #不预先分配存储
仲裁配置文件
[root@feng ~]# vim /usr/local/mongodb/conf/arbiter.conf
dbpath=/data/mongodb/arbiter
logpath=/data/mongodb/logs/arbiter.log
pidfilepath=/data/mongodb/arbiter.pid
directoryperdb=true
logappend=true
replSet=ytk
port=27018
oplogSize=10000
fork=true
noprealloc=true
分别启动 主 从 仲裁的结点
#主
[root@192 logs]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/master.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3372
child process started successfully, parent exiting
#从
[root@feng conf]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/slaver.conf
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 10321
child process started successfully, parent exiting
#仲裁
[root@feng conf]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/arbiter.conf
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 10312
child process started successfully, parent exiting
配置主 从 仲裁结点
编辑用户HOME目录下的.bash_profile配置文件,添加如下内容:
[root@192 ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin
export PATH
[root@192 ~]# source .bash_profile
在主节点中执行如下操作:
[root@192 ~]# mongo
> use admin
switched to db admin
> cfg={_id:"ytk",members:}
2020-06-04T02:12:27.788+0800 E QUERY SyntaxError: Unexpected token }
> cfg={_id:"ytk",members: [ {_id:0,host:'192.168.59.215:27017',priority:2}, {_id:1,host:'192.168.59.217:27017',priority:1}, {_id:2,host:'192.168.59.218:27018',arbiterOnly:true}]};
#使配置文件刷新
> rs.initiate(cfg)
{ "ok" : 1 }
#查看状态
ytk:OTHER> rs.status()
····
{ "ok" : 1 }
····
ytk:PRIMARY> db.printReplicationInfo() #查看Replication信息
configured oplog size: 10000.003845214844MB
log length start to end: 0secs (0hrs)
oplog first event time: Thu Jun 04 2020 02:14:03 GMT+0800 (CST)
oplog last event time: Thu Jun 04 2020 02:14:03 GMT+0800 (CST)
now: Thu Jun 04 2020 02:15:39 GMT+0800 (CST)
ytk:PRIMARY> db.printSlaveReplicationInfo() #查看备节点Replication信息
source: 192.168.59.217:27017
syncedTo: Thu Jun 04 2020 02:14:03 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
ytk:PRIMARY> exit
bye