mongodb 如何检查次服务器是否已同步

dced5bon  于 2023-04-20  发布在  Go
关注(0)|答案(5)|浏览(132)

有一个副本有三个成员(主、次、次),假设其中一个次节点宕机一天,将次节点返回副本后,我如何查找,是否已同步
我在测试环境中这样做了,但无法从rs.status()db.printReplicationInfo()中找到有用的数据。
db.printReplicationInfo()中有“log length start to end”.但是默认情况下它很大,并且在secondary关闭时会增长.

6ie5vjzr

6ie5vjzr1#

注意:请务必查看arcseldon提供的the answer,以获得用户友好的等效版本。

您可以使用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
}

如果其中一个辅助服务器未同步,则相同副本集的输出为rs.status()。首先,您将看到hostname:27003optimeoptimeDate与主服务器不同,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创建了辅助服务器,则optimeoptimeDate可以不同,但stateStrlastHeartbeatMessage将指示是否存在一些延迟。

bogh5gae

bogh5gae2#

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

正如您所看到的,很容易判断副本集中节点之间的同步是否正常。

eiee3dmh

eiee3dmh3#

@arcseldon给出了正确的答案,但是,rs.printSlaveReplicationInfo()将很快被删除
rs 0: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
sr4lhrrt

sr4lhrrt4#

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

gzszwxb45#

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}}

相关问题