Stepping down as a primary instance from the replica set

There are times when, for maintenance activity during business hours, we need to take a server out from the replica set, perform the maintenance, and put it back in the replica set. If the server to be worked upon is the primary, we somehow need to step down from the primary member position, conduct a re-election, and ensure that it doesn't get re-elected for a minimum given time frame. After the server becomes a secondary once the step down operation is successful, we can take it out of the replica set, perform the maintenance activity, and put it back in the replica set.

Getting ready

Refer to the Starting multiple instances as part of a replica set recipe in Chapter 1, Installing and Starting the MongoDB Server, for the prerequisites and to know about replica set basics. Set up a simple three-node replica set on your computer as mentioned in the recipe.

How to do it…

Assuming that at this point of time we have a replica set up and running, perform the following steps:

  1. Execute the following command from the shell connected to one of the replica set members and see which instance is currently the primary:
    > rs.status()
    
  2. Connect to that primary instance from the Mongo shell and execute the following command on the shell:
    > rs.stepDown()
    
  3. The shell should reconnect again, and you should see that the instance connected to, which was initially a primary instance, now becomes secondary. Execute the following command from the shell so that a new primary is now re-elected:
    > rs.status()
    
  4. You may now connect to the primary, modify the replica set configuration, and go ahead with the administration on the servers.

How it works…

The steps we saw in the previous section are pretty simple, but there are a couple of interesting things that we will see.

The rs.stepDown() method did not have any parameter. The function can in fact take a numeric value, the number of seconds for which the instance stepped down won't participate in the elections and won't become a primary; the default value for this is 60 seconds.

Another interesting thing to try out is, what if the instance that was asked to step down has a higher priority than other instances? Well, it turns out that the priority doesn't matter when you step down. The instance stepped down will not become primary, no matter what, for the provided number of seconds. However, if the priority is set for the instance stepped down, and it is higher than others, then after the time given to step down elapses, an election will happen, and the instance with the higher priority will become primary again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset