码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB整理笔记の管理Replica Sets

时间:2015-01-24 13:02:56      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

    一、读写分离

    从库能进行查询,这样可以分担主库的大量的查询请求。

    1、先向主库中插入一条测试数据

[root@localhost bin]# ./mongo --port 28010
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:28010/test
rs1:PRIMARY> db.c1.insert({age:30})
db.c2rs1:PRIMARY> db.c1.find()
{ "_id" : ObjectId("4fc77f421137ea4fdb653b4a"), "age" : 30 }

    2、在从库中进行查询等操作

[root@localhost bin]# ./mongo --port 28011
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> show collections
Thu May 31 22:27:17 uncaught exception: error: { "$err" : "not master and slaveok=false",
"code" : 13435 }
rs1:SECONDARY>

    查询报错,说明是个从库且不能执行查询的操作

    3、让从库可以读,分担主库的压力  

rs1:SECONDARY> db.getMongo().setSlaveOk()
not master and slaveok=false
rs1:SECONDARY> show collections
c1
system.indexes
rs1:SECONDARY> db.c1.find()
{ "_id" : ObjectId("4fc77f421137ea4fdb653b4a"), "age" : 30 }
rs1:SECONDARY>

    我们做到了。

    二、故障转移

   复制集比传统的Master-Slave 有改进的地方就是他可以进行故障的自动转移,如果我们停掉复制集中的一个成员,那么剩余成员会再自动选举出一个成员,做为主库,例如:我们将28010 这个主库停掉,然后再看一下复制集的状态 

    测试:

    1、kill 28010端口的MongoDB

技术分享
[root@localhost bin]# ps aux|grep mongod
root 6706 1.6 6.9 463304 6168 Sl 21:49 0:26
/Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010
root 6733 0.4 6.7 430528 6044 ? Sl 21:50 0:06
/Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011
root 6747 0.4 4.7 431548 4260 ? Sl 21:50 0:06
/Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012
root 7019 0.0 0.7 5064 684 pts/2 S+ 22:16 0:00 grep mongod
[root@localhost bin]# kill -9 6706
View Code

    2、再查看复制集状态

技术分享
[root@localhost bin]# ./mongo --port 28011
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2012-05-31T14:17:03Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 0,
"state" : 1,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"t" : 1338472279000,
"i" : 1
},
"optimeDate" : ISODate("2012-05-31T13:51:19Z"),
"lastHeartbeat" : ISODate("2012-05-31T14:16:42Z"),
"errmsg" : "socket exception"
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"optime" : {
"t" : 1338472279000,
"i" : 1
},
"optimeDate" : ISODate("2012-05-31T13:51:19Z"),
"self" : true
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1528,
"optime" : {
"t" : 1338472279000,
"i" : 1
},
"optimeDate" : ISODate("2012-05-31T13:51:19Z"),
"lastHeartbeat" : ISODate("2012-05-31T14:17:02Z")
}
],
"ok" : 1
}
rs1:SECONDARY>   
"stateStr" : "(not reachable/healthy)",
View Code

    可以看到28010 这个端口的MongoDB 出现了异常,而系统自动选举了28012 这个端口为主,所以这样的故障处理机制,能将系统的稳定性大大提高。

MongoDB整理笔记の管理Replica Sets

标签:

原文地址:http://www.cnblogs.com/tomcatx/p/4245653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!