如何检查辅助现在是否同步

问题描述 投票:0回答:6

有三个成员(主要,次要,次要)的副本。 假设其中一个辅助节点宕机了一天,在将辅助节点返回到副本后我怎么能找到,它是否已同步?

我在测试环境中这样做了,但是无法从

rs.status()
db.printReplicationInfo()
中找到有用的数据。

db.printReplicationInfo()
中有“log length start to end”。但默认情况下它很重要,并且在次级关闭时增长。

mongodb replicaset
6个回答
43
投票

注意:一定要检查arcseldon提供的答案以获得用户友好的等价物。

您可以使用

rs.status()
的输出。如果辅助是同步的并且不是使用
slaveDelay
选项创建的,那么辅助的
optime
optimeDate
应该等于或接近(如果有当前操作)主要的。在这种情况下,
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
}

如果其中一个副本未同步,您将在此处为同一副本集输出

rs.status()
。首先你会看到
optime
optimeDate
for
hostname:27003
与 primary 不同,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
}

如果已使用

slaveDelay
创建了次级,则
optime
optimeDate
可以不同,但
stateStr
lastHeartbeatMessage
将指示是否存在延迟。


34
投票

2017 年 2 月 13 日更新

同意接受的答案,即

rs.status()
提供了足够的信息并且是一个容易记住的命令。然而,(个人 现在使用 Mongo 3),也非常喜欢
rs.printSlaveReplicationInfo()
的便利性和可读性。

它给出的输出是这样的:

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

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


2
投票

@arcseldon 给出了正确的答案,但是,rs.printSlaveReplicationInfo() 将很快被删除

rs0:PRIMARY> rs.printSlaveReplicationInfo() 警告:printSlaveReplicationInfo 已弃用,可能会在下一个主要版本中删除。请改用 printSecondaryReplicationInfo。

所以,开始使用 rs.printSecondaryReplicationInfo() 代替

rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: mongo-1.mongo.ns.svc.cluster.local:27017
        syncedTo: Sat Sep 26 2020 01:26:32 GMT+0000 (UTC)
        10 secs (0 hrs) behind the primary 
source: mongo-2.mongo.ns.svc.cluster.local:27017
        syncedTo: Sat Sep 26 2020 01:26:32 GMT+0000 (UTC)
        10 secs (0 hrs) behind the primary 

1
投票

我为 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');
}

0
投票

根据命令更新

rs.printSecondaryReplicationInfo()
:当您的 SECONDARY 处于初始同步状态时,即
STARTUP2
那么输出将是这样的:

shard_03:PRIMARY> db.printSecondaryReplicationInfo()
source: mongo-1.mongo.ns.svc.cluster.local:27017
        syncedTo: Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
        1623062383 secs (450850.66 hrs) behind the primary 

当初始同步可能完成时,它不会给出任何指示。为了得到这个,连接到 SECONDARY 而不是 PRIMARY:

shard_03:STARTUP2> db.printSecondaryReplicationInfo()
source: mongo-2.mongo.ns.svc.cluster.local:27017
        InitialSyncSyncSource: mongo-1.mongo.ns.svc.cluster.local:27017
        InitialSyncRemainingEstimatedDuration: 1 hour(s) 11 minute(s)

哪个更有用。

从本地日志文件你也可以看到进度,看这个例子:

