3

There is this question here:

How to force a mongod to become primary in a replica set?

But it was not the answer I was looking for.

and there is the question below, which could be related but it is not the same.

How to force a delayed member to become primary in case of failure in mongodb

and this question which is nearly a duplicate, but deals with version 2.6.11 and mine is version 3.0 and 3.2. Plus I don't want to use force : 1 unless I need to.

Force a Member to Become Primary in mongodb

I have been struggling to put in practice the majority described in the answer there, maybe with an example or clarification, therefore this question.

I had my replication working fine, 3 nodes, node1 is the primary.

when I run the following command

//-----------------------------
//check the status of the replication
//-----------------------------
rs.status()

I get this result:

{
    "set" : "Krishna",
    "date" : ISODate("2016-04-26T14:59:40.263Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "jpb01275:37001",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1022,
            "optime" : Timestamp(1461678841, 1),
            "optimeDate" : ISODate("2016-04-26T13:54:01Z"),
            "electionTime" : Timestamp(1461682464, 1),
            "electionDate" : ISODate("2016-04-26T14:54:24Z"),
            "configVersion" : 239068,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "jpb01275:37002",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1022,
            "optime" : Timestamp(1461678841, 1),
            "optimeDate" : ISODate("2016-04-26T13:54:01Z"),
            "lastHeartbeat" : ISODate("2016-04-26T14:59:40.206Z"),
            "lastHeartbeatRecv" : ISODate("2016-04-26T14:59:40.179Z"),
            "pingMs" : 0,
            "configVersion" : 239068
        },
        {
            "_id" : 2,
            "name" : "jpb01275:37003",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1022,
            "optime" : Timestamp(1461678841, 1),
            "optimeDate" : ISODate("2016-04-26T13:54:01Z"),
            "lastHeartbeat" : ISODate("2016-04-26T14:59:40.238Z"),
            "lastHeartbeatRecv" : ISODate("2016-04-26T14:59:39.593Z"),
            "pingMs" : 0,
            "configVersion" : 239068
        }
    ],
    "ok" : 1
}

what happened is that I had to stop node1 (jpb01275:37001) and the node2 became the primary. When I started node1 again, I wanted it to become the primary again.

what I did is

logged into node2 and node3 run the following command on then, one after another:

rs.stepDown()

and the node1 came back as a primary.

I also tried the following on node1, but it did not work for me.

//-----------------------------
// reconfigure the replica set
// because I stopped this server and now it is secondary
// I want it back to primary
//-----------------------------

cfg = rs.conf()

printjson(cfg)

cfg.members = [cfg.members[0] , cfg.members[1] , cfg.members[2]]

rs.reconfig(cfg, {force : true})

what is the correct, safest way to bring back node1 and set it as a primary for this replication?

5

Typically, you don't. Thinking of your nodes in terms of primary or secondary is the wrong way to approach it. Since standard data bearing nodes should have the same dimensions, it is better to think of them as replica set members, one of which gets elected to primary. Since the drivers are replica set aware and know which the current primary is and you are supposed to give more than one server in your connection string like

mongodb://jpb01275:37001,jpb01275:37002/?replicaSet=Krishna

there is no advantage in setting a fixed replica set primary.

If you really have to do it, you need to do an

rs.stepDown(90,30)

on the current primary and repeat that until your chosen node becomes primary, within 90 seconds (the first numeric value set). For details, see the documentation of rs.stepDown().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.