Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check secondary is synced now or not

there is the replica with three member (primary,secondary,secondary). Suppose one of secondaries down for a day, after return secondary back to replica how can i find, is it synced yet or not?

I did that in testing environment, But couldn't find useful data from rs.status() and db.printReplicationInfo().

there is "log length start to end" in db.printReplicationInfo(). but it's big time by default and grows when secondary is down.

like image 543
irmorteza Avatar asked Oct 30 '13 06:10

irmorteza


People also ask

How do I know if MongoDB is replica?

To check the size of the oplog for a given replica set member, connect to the member in mongosh and run the rs. printReplicationInfo() method.

What is optime in MongoDB?

The optimes field holds a document that contains optimes used to inspect replication progress. Starting in MongoDB 4.2, the optimes include the corresponding ISODate-formatted date strings for the various optimes. Each optime value is a document that contains: ts , the Timestamp of the operation.

Where is replication lag in MongoDB?

To check on the current replication rate, use this command in the mongo shell that is connected to the Primary Node: rs. printSlaveReplicationInfo(). This will return the 'syncedTo' value for each member and when the most recent oplog entry was written to the secondary.


2 Answers

Note: Be sure to check the answer provided by arcseldon for an user friendly equivalent.

You can use output of rs.status(). If secondary is synced and wasn't created with slaveDelay option then optime and optimeDate of secondary should be equal or close (if there are current operations) to those of primary. In that case stateStr should be equal to SECONDARY. So if secondary is synced you should see output similar to this (one member has been removed from output for clarity):

 {     "set" : "rs0",     "date" : ISODate("2013-11-08T14:58:49Z"),     "myState" : 1,     "members" : [         {             "_id" : 0,             "name" : "hostname:27001",             "health" : 1,             "state" : 1,             "stateStr" : "PRIMARY",             "uptime" : 155,             "optime" : Timestamp(1383915748, 1),             "optimeDate" : ISODate("2013-11-08T13:02:28Z"),             "self" : true         },          {             "_id" : 2,             "name" : "hostname:27003",             "health" : 0,             "state" : 8,             "stateStr" : "SECONDARY",             "uptime" : 0,             "optime" : Timestamp(1383915748, 1),             "optimeDate" : ISODate("2013-11-08T13:02:28Z"),             "lastHeartbeat" : ISODate("2013-11-08T14:58:48Z"),             "lastHeartbeatRecv" : ISODate("2013-11-08T14:58:42Z"),             "pingMs" : 0,             "syncingTo" : "hostname:27001"         }     ],     "ok" : 1 } 

Here you have output of rs.status() for the same replica set if one of secondaries is not synced. First of all you'll see that optime and optimeDate for hostname:27003 differs from primary, stateStr is set to RECOVERING and there is appropriate lastHeartbeatMessage.

{     "set" : "rs0",     "date" : ISODate("2013-11-08T15:01:34Z"),     "myState" : 1,     "members" : [         {             "_id" : 0,             "name" : "hostname:27001",             "health" : 1,             "state" : 1,             "stateStr" : "PRIMARY",             "uptime" : 320,             "optime" : Timestamp(1383922858, 767),             "optimeDate" : ISODate("2013-11-08T15:00:58Z"),             "self" : true         },          {             "_id" : 2,             "name" : "hostname:27003",             "health" : 1,             "state" : 3,             "stateStr" : "RECOVERING",             "uptime" : 14,             "optime" : Timestamp(1383915748, 1),             "optimeDate" : ISODate("2013-11-08T13:02:28Z"),             "lastHeartbeat" : ISODate("2013-11-08T15:01:34Z"),             "lastHeartbeatRecv" : ISODate("2013-11-08T15:01:34Z"),             "pingMs" : 0,             "lastHeartbeatMessage" : "still syncing, not yet to minValid optime 527cfc90:19c4",             "syncingTo" : "hostname:27001"         }     ],     "ok" : 1 } 

If secondary has been created with slaveDelay then optime and optimeDate can be different but stateStr and lastHeartbeatMessage will indicate if there is some lag.

like image 95
zero323 Avatar answered Sep 19 '22 06:09

zero323


Update 13th February 2017

Agree with the accepted answer that rs.status() offers adequate information and is an easy command to remember. However, (personally using Mongo 3 now), do also really like the convenience and readability of rs.printSlaveReplicationInfo().

It gives an output something like:

rs.printSlaveReplicationInfo()

source: node-2:27017
    syncedTo: Mon Feb 13 2017 06:15:17 GMT-0500 (EST)
    0 secs (0 hrs) behind the primary
source: node-3:27017
    syncedTo: Mon Feb 13 2017 06:15:16 GMT-0500 (EST)
    1 secs (0 hrs) behind the primary

As you can see, it is easy to get a sense of whether the synchronization between the nodes in the replica set is healthy or not.

like image 30
arcseldon Avatar answered Sep 19 '22 06:09

arcseldon