一、环境
centos6.5
node1:172.30.31.114
node2:172.30.31.115
node3:172.30.31.108
node4:172.30.31.109 仲裁点
二、安装
四个节点均需执行
1、安装软件包
#yum install -y mongodb mongodb-server
#echo "this is key file" > /home/keyfile
#chown mongodb /home/keyfile
#chmod 600 /home/keyfile
#vim /etc/mongodb.conf
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath =/var/lib/mongodb
journal = true
logappend = true
replSet = mongotest
keyFile = /home/keyfile
auth = true
oplogSize = 10000
#/etc/init.d/mongod start
#chkconfig mongod on
三、创建复制集
1node1 上执行
#mongo admin
>db.runCommand({"replSetInitiate":{"_id":"mongotest","members": [
{"_id":1,"host":172.30.31.114"},
{"_id":2,"host":172.30.31.115"},
{"_id":3,"host":172.30.31.108"}
]}})
>{"info":"Config now saved locally .Should come online in about a minute.","ok":1}
>exit
#mongo admin
mongotest:PRIMARY>use admin
mongotest:PRIMARY>db.addUser("root","mongodb")
创建管理账号,此时node2,node3均使用此账号密码。即使node2,node3使用的管理账号不同,也会被覆盖为此
四、添加仲裁节点
1在主节点上执行
mongotest:PRIMARY>rs.addArb(“172.30.31.109:27017”)
mongotest:PRIMARY>rs.status() #查看集群状态
注意SECONDARY节点默认没有读权限,执行rs.slaveok()可以开启。仲裁节点只参与投票选举,不参与同步数据。