MongoDB Replication Administration and Troubleshooting



How to connect to mongodb server (specific to the replication enabled database)  

mongo "mongodb://localhost:27018,localhost:27019,localhost:27020/myDB?replicaSet=rs0"


Replica Status

rs.status()


Example

MongoDB rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-07-09T11:57:31.329Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1594295848, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-07-09T11:57:28.322Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1594295848, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-07-09T11:57:28.322Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1594295848, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1594295848, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-07-09T11:57:28.322Z"),
                "lastDurableWallTime" : ISODate("2020-07-09T11:57:28.322Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1594295798, 1),
        "lastStableCheckpointTimestamp" : Timestamp(1594295798, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-07-09T10:55:37.887Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1594292126, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-07-09T10:55:38.027Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-07-09T10:55:38.771Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 4588,
                        "optime" : {
                                "ts" : Timestamp(1594295848, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T11:57:28Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1594292137, 1),
                        "electionDate" : ISODate("2020-07-09T10:55:37Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3724,
                        "optime" : {
                                "ts" : Timestamp(1594295848, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1594295848, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T11:57:28Z"),
                        "optimeDurableDate" : ISODate("2020-07-09T11:57:28Z"),
                        "lastHeartbeat" : ISODate("2020-07-09T11:57:30.828Z"),
                        "lastHeartbeatRecv" : ISODate("2020-07-09T11:57:29.784Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27018",
                        "syncSourceHost" : "localhost:27018",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3724,
                        "optime" : {
                                "ts" : Timestamp(1594295848, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1594295848, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T11:57:28Z"),
                        "optimeDurableDate" : ISODate("2020-07-09T11:57:28Z"),
                        "lastHeartbeat" : ISODate("2020-07-09T11:57:30.820Z"),
                        "lastHeartbeatRecv" : ISODate("2020-07-09T11:57:29.784Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27018",
                        "syncSourceHost" : "localhost:27018",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1594295848, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1594295848, 1)
}


Replication information, like oplog information details 

rs.printReplicationInfo()

Example:

MongoDB rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size:   128MB     ------------------------ configured oplog size 
log length start to end: 3972secs (1.1hrs)
oplog first event time:  Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time) --- first event time
oplog last event time:   Thu Jul 09 2020 17:31:38 GMT+0530 (India Standard Time) --- last event time
now:                     Thu Jul 09 2020 17:31:38 GMT+0530 (India Standard Time)



db.getReplicationInfo()


MongoDB Enterprise rs0:SECONDARY> db.getReplicationInfo()
{
        "logSizeMB" : 128,   ------------------- configured oplog size 
        "usedMB" : 0.06,       ------------------- current used size
        "timeDiff" : 4272,      ------------------- time difference 
        "timeDiffHours" : 1.19,
        "tFirst" : "Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time)",  --- first event time
        "tLast" : "Thu Jul 09 2020 17:36:38 GMT+0530 (India Standard Time)",  --- last event time
        "now" : "Thu Jul 09 2020 17:36:46 GMT+0530 (India Standard Time)"
}


rs.printReplicationInfo()

MongoDB rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size:   128MB
log length start to end: 5212secs (1.45hrs)
oplog first event time:  Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time)
oplog last event time:   Thu Jul 09 2020 17:52:18 GMT+0530 (India Standard Time)
now:                     Thu Jul 09 2020 17:52:20 GMT+0530 (India Standard Time)


db.getReplicationInfo()

MongoDB rs0:PRIMARY> db.getReplicationInfo()
{
        "logSizeMB" : 128,
        "usedMB" : 0.07,
        "timeDiff" : 5232,
        "timeDiffHours" : 1.45,
        "tFirst" : "Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time)",
        "tLast" : "Thu Jul 09 2020 17:52:38 GMT+0530 (India Standard Time)",
        "now" : "Thu Jul 09 2020 17:52:45 GMT+0530 (India Standard Time)"
}

rs.conf()

MongoDB rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27018",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27020",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5f06f799088a2096663c245b")
        }
}

rs.status()

MongoDB rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-07-09T12:23:42.107Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1594297418, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1594297418, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1594297418, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1594297418, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
                "lastDurableWallTime" : ISODate("2020-07-09T12:23:38.436Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1594297418, 1),
        "lastStableCheckpointTimestamp" : Timestamp(1594297418, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-07-09T10:55:37.887Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1594292126, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-07-09T10:55:38.027Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-07-09T10:55:38.771Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 6159,
                        "optime" : {
                                "ts" : Timestamp(1594297418, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T12:23:38Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1594292137, 1),
                        "electionDate" : ISODate("2020-07-09T10:55:37Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5295,
                        "optime" : {
                                "ts" : Timestamp(1594297418, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1594297418, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T12:23:38Z"),
                        "optimeDurableDate" : ISODate("2020-07-09T12:23:38Z"),
                        "lastHeartbeat" : ISODate("2020-07-09T12:23:41.221Z"),
                        "lastHeartbeatRecv" : ISODate("2020-07-09T12:23:40.209Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27018",
                        "syncSourceHost" : "localhost:27018",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5295,
                        "optime" : {
                                "ts" : Timestamp(1594297418, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1594297418, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-07-09T12:23:38Z"),
                        "optimeDurableDate" : ISODate("2020-07-09T12:23:38Z"),
                        "lastHeartbeat" : ISODate("2020-07-09T12:23:41.224Z"),
                        "lastHeartbeatRecv" : ISODate("2020-07-09T12:23:40.200Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27018",
                        "syncSourceHost" : "localhost:27018",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1594297418, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1594297418, 1)
}

rs.isMaster()

MongoDB Enterprise rs0:PRIMARY> rs.isMaster()
{
        "hosts" : [
                "localhost:27018",
                "localhost:27019",
                "localhost:27020"
        ],
        "setName" : "rs0",
        "setVersion" : 1,
        "ismaster" : true,
        "secondary" : false,
        "primary" : "localhost:27018",
        "me" : "localhost:27018",
        "electionId" : ObjectId("7fffffff0000000000000001"),
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1594297498, 1),
                        "t" : NumberLong(1)
                },
                "lastWriteDate" : ISODate("2020-07-09T12:24:58Z"),
                "majorityOpTime" : {
                        "ts" : Timestamp(1594297498, 1),
                        "t" : NumberLong(1)
                },
                "majorityWriteDate" : ISODate("2020-07-09T12:24:58Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 100000,
        "localTime" : ISODate("2020-07-09T12:24:58.854Z"),
        "logicalSessionTimeoutMinutes" : 30,
        "connectionId" : 1,
        "minWireVersion" : 0,
        "maxWireVersion" : 8,
        "readOnly" : false,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1594297498, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1594297498, 1)
}



How to add a new replica set to the existing active replication.

rs.add("server details and port no")

How to remove the existing replica from the active replication.

rs.remove("server details")

How to run the mongod process in the background

Linux

- fork option

ex: mongod --fork --logpath


Windows

net start MongoDB

How to shutdown the server which you are connected to

Linux

db.shutdownServer()

Windows

net stop MongoDB



rs.slaveok()
rs.printSlaveReplicationInfo()


Comments

Popular posts from this blog

Auditing in Oracle database

rs.stepDown() in MongoDB replication

Tuples in Python