Replication

Mongodb:次要不會成為主要

  • May 30, 2018

我有一個包含 2 個伺服器的副本集,其中包含一個數據和一個仲裁器。rs.status() 顯示所有三個伺服器都已連接。

但是當我停止我的主伺服器時,輔助伺服器仍然是輔助伺服器並且不會成為主伺服器。

這可能是個問題?

rs.status() 在停止主要之前:

r6:SECONDARY> rs.status()
{
   "set" : "r6",
   "date" : ISODate("2018-05-30T08:57:23.195Z"),
   "myState" : 2,
   "term" : NumberLong(5),
   "heartbeatIntervalMillis" : NumberLong(2000),
   "optimes" : {
           "lastCommittedOpTime" : {
                   "ts" : Timestamp(1527669880, 1),
                   "t" : NumberLong(4)
           },
           "appliedOpTime" : {
                   "ts" : Timestamp(1527669880, 1),
                   "t" : NumberLong(4)
           },
           "durableOpTime" : {
                   "ts" : Timestamp(1527669880, 1),
                   "t" : NumberLong(4)
           }
   },
   "members" : [
           {
                   "_id" : 0,
                   "name" : "node14.company.biz:27118",
                   "health" : 1,
                   "state" : 1,
                   "stateStr" : "PRIMARY",
                   "uptime" : 10,
                   "optime" : {
                           "ts" : Timestamp(1527670641, 1),
                           "t" : NumberLong(5)
                   },
                   "optimeDurable" : {
                           "ts" : Timestamp(1527670641, 1),
                           "t" : NumberLong(5)
                   },
                   "optimeDate" : ISODate("2018-05-30T08:57:21Z"),
                   "optimeDurableDate" : ISODate("2018-05-30T08:57:21Z"),
                   "lastHeartbeat" : ISODate("2018-05-30T08:57:22.613Z"),
                   "lastHeartbeatRecv" : ISODate("2018-05-30T08:57:22.230Z"),
                   "pingMs" : NumberLong(0),
                   "electionTime" : Timestamp(1527670640, 1),
                   "electionDate" : ISODate("2018-05-30T08:57:20Z"),
                   "configVersion" : 3
           },
           {
                   "_id" : 1,
                   "name" : "node15:27139",
                   "health" : 1,
                   "state" : 2,
                   "stateStr" : "SECONDARY",
                   "uptime" : 840,
                   "optime" : {
                           "ts" : Timestamp(1527669880, 1),
                           "t" : NumberLong(4)
                   },
                   "optimeDate" : ISODate("2018-05-30T08:44:40Z"),
                   "configVersion" : 3,
                   "self" : true
           },
           {
                   "_id" : 2,
                   "name" : "node16:27159",
                   "health" : 1,
                   "state" : 7,
                   "stateStr" : "ARBITER",
                   "uptime" : 837,
                   "lastHeartbeat" : ISODate("2018-05-30T08:57:22.240Z"),
                   "lastHeartbeatRecv" : ISODate("2018-05-30T08:57:19.752Z"),
                   "pingMs" : NumberLong(0),
                   "configVersion" : 3
           }
   ],
   "ok" : 1
}

rs.status() 停止主要後:

r6:SECONDARY> rs.status()
{
   "set" : "r6",
   "date" : ISODate("2018-05-30T08:58:58.612Z"),
   "myState" : 2,
   "term" : NumberLong(5),
   "heartbeatIntervalMillis" : NumberLong(2000),
   "optimes" : {
           "lastCommittedOpTime" : {
                   "ts" : Timestamp(1527670731, 1),
                   "t" : NumberLong(5)
           },
           "appliedOpTime" : {
                   "ts" : Timestamp(1527670731, 1),
                   "t" : NumberLong(5)
           },
           "durableOpTime" : {
                   "ts" : Timestamp(1527670731, 1),
                   "t" : NumberLong(5)
           }
   },
   "members" : [
           {
                   "_id" : 0,
                   "name" : "node14.company.biz:27118",
                   "health" : 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:00Z"),
                   "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                   "lastHeartbeat" : ISODate("2018-05-30T08:58:55.409Z"),
                   "lastHeartbeatRecv" : ISODate("2018-05-30T08:58:54.257Z"),
                   "pingMs" : NumberLong(0),
                   "lastHeartbeatMessage" : "Connection refused",
                   "configVersion" : -1
           },
           {
                   "_id" : 1,
                   "name" : "node15:27139",
                   "health" : 1,
                   "state" : 2,
                   "stateStr" : "SECONDARY",
                   "uptime" : 935,
                   "optime" : {
                           "ts" : Timestamp(1527670731, 1),
                           "t" : NumberLong(5)
                   },
                   "optimeDate" : ISODate("2018-05-30T08:58:51Z"),
                   "infoMessage" : "could not find member to sync from",
                   "configVersion" : 3,
                   "self" : true
           },
           {
                   "_id" : 2,
                   "name" : "node16:27159",
                   "health" : 1,
                   "state" : 7,
                   "stateStr" : "ARBITER",
                   "uptime" : 932,
                   "lastHeartbeat" : ISODate("2018-05-30T08:58:55.407Z"),
                   "lastHeartbeatRecv" : ISODate("2018-05-30T08:58:54.760Z"),
                   "pingMs" : NumberLong(0),
                   "configVersion" : 3
           }
   ],
   "ok" : 1
}

解決了!

問題是每個主機都知道自己的 DNS 後綴,例如“node08.company.biz”,而我將伺服器和仲裁器添加到沒有此後綴的副本集中(例如“node08”)。

我使用不帶後綴的名稱重新配置了每個副本集,現在它正在工作

引用自:https://serverfault.com/questions/914394