mongodb学习之:副本集
前面一张介绍了主从模式,现在mongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模式。副本集不能在一台电脑上操作。需要准备3台电脑进行搭建。副本集就是mongoDB副本所组成的一个集群。
同步原理是,写操作发生在主库,从库同步主库的OpLog日志。
集群中没有特定的主库,主库是选举产生,如果主库down了,会再选举出一台主库。
下面来看具体的搭建。使用三台电脑进行搭建,分别是
192.168.0.12:27017 zhf-maple
192.168.0.11:27017 zhf-linux
192.168.0.4:27017 ubuntu
root@zhf-maple:/home/zhf/桌面# cat /etc/hosts
127.0.0.1 localhost
192.168.0.12 zhf-maple
192.168.0.11 zhf-linux
192.168.0.4 ubuntu
在其他两台电脑的/etc/hosts文件也分别添加对应的域名进去
在三台电脑上分别创建node0,node1,node2文件夹然后分别运行命令
mongod --dbpath /home/zhf/node0 --replSet rs0
mongod --dbpath /home/zhf/node1 --replSet rs0
mongod --dbpath /home/zhf/node2 --replSet rs0
此时进入任意一个mongod服务器。使用admin集合。然后运行rs.initiate()
> use admin
switched to db admin
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "zhf-linux:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
确认配置
> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "zhf-linux:27017"
}
]
}
测试在primary上添加从库
rs0:PRIMARY> rs.add('ubuntu:27017')
{ "ok" : 1 }
查看状态。health:1代表正常启动,stateStr可以看到哪个是主的,哪个是从的
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-01-16T09:38:21Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "zhf-linux:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 587,
"optime" : Timestamp(1516095474, 1),
"optimeDate" : ISODate("2018-01-16T09:37:54Z"),
"electionTime" : Timestamp(1516095459, 1),
"electionDate" : ISODate("2018-01-16T09:37:39Z"),
"self" : true
},
{
"_id" : 1,
"name" : "ubuntu:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 27,
"optime" : Timestamp(1516095474, 1),
"optimeDate" : ISODate("2018-01-16T09:37:54Z"),
"lastHeartbeat" : ISODate("2018-01-16T09:38:20Z"),
"lastHeartbeatRecv" : ISODate("2018-01-16T09:38:19Z"),
"pingMs" : 54,
"syncingTo" : "zhf-linux:27017"
}
],
"ok" : 1
}
再次添加从库。此时2个secondary,一个primary就已经启动起来了
rs0:PRIMARY> rs.add('zhf-maple:27017')
{ "down" : [ "zhf-maple:27017" ], "ok" : 1 }
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-01-16T09:38:42Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "zhf-linux:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 608,
"optime" : Timestamp(1516095516, 1),
"optimeDate" : ISODate("2018-01-16T09:38:36Z"),
"electionTime" : Timestamp(1516095459, 1),
"electionDate" : ISODate("2018-01-16T09:37:39Z"),
"self" : true
},
{
"_id" : 1,
"name" : "ubuntu:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : Timestamp(1516095516, 1),
"optimeDate" : ISODate("2018-01-16T09:38:36Z"),
"lastHeartbeat" : ISODate("2018-01-16T09:38:41Z"),
"lastHeartbeatRecv" : ISODate("2018-01-16T09:38:42Z"),
"pingMs" : 7,
"syncingTo" : "zhf-linux:27017"
},
{
"_id" : 2,
"name" : "zhf-maple:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-01-16T09:38:42Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Received heartbeat from member with the same member ID as ourself: 0"
}
],
接下来在primary上进行数据更新:
s0:PRIMARY> use maple
switched to db maple
rs0:PRIMARY> db.insert({'name':'zhanghongfeng'})
2018-01-16T17:56:06.625+0800 TypeError: Property 'insert' of object maple is not a function
rs0:PRIMARY> db.maple.insert({'name':'zhanghongfeng'})
WriteResult({ "nInserted" : 1 })
此时在secondary上查看数据库
rs0:SECONDARY> use maple
switched to db maple
rs0:SECONDARY> db.maple.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
查找的时候提示not master and slaveOk=false。原因在于mongodb默认是从主节点读写数据的,副本节点上不允许读(更不能写入),需要设置副本节点可以读。通过db.getMongo().setSlaveOk()进行设置
rs0:SECONDARY> db.getMongo().setSlaveOk()
rs0:SECONDARY> db.maple.find()
{ "_id" : ObjectId("5a5dcc538c0f7da3bc2f819f"), "name" : "zhanghongfeng" }
这样就能查询到对应的数据了,表明在副本节点上进行了同步