Table of Contents

Overview

Like any other DB, Mongo rises the quesion, what happens if the primary dies :) Well, like many other DBs again, failover occurs. In this case, the next database (elected or not) will become primary as follows:

Force shutdown Primary

testset:PRIMARY> db.shutdownServer()
2018-03-24T23:31:32.017+0100 DBClientCursor::init call() failed
server should be down...
2018-03-24T23:31:32.018+0100 trying reconnect to 127.0.0.1:9005 (127.0.0.1) failed
2018-03-24T23:31:32.018+0100 reconnect 127.0.0.1:9005 (127.0.0.1) ok
2018-03-24T23:31:32.020+0100 Socket recv() errno:104 Connection reset by peer 127.0.0.1:9005
2018-03-24T23:31:32.020+0100 SocketException: remote: 127.0.0.1:9005 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:9005]
2018-03-24T23:31:32.020+0100 DBClientCursor::init call() failed
2018-03-24T23:31:32.022+0100 trying reconnect to 127.0.0.1:9005 (127.0.0.1) failed
2018-03-24T23:31:32.023+0100 warning: Failed to connect to 127.0.0.1:9005, reason: errno:111 Connection refused
2018-03-24T23:31:32.023+0100 reconnect 127.0.0.1:9005 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:9005 (127.0.0.1), connection attempt failed
>

After I shutdown the primary, the primary role when to the other possible database, in order case: lparbmongo:

Failover

testset:SECONDARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T22:32:09Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:32:08Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:31:45Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "**PRIMARY**",
                        "uptime" : 3432,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "electionTime" : Timestamp(1521930710, 1),
                        "electionDate" : ISODate("2018-03-24T22:31:50Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "lparcmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 708,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:32:07Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:32:09Z"),
                        "pingMs" : 0,
                        "lastHeartbeatMessage" : "syncing to: lparbmongo:9005",
                        "syncingTo" : "lparbmongo:9005"
                }
        ],
        "ok" : 1
}

In case the old primary is started again, it will re-join the replica set as SECONDARY (e.g. WAY EASIER THAN ORACLE :D )

Reinstate the old-primary

testset:SECONDARY> rs.status()
{
        "set" : "testset",
        "date" : ISODate("2018-03-24T22:37:09Z"),
        "myState" : 2,
        "syncingTo" : "lparbmongo:9005",
        "members" : [
                {
                        "_id" : 0,
                        "name" : "lparamongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 13,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "infoMessage" : "syncing to: lparbmongo:9005",
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "lparbmongo:9005",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 12,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:37:09Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:37:09Z"),
                        "pingMs" : 0,
                        "electionTime" : Timestamp(1521930710, 1),
                        "electionDate" : ISODate("2018-03-24T22:31:50Z")
                },
                {
                        "_id" : 2,
                        "name" : "lparcmongo:9005",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 12,
                        "optime" : Timestamp(1521930007, 1),
                        "optimeDate" : ISODate("2018-03-24T22:20:07Z"),
                        "lastHeartbeat" : ISODate("2018-03-24T22:37:09Z"),
                        "lastHeartbeatRecv" : ISODate("2018-03-24T22:37:09Z"),
                        "pingMs" : 0,
                        "syncingTo" : "lparbmongo:9005"
                }
        ],
        "ok" : 1
}