仲裁者(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 }
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
192.168.1.11:27017(primary)
192.168.1.12:27018(secondary)
在192.168.1.11:27017(primary)中刪除服務進程,primary並沒有切換:
[root@redhat11 ~]# ps -ef | grep mong mongod 4592 1 0 20:00 ? 00:00:02 /usr/bin/mongod -f /etc/mongod.conf root 4648 2580 0 20:00 pts/0 00:00:00 mongo mongodb11.kk.net:27017 root 4738 2618 0 20:06 pts/1 00:00:00 grep mong [root@redhat11 ~]# [root@redhat11 ~]# kill 4592
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(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.pid logpath=/var/log/mongodb/mongod.log bind_ip=192.168.1.13 dbpath=/var/lib/mongo/arbiter port=27019 logappend=false fork=true replSet=rs0
接著重啟服務:
在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 mong mongod 4771 1 0 20:08 ? 00:00:09 /usr/bin/mongod -f /etc/mongod.conf root 4827 2580 0 20:08 pts/0 00:00:00 mongo mongodb11.kk.net:27017 root 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>
現在看看arbiter,連接到192.168.1.13:27019。
[root@mongodb13 ~]# mongo 192.168.1.13:27019
rs0:ARBITER> rs0:ARBITER> rs.slaveOk(); rs0:ARBITER> db.isMaster()