-->

MongoDb primary replica becomes secondary if secon

2019-09-21 00:54发布

问题:

I have 2 mongo replicas. One is primary and the second one is secondary. If I stop the secondary replica, the primary one becomes secondary and I lost the write permissions.

Here are my configs:

replica:PRIMARY> rs.status()
{
        "set" : "replica",
        "date" : ISODate("2016-02-26T11:27:50.140Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 1,
                        "name" : "192.168.5.44:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 80,
                        "optime" : Timestamp(1456486069, 1),
                        "optimeDate" : ISODate("2016-02-26T11:27:49Z"),
                        "electionTime" : Timestamp(1456485992, 1),
                        "electionDate" : ISODate("2016-02-26T11:26:32Z"),
                        "configVersion" : 82935,
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "192.168.5.34:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 79,
                        "optime" : Timestamp(1456486067, 2),
                        "optimeDate" : ISODate("2016-02-26T11:27:47Z"),
                        "lastHeartbeat" : ISODate("2016-02-26T11:27:48.874Z"),
                        "lastHeartbeatRecv" : ISODate("2016-02-26T11:27:48.930Z"),
                        "pingMs" : 1,
                        "syncingTo" : "192.168.5.44:27017",
                        "configVersion" : 82935
                }
        ],
        "ok" : 1
}

replica:SECONDARY> rs.status()
{
        "set" : "replica",
        "date" : ISODate("2016-02-26T11:21:38.574Z"),
        "myState" : 2,
        "syncingTo" : "192.168.5.44:27017",
        "members" : [
                {
                        "_id" : 1,
                        "name" : "192.168.5.44:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1306,
                        "optime" : Timestamp(1456485695, 3),
                        "optimeDate" : ISODate("2016-02-26T11:21:35Z"),
                        "lastHeartbeat" : ISODate("2016-02-26T11:21:36.602Z"),
                        "lastHeartbeatRecv" : ISODate("2016-02-26T11:21:37.412Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1456484393, 1),
                        "electionDate" : ISODate("2016-02-26T10:59:53Z"),
                        "configVersion" : 82935
                },
                {
                        "_id" : 2,
                        "name" : "192.168.5.34:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1306,
                        "optime" : Timestamp(1456485697, 1),
                        "optimeDate" : ISODate("2016-02-26T11:21:37Z"),
                        "syncingTo" : "192.168.5.44:27017",
                        "configVersion" : 82935,
                        "self" : true
                }
        ],
        "ok" : 1
}
replica:SECONDARY> cfg = rs.conf()
{
        "_id" : "replica",
        "version" : 82935,
        "members" : [
                {
                        "_id" : 1,
                        "host" : "192.168.5.44:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 2,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "192.168.5.34:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatTimeoutSecs" : 10,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                }
        }
}

Even if I shutdown the primary replica, the secondary won't become primary.

If you need any other details notify me. Thank you for help!

回答1:

MongoDB need the majority of members to accomplish an election. In a 2 members replication set, both member must be available to reach the majority. If one is down, another cannot be elected to primary.