仲裁者(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()
[[email protected] ~]# vi /etc/mongod.conf #replSet=rs0 [[email protected] ~]# service mongod restart
删除后剩余2个节点如下:
192.168.1.11:27017(primary)
192.168.1.12:27018(secondary)
在192.168.1.11:27017(primary)中删除服务进程,primary并没有切换:
[[email protected] ~]# 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 [[email protected] ~]# [[email protected] ~]# 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
创建数据文件目录:
[[email protected] ~]# mkdir /var/lib/mongo/arbiter
[[email protected] ~]# chown mongod:mongod /var/lib/mongo/arbiter
配置参数文件:
[[email protected] ~]# 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
接着重启服务:
[[email protected] ~]# 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)中删除服务进程:
[[email protected] ~]# 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 [[email protected] ~]# 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。
[[email protected] ~]# mongo 192.168.1.13:27019
rs0:ARBITER> rs0:ARBITER> rs.slaveOk(); rs0:ARBITER> db.isMaster()
arbiter 最为仲裁者,没有数据副本存储在本地,能读取复制集的信息。
参考:
Replica Sets with Four or More Members