Nosql Mongodb之旅(25)—MongoDB Replica Sets增加节点

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

MongoDB Replica Sets 不仅提供高可用性的解决方案,它也同时提供负载均衡的解决方案,增减Replica Sets 节点在实际应用中非常普遍,例如当应用的读压力暴增时,3 台节点的环境已不能满足需求,那么就需要增加一些节点将压力平均分配一下。

增加节点

两种方式:一是通过oplog增加节点,二是通过数据库快照和oplog来增加节点

通过oplog增加节点

1、配置并启动新节点,启用28013这个端口给新的节点

[plain] view plain copy
  1. root@localhost~]#mkdir-p/data/data/r3
  2. [root@localhost~]#echo"thisisrs1supersecretkey">/data/key/r3
  3. [root@localhost~]#chmod600/data/key/r3
  4. [root@localhost~]#/Apps/mongo/bin/mongod--replSetrs1--keyFile/data/key/r3--fork--port
  5. 28013--dbpath/data/data/r3--logpath=/data/log/r3.log--logappend
  6. alloutputgoingto:/data/log/r3.log
  7. forkedprocess:10553
  8. [root@localhost~]#
2、添加此新节点到现有的Replica Sets
    rs1:PRIMARY>rs.add("localhost:28013")
  1. {"ok":1}
3、查看Replica Sets,我们可以清晰的看到内部是如何添加28013这个新节点的

(1)进行初始化

    rs1:PRIMARY>rs.status()
  1. {
  2. "set":"rs1",
  3. "date":ISODate("2012-05-31T12:17:44Z"),
  4. "myState":1,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "members":[
  5. ……
  6. "_id":3,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"localhost:28013",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":6,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":{
  7. "t":0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "i":0
  8. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "optimeDate":ISODate("1970-01-01T00:00:00Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T12:17:43Z"),108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "errmsg":"stillinitializing"
  9. }
  10. ],248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ok":1
  11. }
(2)进行数据同步
    "date":ISODate("2012-05-31T12:18:07Z"),108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "health":1,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "state":3,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "stateStr":"RECOVERING",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "uptime":16,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T12:18:05Z"),108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "errmsg":"initialsyncneedamembertobeprimaryorsecondary
  1. todoourinitialsync"
  2. }
  3. ],108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ok":1
  4. }
(3)初始化同步完成
    "date":ISODate("2012-05-31T12:18:08Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "uptime":17,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "t":1338466661000,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "i":1
  1. "optimeDate":ISODate("2012-05-31T12:17:41Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T12:18:07Z"),108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "errmsg":"initialsyncdone"
  2. (4)节点添加完成,状态正常
      "date":ISODate("2012-05-31T12:18:10Z"),248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "state":2,108); list-style:decimal-leading-zero outside; color:inherit; 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"> "uptime":19,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastHeartbeat":ISODate("2012-05-31T12:18:09Z")
    1. 4、验证数据已经同步过来
        [root@localhostdata]#/Apps/mongo/bin/mongo-port28013
      1. MongoDBshellversion:1.8.1
      2. connectingto:127.0.0.1:28013/test
      3. rs1:SECONDARY>rs.slaveOk()
      4. rs1:SECONDARY>db.c1.find()
      5. {"_id":ObjectId("4fc760d2383ede1dce14ef86"),"age":10}
      6. rs1:SECONDARY>
      通过数据库快照和oplog增加节点

      通过oplog 直接进行增加节点操作简单且无需人工干预过多,但oplog 是capped collection,采用循环的方式进行日志处理,所以采用oplog 的方式进行增加节点,有可能导致数据的不一致,因为日志中存储的信息有可能已经刷新过了。不过没关系,我们可以通过数据库快照(--fastsync)和oplog 结合的方式来增加节点,这种方式的操作流程是,先取某一个复制集成员的物理文件来做为初始化数据,然后剩余的部分用oplog 日志来追,最终达到数据一致性。

      (1)取某一个复制集成员的物理文件来作为初始化数据

        [root@localhost~]#scp-r/data/data/r3/data/data/r4
      1. [root@localhost~]#echo"thisisrs1supersecretkey">/data/key/r4
      2. [root@localhost~]#chmod600/data/key/r4
      (2)在取完物理文件后,在c1集中插入一条新文档,用于最后验证此更新也同步了
        rs1:PRIMARY>db.c1.find()
      1. rs1:PRIMARY>db.c1.insert({age:20})
      2. rs1:PRIMARY>db.c1.find()
      3. {"_id":ObjectId("4fc760d2383ede1dce14ef86"),"age":10}
      4. {"_id":ObjectId("4fc7748f479e007bde6644ef"),"age":20}
      5. rs1:PRIMARY>
      (3)启用28014这个端口给新的节点
        /Apps/mongo/bin/mongod--replSetrs1--keyFile/data/key/r4--fork--port28014--dbpath
      1. /data/data/r4--logpath=/data/log/r4.log--logappend--fastsync
      (4)添加28014节点
        rs1:PRIMARY>rs.add("localhost:28014")
      1. (5)验证数据已经同步过来
          [root@localhostdata]#/Apps/mongo/bin/mongo-port28014
        1. connectingto:127.0.0.1:28014/test
        2. {"_id":ObjectId("4fc7748f479e007bde6644ef"),"age":20}
        3. rs1:SECONDARY
        原文链接:https://www.f2er.com/nosql/204151.html

        猜你在找的NoSQL相关文章