MongoDB Force a Member to Become Primary in a ReplicaSet

Problem Background

The primary is the only member in the replica set that receives write operations. If for some reason primary may becomes unavailable & an election determines the new primary. Which is not expected in few occasion. Election happens due to hardware issue or may be someone accidentally shutdown the primary which lead to this unwanted selection.On other note you may want one of your secondary to act as primary. You can achieve this forcefully and here is how you do it.

Solution To Problem

Say we have 3 note replica set, 3 physically separated machines.

192.168.1.0 – the current primary.
192.168.1.1 – a secondary.
192.168.1.2 – a secondary .

Our goal is to make 192.168.1.2 primary

From mongo shell, connect to current primary. use the following sequence of operations.

mongo 192.168.1.0:27017
rs.stepDown(120)

Now connect to a secondary. use the following sequence of operations.

mongo 192.168.1.1:27017
rs.freeze(120)

wait for 120 seconds

mongo 192.168.1.2:27017
rs.Status()

You will see machine with IP 192.168.1.2 is now became primary.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s