mongodb 如何检查次要现在是否同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19675117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:28:34  来源:igfitidea点击:

How to check secondary is synced now or not

mongodbreplicaset

提问by irmorteza

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().

我在测试环境中这样做了,但无法从rs.status()和 中找到有用的数据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.

中有“日志长度开始到结束” db.printReplicationInfo()。但默认情况下它很重要,并且在二级关闭时增长。

回答by zero323

Note: Be sure to check the answerprovided by arcseldonfor an user friendly equivalent.

:一定要检查答案提供arcseldon一个用户友好的等价物。

You can use output of rs.status(). If secondary is synced and wasn't created with slaveDelayoption then optimeand optimeDateof secondary should be equal or close (if there are current operations) to those of primary. In that case stateStrshould 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):

您可以使用rs.status(). 如果次要同步并且不是使用slaveDelay选项创建的,那么次要的optimeoptimeDate应等于或接近(如果有当前操作)与主要的。在那种情况下stateStr应该等于SECONDARY。因此,如果次要同步,您应该会看到与此类似的输出(为清楚起见,已从输出中删除了一个成员):

 {
    "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 optimeand optimeDatefor hostname:27003differs from primary, stateStr is set to RECOVERINGand there is appropriate lastHeartbeatMessage.

rs.status()如果次要副本之一未同步,您将在此处获得相同副本集的输出。首先你会看到,optimeoptimeDatehostname:27003从小学不同,stateStr设置为RECOVERING有为宜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 slaveDelaythen optimeand optimeDatecan be different but stateStrand lastHeartbeatMessagewill indicate if there is some lag.

如果二次一直与创建slaveDelay那么optimeoptimeDate可以不同,但stateStrlastHeartbeatMessage将表明,如果有一定的滞后性。

回答by arcseldon

Update 13th February 2017

2017 年 2 月 13 日更新

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

同意rs.status()提供足够信息且易于记忆的公认答案。不过,(现在个人在使用 Mongo 3),确实也很喜欢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.

如您所见,很容易了解副本集中节点之间的同步是否健康。

回答by Andrey Hohutkin

I wrote a little script for mongoDB shell. It shows a diff between optime and optimeDate. You can use it instead of comparing manually replica set members.

我为 mongoDB shell 写了一个小脚本。它显示了 optime 和 optimeDate 之间的差异。您可以使用它而不是手动比较副本集成员。

var isMaster = rs.isMaster();
var me = isMaster.me;

if(!isMaster.ismaster && isMaster.secondary)
{
    var status = rs.status();
    var master = isMaster.primary;

    var masterOptime = 0;
    var masterOptimeDate = 0;
    var myOptime = 0;
    var myOptimeDate = 0;

    for(var i = 0 ; i < status.members.length ; i++)
    {
        var member = status.members[i];
        if(member.name == me)
        {
            if(member.stateStr == "SECONDARY") {
                myOptime = member.optime.getTime();
                myOptimeDate = member.optimeDate.getTime();
            }
            else
            {
                print(me + ' is out of sync ' + member.stateStr);
                break;
            }
        }
        else if(member.name == master)
        {
            masterOptime = member.optime.getTime();
            masterOptimeDate = member.optimeDate.getTime();
        }

    }

    if(myOptime && myOptimeDate)
    {
        var optimeDiff = masterOptime - myOptime;
        var optimeDateDiff = masterOptimeDate - myOptimeDate;

        print('optime diff: ' + optimeDiff);
        print('optimeDate diff: ' + optimeDateDiff);
    }

}
else
{
    print(me + ' is not secondary');
}