How it works...
Forcing a primary node to step down is a fairly straightforward process. As shown in steps 1 and step 2, we just need to log in to the primary node and run the rs.stepDown() command. This forces the node to become secondary and initiates an election in the replica set. Within a few seconds (or less), one of the secondary nodes would be elected as the new primary node. In this recipe, we assume that node 3 got elected as the new primary node.
In step 3, we run another neat little helper, rs.isMaster(), and look for the value of the ismaster key. If its value is set to true, then the current node is a primary. Otherwise, it is a secondary.
For the next part, we work towards converting a particular secondary node to a primary ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access