Switching between primary and secondary nodes
In this recipe, we will be looking at how to force a primary node to become secondary and vice versa. Let’s get to it then.
Getting ready
We need a three node replica set, preferably without an arbiter. If you have followed the previous recipes, we should have three mongod instances running on the same instance on three different ports, 27017
, 27018
, and 27019
. In order to keep things simple, we will call them node 1, node 2, and node 3 respectively. Here, we assume that node 1 is primary, whereas node 2 and node 3 are secondary. In the first part of this recipe, we will force node 1 to become secondary. Assuming that node 3 gets elected as primary, in the second part of the recipe, we will try to make node 1 primary.
How to do it...
- Connect to the primary member (node 1) of the replica set:
mongo mongodb://192.168.200.200:27017
- Force it to become secondary:
rs.stepDown()
- Confirm the member is now secondary:
rs.isMaster()['ismaster']
- Log in to node 2,...