Table of Contents

Overview

Switchover or Stepdown is a planned change of the primary. Unlike failover, which happens by accident, the switchover can be issued in controlled manner as follows:

Check current primary

testset:PRIMARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-25T18:20:43Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 16563,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "lastHeartbeat" : ISODate("2018-03-25T18:20:42Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:20:42Z"),
                        "pingMs" : 0,
                        "syncingTo" : "lparbmongo:9005"
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005", <- CURRENT PRIMARY
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 16577,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "electionTime" : Timestamp(1521985532, 1),
                        "electionDate" : ISODate("2018-03-25T13:45:32Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "lparcmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 16306,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "lastHeartbeat" : ISODate("2018-03-25T18:20:43Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:20:43Z"),
                        "pingMs" : 0,
                        "syncingTo" : "lparbmongo:9005"
                },
                {
                        "_id" : 3,
                        "name" : "lpardmongo:9005",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 3,
                        "lastHeartbeat" : ISODate("2018-03-25T18:20:42Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:20:43Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}

Switchover

testset:PRIMARY> rs.stepDown()
2018-03-25T20:26:34.397+0200 DBClientCursor::init call() failed
2018-03-25T20:26:34.398+0200 Error: error doing query: failed at src/mongo/shell/query.js:81
2018-03-25T20:26:34.399+0200 trying reconnect to 127.0.0.1:9005 (127.0.0.1) failed
2018-03-25T20:26:34.399+0200 reconnect 127.0.0.1:9005 (127.0.0.1) ok
testset:SECONDARY>
bye
[root@lparbmongo ~]# mongo --port 9005
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:9005/test
testset:SECONDARY>
testset:SECONDARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-25T18:26:51Z"),
        "myState" : 2,
        "syncingTo" : "lparamongo:9005",
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005", <- NEW PRIMARY
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY", 
                        "uptime" : 16931,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "lastHeartbeat" : ISODate("2018-03-25T18:26:50Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:26:50Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1522002400, 1),
                        "electionDate" : ISODate("2018-03-25T18:26:40Z")
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 16945,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "infoMessage" : "syncing to: lparamongo:9005",
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "lparcmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 16674,
                        "optime" : Timestamp(1522002040, 1),
                        "optimeDate" : ISODate("2018-03-25T18:20:40Z"),
                        "lastHeartbeat" : ISODate("2018-03-25T18:26:49Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:26:51Z"),
                        "pingMs" : 0,
                        "lastHeartbeatMessage" : "syncing to: lparbmongo:9005",
                        "syncingTo" : "lparbmongo:9005"
                },
                {
                        "_id" : 3,
                        "name" : "lpardmongo:9005",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 371,
                        "lastHeartbeat" : ISODate("2018-03-25T18:26:50Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-25T18:26:51Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}