MongoDB 基礎(八)複製Ⅱ—部署仲裁節點,mongodb

來源:互聯網
上載者:User

MongoDB 基礎(八)複製Ⅱ—部署仲裁節點,mongodb

仲裁者(Arbiter)是複製集中的一個mongodb執行個體,它並不儲存資料。仲裁節點使用最小的資源並且不要求硬體裝置,不能將Arbiter部署在同一個資料集節點中,可以部署在其他應用伺服器或者監視伺服器中,也可部署在單獨的虛擬機器中。為了確保複製集中有奇數的投票成員(包括primary),需要添加仲裁節點做為投票,否則primary不能運行時不會自動切換primary。


一個複製集中可設定50個成員,但只有7個投票成員(包括primary),其餘為非投票成員(Non-Voting Members)。非投票成員是複製集中資料的備份副本,不參與投票,但可以被投票或成為主節點。


結構如下:



上一篇中:http://blog.csdn.net/kk185800961/article/details/45700485

配置了3個節點:

192.168.1.11:27017(primary)
192.168.1.12:27018(secondary)
192.168.1.13:27019(secondary)


當前節點情況:

rs0:SECONDARY> db.isMaster(){"setName" : "rs0","setVersion" : 12,"ismaster" : false,"secondary" : true,"hosts" : ["mongodb11.kk.net:27017","mongodb12.kk.net:27018","mongodb13.kk.net:27019"],"primary" : "mongodb13.kk.net:27019","me" : "mongodb11.kk.net:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 1000,"localTime" : ISODate("2015-05-17T10:10:02.537Z"),"maxWireVersion" : 3,"minWireVersion" : 0,"ok" : 1}

現在在primary中把節點"mongodb13.kk.net:27019" 移除:

rs0:PRIMARY> rs.remove("mongodb13.kk.net:27019")rs0:PRIMARY> rs.conf()
[root@mongodb13 ~]# vi /etc/mongod.conf#replSet=rs0[root@mongodb13 ~]# service mongod restart


刪除後剩餘2個節點如下:

192.168.1.11:27017(primary)
192.168.1.12:27018(secondary)


在192.168.1.11:27017(primary)中刪除服務進程,primary並沒有切換:

[root@redhat11 ~]# ps -ef | grep mongmongod    4592     1  0 20:00 ?        00:00:02 /usr/bin/mongod -f /etc/mongod.confroot      4648  2580  0 20:00 pts/0    00:00:00 mongo mongodb11.kk.net:27017root      4738  2618  0 20:06 pts/1    00:00:00 grep mong[root@redhat11 ~]# [root@redhat11 ~]# kill 4592

在192.168.1.12:27018(secondary)查看結果為:

rs0:SECONDARY> rs.status(){"set" : "rs0","date" : ISODate("2015-05-17T12:02:55.595Z"),"myState" : 2,"members" : [{"_id" : 0,"name" : "mongodb11.kk.net:27017","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"optime" : Timestamp(0, 0),"optimeDate" : ISODate("1970-01-01T00:00:00Z"),"lastHeartbeat" : ISODate("2015-05-17T12:02:53.655Z"),"lastHeartbeatRecv" : ISODate("2015-05-17T12:02:28.462Z"),"pingMs" : 0,"lastHeartbeatMessage" : "Failed attempt to connect to mongodb11.kk.net:27017; couldn't connect to server mongodb11.kk.net:27017 (192.168.1.11), connection attempt failed","configVersion" : -1},{"_id" : 1,"name" : "mongodb12.kk.net:27018","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 726,"optime" : Timestamp(1431860765, 1),"optimeDate" : ISODate("2015-05-17T11:06:05Z"),"infoMessage" : "could not find member to sync from","configVersion" : 13,"self" : true}],"ok" : 1}

啟動192.168.1.11:27017(primary)mongod服務後,發現才自動切換:

192.168.1.11:27017(secondary)
192.168.1.12:27018(primary)


方便測試,將primary改回原來:

192.168.1.11:27017(primary)
192.168.1.12:27018(secondary)


現在將添加一個仲裁節點:192.168.1.13:27019


建立資料檔案目錄:

[root@mongodb13 ~]# mkdir /var/lib/mongo/arbiter
[root@mongodb13 ~]# chown mongod:mongod /var/lib/mongo/arbiter


配置參數檔案:

[root@mongodb13 ~]# vi /etc/mongod.conf

pidfilepath=/var/run/mongodb/mongod.pidlogpath=/var/log/mongodb/mongod.logbind_ip=192.168.1.13dbpath=/var/lib/mongo/arbiterport=27019logappend=falsefork=truereplSet=rs0


接著重啟服務:
[root@mongodb13 ~]# service mongod restart


在192.168.1.11:27017(primary)中添加仲裁節點並查看結果:

rs0:PRIMARY> rs.addArb("mongodb13.kk.net:27019")rs0:PRIMARY> rs0:PRIMARY> db.isMaster(){"setName" : "rs0","setVersion" : 16,"ismaster" : true,"secondary" : false,"hosts" : ["mongodb11.kk.net:27017","mongodb12.kk.net:27018"],"arbiters" : ["mongodb13.kk.net:27019"],"primary" : "mongodb11.kk.net:27017","me" : "mongodb11.kk.net:27017","electionId" : ObjectId("555884dc41ecb76a9ab03676"),"maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 1000,"localTime" : ISODate("2015-05-17T12:31:34.720Z"),"maxWireVersion" : 3,"minWireVersion" : 0,"ok" : 1}rs0:PRIMARY> 

至此,添加完成!~


再次測試,在192.168.1.11:27017(primary)中刪除服務進程:

[root@redhat11 ~]# ps -ef | grep mongmongod    4771     1  0 20:08 ?        00:00:09 /usr/bin/mongod -f /etc/mongod.confroot      4827  2580  0 20:08 pts/0    00:00:00 mongo mongodb11.kk.net:27017root      5255  2618  0 20:33 pts/1    00:00:00 grep mong[root@redhat11 ~]# kill 4771


到192.168.1.12:27018(secondary)查看,primary切換為192.168.1.12 

rs0:SECONDARY> db.isMaster(){"setName" : "rs0","setVersion" : 16,"ismaster" : true,"secondary" : false,"hosts" : ["mongodb11.kk.net:27017","mongodb12.kk.net:27018"],"arbiters" : ["mongodb13.kk.net:27019"],"primary" : "mongodb12.kk.net:27018","me" : "mongodb12.kk.net:27018","electionId" : ObjectId("555889751e2e989b81963adc"),"maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 1000,"localTime" : ISODate("2015-05-17T12:32:08.745Z"),"maxWireVersion" : 3,"minWireVersion" : 0,"ok" : 1}rs0:PRIMARY> 

primary能正常啟動切換了!~


現在看看arbiter,串連到192.168.1.13:27019。

[root@mongodb13 ~]# mongo 192.168.1.13:27019

rs0:ARBITER> rs0:ARBITER> rs.slaveOk();rs0:ARBITER> db.isMaster()

arbiter 最為仲裁者,沒有資料副本儲存在本地,能讀取複製集的資訊。 




參考:

Replication Introduction

Replica Set Elections

Replica Sets with Four or More Members

Add an Arbiter to Replica Set



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.