$ tail -f /var/log/mongodb/mongod.log | grep Repl
{"t":{"$date":"2021-06-07T12:44:57.310+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplWriterWorker-15","msg":"progress meter","attr":{"name":"data.sessions.20210603 collection clone progress","done":2541288,"total":14003530,"percent":18,"units":"documents copied"}}
{"t":{"$date":"2021-06-07T12:46:02.478+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplWriterWorker-15","msg":"progress meter","attr":{"name":"data.sessions.20210603 collection clone progress","done":5703094,"total":14003530,"percent":40,"units":"documents copied"}}
{"t":{"$date":"2021-06-07T12:47:11.357+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplWriterWorker-15","msg":"progress meter","attr":{"name":"data.sessions.20210603 collection clone progress","done":9131425,"total":14003530,"percent":65,"units":"documents copied"}}
{"t":{"$date":"2021-06-07T12:48:13.295+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplWriterWorker-15","msg":"progress meter","attr":{"name":"data.sessions.20210603 collection clone progress","done":11761778,"total":14003530,"percent":83,"units":"documents copied"}}
{"t":{"$date":"2021-06-07T12:49:01.000+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":4847100,"total":14003530,"percent":34}}
{"t":{"$date":"2021-06-07T12:49:04.001+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":10169600,"total":14003530,"percent":72}}
{"t":{"$date":"2021-06-07T12:49:05.952+02:00"},"s":"I",  "c":"INDEX",    "id":20685,   "ctx":"ReplCoordExtern-0","msg":"Index build: inserted keys from external sorter into index","attr":{"namespace":"data.sessions.20210603","index":"tsi_1_t0_1_t_1","keysInserted":14003530,"durationMillis":8000}}
{"t":{"$date":"2021-06-07T12:49:09.000+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":6208700,"total":14003530,"percent":44}}
{"t":{"$date":"2021-06-07T12:49:11.977+02:00"},"s":"I",  "c":"INDEX",    "id":20685,   "ctx":"ReplCoordExtern-0","msg":"Index build: inserted keys from external sorter into index","attr":{"namespace":"data.sessions.20210603","index":"si_1","keysInserted":14003530,"durationMillis":5000}}
{"t":{"$date":"2021-06-07T12:49:15.001+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":4498400,"total":14003530,"percent":32}}
{"t":{"$date":"2021-06-07T12:49:18.001+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":11187800,"total":14003530,"percent":79}}
{"t":{"$date":"2021-06-07T12:49:19.557+02:00"},"s":"I",  "c":"INDEX",    "id":20685,   "ctx":"ReplCoordExtern-0","msg":"Index build: inserted keys from external sorter into index","attr":{"namespace":"data.sessions.20210603","index":"tsi_1_si_1","keysInserted":14003530,"durationMillis":7000}}
{"t":{"$date":"2021-06-07T12:49:19.697+02:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"ReplCoordExtern-0","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"data.sessions.20210603","index":"tsi_1_t0_1_t_1","commitTimestamp":{"$timestamp":{"t":1623062959,"i":22000}}}}
{"t":{"$date":"2021-06-07T12:49:19.698+02:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"ReplCoordExtern-0","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"data.sessions.20210603","index":"si_1","commitTimestamp":{"$timestamp":{"t":1623062959,"i":22000}}}}
{"t":{"$date":"2021-06-07T12:49:19.699+02:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"ReplCoordExtern-0","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"data.sessions.20210603","index":"tsi_1_si_1","commitTimestamp":{"$timestamp":{"t":1623062959,"i":22000}}}}
{"t":{"$date":"2021-06-07T12:49:26.001+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":5717400,"total":14003530,"percent":40}}
{"t":{"$date":"2021-06-07T12:49:29.000+02:00"},"s":"I",  "c":"-",        "id":51773,   "ctx":"ReplCoordExtern-0","msg":"progress meter","attr":{"name":"Index Build: inserting keys from external sorter into index","done":12567700,"total":14003530,"percent":89}}
{"t":{"$date":"2021-06-07T12:49:29.618+02:00"},"s":"I",  "c":"INDEX",    "id":20685,   "ctx":"ReplCoordExtern-0","msg":"Index build: inserted keys from external sorter into index","attr":{"namespace":"data.sessions.20210603","index":"_id_","keysInserted":14003530,"durationMillis":9000}}
{"t":{"$date":"2021-06-07T12:49:29.833+02:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"ReplCoordExtern-0","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"data.sessions.20210603","index":"_id_","commitTimestamp":{"$timestamp":{"t":1623062969,"i":7002}}}}
{"t":{"$date":"2021-06-07T12:49:29.839+02:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"ReplCoordExtern-0","msg":"createCollection","attr":{"namespace":"data.sessions.20210601","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"bddc70be-463a-472a-a1e9-bdc5162a13f0"}},"options":{"uuid":{"$uuid":"bddc70be-463a-472a-a1e9-bdc5162a13f0"}}}}
{"t":{"$date":"2021-06-07T12:49:29.846+02:00"},"s":"I",  "c":"INDEX",    "id":20384,   "ctx":"ReplCoordExtern-0","msg":"Index build: starting","attr":{"namespace":"data.sessions.20210601","buildUUID":null,"properties":{"v":2,"key":{"tsi":1.0,"t0":1.0,"t":1.0},"name":"tsi_1_t0_1_t_1"},"method":"Hybrid","maxTemporaryMemoryUsageMB":66}}
{"t":{"$date":"2021-06-07T12:49:29.849+02:00"},"s":"I",  "c":"INDEX",    "id":20384,   "ctx":"ReplCoordExtern-0","msg":"Index build: starting","attr":{"namespace":"data.sessions.20210601","buildUUID":null,"properties":{"v":2,"key":{"si":1.0},"name":"si_1"},"method":"Hybrid","maxTemporaryMemoryUsageMB":66}}
{"t":{"$date":"2021-06-07T12:49:29.853+02:00"},"s":"I",  "c":"INDEX",    "id":20384,   "ctx":"ReplCoordExtern-0","msg":"Index build: starting","attr":{"namespace":"data.sessions.20210601","buildUUID":null,"properties":{"v":2,"key":{"tsi":1.0,"si":1.0},"name":"tsi_1_si_1"},"method":"Hybrid","maxTemporaryMemoryUsageMB":66}}
{"t":{"$date":"2021-06-07T12:49:29.859+02:00"},"s":"I",  "c":"INDEX",    "id":20384,   "ctx":"ReplCoordExtern-0","msg":"Index build: starting","attr":{"namespace":"data.sessions.20210601","buildUUID":null,"properties":{"v":2,"key":{"_id":1},"name":"_id_"},"method":"Hybrid","maxTemporaryMemoryUsageMB":200}}

-1
投票

不完全是你问的,但我还是想分享这个...... 通常你想看到复制的状态。如果它失败了?进展到什么程度? 我推荐这个来自 Alex Bevilacqua 的精彩剧本:

https://www.alexbevi.com/blog/2020/02/13/mongodb-initial-sync-progress-monitoring/

© www.soinside.com 2019 - 2024. All rights reserved.