Mongodb – How to make a replica slave member to be primary member

mongodb

I have setup a shard cluster but there is some errors on my config server replica. Below is my configure server replica status:

cnf-serv:SECONDARY> rs.status()
{
    "set" : "cnf-serv",
    "date" : ISODate("2017-08-21T01:28:00.926Z"),
    "myState" : 2,
    "term" : NumberLong(1),
    "configsvr" : true,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1502320369, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1502320369, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.19.0.13:27017",
            "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("2017-08-21T01:28:00.831Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "Connection refused",
            "configVersion" : -1
        },
        {
            "_id" : 1,
            "name" : "172.19.0.4:27017",
            "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("2017-08-21T01:28:00.831Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "Connection refused",
            "configVersion" : -1
        },
        {
            "_id" : 2,
            "name" : "172.19.0.7:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 119601,
            "optime" : {
                "ts" : Timestamp(1502320369, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-08-09T23:12:49Z"),
            "configVersion" : 185982,
            "self" : true
        }
    ],
    "ok" : 1
}

I can see from above command two replica members are not reachable and the only reachable member is a secondary. So I need to make the secondary member to be primary in order to make the config replica works. I tried below command in secondary but not works:

cnf-serv:SECONDARY> rs.slaveOk()
cnf-serv:SECONDARY> var cfg = rs.config();
cnf-serv:SECONDARY> cfg.members[0].priority=0.5
0.5
cnf-serv:SECONDARY> cfg.members[1].priority=0.5
0.5
cnf-serv:SECONDARY> cfg.members[2].priority=1
1
cnf-serv:SECONDARY> rs.reconfig(cfg, {force: true})
{ "ok" : 1 }

after making this change the secondary is still secondary. It didn't become the primary. How can I fix this replica set issue?

Best Answer

Your problem is that one node of three cannot be "majority". If you remove those two nodes from setup, that one can become primary. Altho better solution is to get those two other nodes to response.

That error message tells that this node (where you gave rs.status() -command) cannot connect to other two nodes. I guess that there is a firewall between and there is no hole for port 27017.

As always, the best source for the problem solving is mongodb.log file, where program tell you what is wrong with setup.