Nosql Mongodb之旅(24)—MongoDB Replica Sets

前端之家收集整理的这篇文章主要介绍了Nosql Mongodb之旅(24)—MongoDB Replica Sets前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、读写分离

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

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

[plain] view plain copy
  1. [root@localhostbin]#./mongo--port28010
  2. MongoDBshellversion:1.8.1
  3. connectingto:127.0.0.1:28010/test
  4. rs1:PRIMARY>db.c1.insert({age:30})
  5. db.c2rs1:PRIMARY>db.c1.find()
  6. {"_id":ObjectId("4fc77f421137ea4fdb653b4a"),"age":30}
2、在从库中进行查询等操作
    [root@localhostbin]#./mongo--port28011
  1. connectingto:127.0.0.1:28011/test
  2. rs1:SECONDARY>showcollections
  3. ThuMay3122:27:17uncaughtexception:error:{"$err":"notmasterandslaveok=false",
  4. "code":13435}
  5. rs1:SECONDARY>
查询报错,说明是个从库且不能执行查询的操作

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

    rs1:SECONDARY>db.getMongo().setSlaveOk()
  1. notmasterandslaveok=false
  2. rs1:SECONDARY>showcollections
  3. c1
  4. system.indexes
  5. rs1:SECONDARY>db.c1.find()
  6. {"_id":ObjectId("4fc77f421137ea4fdb653b4a"),"age":30}
  7. rs1:SECONDARY>
我们做到了。

二、故障转移

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

测试:

1、kill 28010端口的MongoDB

    [root@localhostbin]#psaux|grepmongod
  1. root67061.66.94633046168Sl21:490:26
  2. /Apps/mongo/bin/mongod--replSetrs1--keyFile/data/key/r0--fork--port28010
  3. root67330.46.74305286044?Sl21:500:06
  4. /Apps/mongo/bin/mongod--replSetrs1--keyFile/data/key/r1--fork--port28011
  5. root67470.44.74315484260?Sl21:500:06
  6. /Apps/mongo/bin/mongod--replSetrs1--keyFile/data/key/r2--fork--port28012
  7. root70190.00.75064684pts/2S+22:160:00grepmongod
  8. [root@localhostbin]#kill-96706
2、再查看复制集状态
    rs1:SECONDARY>rs.status()
  1. {
  2. "set":"rs1",
  3. "date":ISODate("2012-05-31T14:17:03Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "myState":2,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "members":[
  4. {
  5. "_id":0,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"localhost:28010",108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "health":0,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "state":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "stateStr":"(notreachable/healthy)",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "uptime":0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "optime":{
  6. "t":1338472279000,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "i":1
  7. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "optimeDate":ISODate("2012-05-31T13:51:19Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T14:16:42Z"),108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "errmsg":"socketexception"
  8. "_id":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"localhost:28011",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "health":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "state":2,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "stateStr":"SECONDARY",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "self":true
  9. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "_id":2,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"localhost:28012",108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "health":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "stateStr":"PRIMARY",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "uptime":1528,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T14:17:02Z")
  10. }
  11. ],108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ok":1
  12. }
  13. rs1:SECONDARY>
    "stateStr":"(notreachable/healthy)",
可以看到28010 这个端口的MongoDB 出现了异常,而系统自动选举了28012 这个端口为主,所以这样的故障处理机制,能将系统的稳定性大大提高。

猜你在找的NoSQL相关文章