6

I looked into answers but nothing works for me in mongo 2.6.11 cluster 'nms' (members nms01m and nms02) below. I want my current secondary nms01m to become primary, and now nms02 is primary instead of nms02 (after nms01m failure last night). I tried stopping nsm02, restarting nms01m , changing priority, rs.reconfig(cfg, {force : true}) but nothing helps. It is always a struggle for me

nms:SECONDARY> rs.conf()
{
        "_id" : "nms",
        "version" : 193970,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "nms02:27017",
                        "priority" : 2
                },
                {
                        "_id" : 1,
                        "host" : "nms01m:27017"
                }
        ]
}
5
  • 2
    In the details you provided, nms02 is listed with priority=2, which is why it becomes primary when it joins the replicaset. Also, do you have a reason to prefer one member over another as PRIMARY? Commented Dec 17, 2015 at 13:40
  • 1
    I added that priority=2 to make it secondary lol. yes I prefer nms01m as primary now, nms02 only if no nsm01m avail
    – irom
    Commented Dec 17, 2015 at 14:32
  • @irom the higher the priority value, the more eligibility for a node to become the primary. Apparently, you got it backwards :) docs.mongodb.org/v3.0/reference/command/replSetGetConfig/… Commented Dec 17, 2015 at 14:50
  • Yep, fixed now, thnx
    – irom
    Commented Dec 17, 2015 at 14:59
  • Before fiddling with something like that, you should learn the very basics. A replica set should always consist of an odd number of members. With only two members, you do not have failover capabilities - the remaining node will always revert to secondary if the other fails. Add an arbiter. Commented Dec 19, 2015 at 9:47

2 Answers 2

2

A MongoDB replica set should have at least 3 members in order to help you achieve high availability.

The rule for MongoDB replica set failover (when a new member becomes a primary) is that the MAJORITY of the "surviving" nodes vote for it to become the new primary.

So let's think about this:

  1. If you have a replica set of one member, and that member fails, there is no other node to become primary.
  2. If you have a replica set with 2 members, if one of the members fail, there is only one "surviving" member. The total number of members in the replica is 2 and there is only 1 survivor. Majority means MORE than half, but 1 survivor is just half of the nodes. Since there is no majority, the only survivor node, will stay as a secondary and consequently be read-only until the other node recovers.
  3. If you have a replica set with 3 members, if one of the members fail, there are 2 surviving nodes. If the survivor nodes can "talk" via the network, the election algorithm will ensure that one of them becomes the "new" primary.

If for any reason you are not interested in investing in a 3rd server for your MongoDB replica set, you can consider using an arbiter. The arbiter can be a very small machine, and it requires minimum amounts of CPU and storage. But an arbiter votes in an election and it is counted as one of the nodes.

Look at this link in the MongoDB documentation: [https://docs.mongodb.org/v3.0/core/replication-introduction/#automatic-failover][1]

When a primary does not communicate with the other members of the set for more than 10 seconds, an eligible secondary will hold an election to elect itself the new primary. The first secondary to hold an election and receive a majority of the members’ votes becomes primary.

In a situation where you have 2 "regular" members of a MongoDB replica set, plus an arbiter, in case one of the members fails, the replica and the surviving node, will form a MAJORITY which is needed for the election of a new primary. [https://docs.mongodb.org/v3.0/core/replica-set-arbiter/][1]

4

The reason nms02 becomes Primary when it joins the replicaset is due to having the higher priority setting.

According to the documentation:

Specify higher values to make a member more eligible to become primary, and lower values to make the member less eligible.

snip

A priority of 0 makes it impossible for a member to become primary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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