mongodb cluster - TuPengXiong/TuPengXiong.github.io GitHub Wiki
#Replica Set
- master 主节点 --主要访问的数据库
- slaver 备份节点 --备份master数据库
- arbiter 仲裁节点 --记录 master数据库出现问题将slaver设为主节点
###创建数据库存储目录
mkdir -p /mongodb/data/master
mkdir -p /mongodb/data/slaver
mkdir -p /mongodb/data/arbiter
###master配置文件
- master.conf
dbpath=/mongodb/data/master
logpath=/mongodb/log/master.log
pidfilepath=/mongodb/master.pid
directoryperdb=true
logappend=true
replSet=test
bind_ip=172.16.109.130
port=27017
oplogSize=10000
fork=true
noprealloc=true
###slaver配置文件
- slaver.conf
dbpath=/mongodb/data/slaver
logpath=/mongodb/log/slaver.log
pidfilepath=/mongodb/slaver.pid
directoryperdb=true
logappend=true
replSet=test
bind_ip=172.16.109.129
port=27017
oplogSize=10000
fork=true
noprealloc=true
###arbiter配置文件
- arbiter.conf
dbpath=/mongodb/data/arbiter
logpath=/mongodb/log/arbiter.log
pidfilepath=/mongodb/arbiter.pid
directoryperdb=true
logappend=true
replSet=test
bind_ip=172.16.109.128
port=27017
oplogSize=10000
fork=true
noprealloc=true
###启动mongodb
./monood master.conf
./mongod slaver.conf
./mongod arbiter.conf
./mongo 172.16.109.130:27017 #ip和port是某个节点的地址
use admin
config={
_id:"test",
members:[
{_id:0,host:'172.16.109.130:27017',priority:2},
{_id:1,host:'172.16.109.129:27017',priority:1},
{_id:2,host:'172.16.109.128:27017',arbiterOnly:true}
]
};
rs.initiate(config) #使配置生效
{ "ok" : 1 }
###查看状态
rs.status()
{
"set" : "test",
"date" : ISODate("2016-08-14T02:16:19.321Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "172.16.109.130:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 29,
"optime" : {
"ts" : Timestamp(1471140966, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-08-14T02:16:06Z"),
"electionTime" : Timestamp(1471140965, 1),
"electionDate" : ISODate("2016-08-14T02:16:05Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "172.16.109.129:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : {
"ts" : Timestamp(1471140966, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-08-14T02:16:06Z"),
"lastHeartbeat" : ISODate("2016-08-14T02:16:17.824Z"),
"lastHeartbeatRecv" : ISODate("2016-08-14T02:16:18.648Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "127.0.0.1:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.109.128:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 15,
"lastHeartbeat" : ISODate("2016-08-14T02:16:17.823Z"),
"lastHeartbeatRecv" : ISODate("2016-08-14T02:16:17.486Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1
}
###结果
在slaver数据中想查看数据是否一致,先执行 如果通过shell访问mongo,要在slaver进行查询。会出现如下错误:
test:SECONDARY> db.fs.files.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
有两种方法实现从机的查询: 第一种方法:
db.getMongo().setSlaveOk();
第二种方法:
rs.slaveOk();
但是这种方式有一个缺点就是,下次再通过mongo进入实例的时候,查询仍然会报错,为此可以通过下列方式
vi ~/.mongorc.js
增加一行
rs.slaveOk();
这样的话以后每次通过mongo命令进入都可以查询了
在master数据库的操作会在slaver的数据库中同步,达到了数据同步的效果