副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验)
步骤:
1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章 https://www.cnblogs.com/anxbb/p/9482304.html)
2.运行 rs.status(),查看状态
{ "set" : "haibin", "date" : ISODate("2018-08-16T03:18:05.461Z"), "myState" : 1, "term" : NumberLong(9), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) }, "appliedOpTime" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) }, "durableOpTime" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) } }, "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 1.0, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 788, "optime" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) }, "optimeDate" : ISODate("2018-08-16T03:18:03.000Z"), "electionTime" : Timestamp(1534389270, 1), "electionDate" : ISODate("2018-08-16T03:14:30.000Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1.0, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 6, "optime" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) }, "optimeDurable" : { "ts" : Timestamp(1534389483, 1), "t" : NumberLong(9) }, "optimeDate" : ISODate("2018-08-16T03:18:03.000Z"), "optimeDurableDate" : ISODate("2018-08-16T03:18:03.000Z"), "lastHeartbeat" : ISODate("2018-08-16T03:18:04.903Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:18:03.874Z"), "pingMs" : NumberLong(0), "syncingTo" : "127.0.0.1:27017", "configVersion" : 2 }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1.0, "state" : 7, "stateStr" : "ARBITER", "uptime" : 221, "lastHeartbeat" : ISODate("2018-08-16T03:18:04.603Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:18:04.103Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1.0 }
3.试着关闭 27017这个端口 然后在运行 rs.status(),结果如下
{ "set" : "haibin", "date" : ISODate("2018-08-16T03:20:21.708Z"), "myState" : 1, "term" : NumberLong(10), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534389603, 1), "t" : NumberLong(9) }, "appliedOpTime" : { "ts" : Timestamp(1534389616, 1), "t" : NumberLong(10) }, "durableOpTime" : { "ts" : Timestamp(1534389616, 1), "t" : NumberLong(10) } }, "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00.000Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"), "lastHeartbeat" : ISODate("2018-08-16T03:20:21.009Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1.0, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 144, "optime" : { "ts" : Timestamp(1534389616, 1), "t" : NumberLong(10) }, "optimeDate" : ISODate("2018-08-16T03:20:16.000Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1534389614, 1), "electionDate" : ISODate("2018-08-16T03:20:14.000Z"), "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1.0, "state" : 7, "stateStr" : "ARBITER", "uptime" : 142, "lastHeartbeat" : ISODate("2018-08-16T03:20:20.912Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:20:20.345Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1.0 }
我们发现现在端口 27018 变成了主节点,27017 为断开状态
4.试着关闭 27019 这个端口 然后在运行 rs.status(),结果如下
/* 1 */ { "set" : "haibin", "date" : ISODate("2018-08-16T03:21:34.102Z"), "myState" : 1, "term" : NumberLong(10), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534389603, 1), "t" : NumberLong(9) }, "appliedOpTime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) }, "durableOpTime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) } }, "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00.000Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"), "lastHeartbeat" : ISODate("2018-08-16T03:21:31.166Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1.0, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 217, "optime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) }, "optimeDate" : ISODate("2018-08-16T03:21:26.000Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1534389614, 1), "electionDate" : ISODate("2018-08-16T03:20:14.000Z"), "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "lastHeartbeat" : ISODate("2018-08-16T03:21:31.966Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:21:25.358Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 } ], "ok" : 1.0 }
发现现在 27018这个端口还是主节点,那么我们过一会在运行 rs.tatus(),在看下结果
/* 1 */ { "set" : "haibin", "date" : ISODate("2018-08-16T03:23:20.966Z"), "myState" : 2, "term" : NumberLong(10), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534389603, 1), "t" : NumberLong(9) }, "appliedOpTime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) }, "durableOpTime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) } }, "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00.000Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"), "lastHeartbeat" : ISODate("2018-08-16T03:23:20.305Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1.0, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 323, "optime" : { "ts" : Timestamp(1534389686, 1), "t" : NumberLong(10) }, "optimeDate" : ISODate("2018-08-16T03:21:26.000Z"), "infoMessage" : "could not find member to sync from", "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "lastHeartbeat" : ISODate("2018-08-16T03:23:13.088Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:21:25.358Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 } ], "ok" : 1.0 }
发现此时的27018 已经不是主节点了,变成了备份节点。
经过上面的实验可以让大家知道,副本集的大多数概念,一定要记住。
最后我们再次重启一个节点(27019)过一段时间按发现 27018又变回了主节点
/* 1 */ { "set" : "haibin", "date" : ISODate("2018-08-16T03:25:34.676Z"), "myState" : 1, "term" : NumberLong(11), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534389603, 1), "t" : NumberLong(9) }, "appliedOpTime" : { "ts" : Timestamp(1534389931, 1), "t" : NumberLong(11) }, "durableOpTime" : { "ts" : Timestamp(1534389931, 1), "t" : NumberLong(11) } }, "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 0.0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00.000Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"), "lastHeartbeat" : ISODate("2018-08-16T03:25:31.632Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�", "configVersion" : -1 }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1.0, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 457, "optime" : { "ts" : Timestamp(1534389931, 1), "t" : NumberLong(11) }, "optimeDate" : ISODate("2018-08-16T03:25:31.000Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1534389928, 1), "electionDate" : ISODate("2018-08-16T03:25:28.000Z"), "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1.0, "state" : 7, "stateStr" : "ARBITER", "uptime" : 7, "lastHeartbeat" : ISODate("2018-08-16T03:25:34.640Z"), "lastHeartbeatRecv" : ISODate("2018-08-16T03:25:32.362Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1.0 }
截取书上的文字说明,说明下为什么要使用这样的规则: