在 MongoDB 中,查看副本集成员之间的副本延迟可以通过以下步骤进行:
-
使用
rs.status()
命令:
这个命令提供了副本集的详细状态信息,包括每个成员的延迟情况。在 MongoDB shell 中,你可以执行以下命令:rs.status()
这个命令会返回一个包含副本集所有成员状态的对象。在返回的对象中,每个成员都有一个
optime
字段,显示了该成员最后应用的操作的时间戳。 -
查看
optime
字段:
optime
字段是一个包含两个部分的对象:ts
(timestamp)和t
(term)。ts
是一个 Timestamp 对象,表示最后应用的操作的时间。 -
比较主节点和从节点的
optime
:
主节点的optime
应该总是最新的,而从节点的optime
可能会有所延迟。你可以通过比较主节点和从节点的optime
来估计延迟。 -
使用
rs.printReplicationInfo()
命令:
这个命令提供了一个更易于阅读的副本集状态报告,包括每个成员的延迟信息。在 MongoDB shell 中,执行以下命令:rs.printReplicationInfo()
这个命令会打印出副本集的状态信息,包括每个成员的延迟。
-
监控延迟:
如果你想要持续监控副本集的延迟,你可以定期运行这些命令,或者使用 MongoDB 的监控工具,如 MongoDB Cloud Manager 或第三方监控解决方案。
请注意,副本延迟可能会因为网络延迟、磁盘 I/O、CPU 使用率等因素而变化。如果你发现延迟异常高,可能需要检查副本集成员的性能和配置。
mongo-baa26600-replica0:SECONDARY> rs.printReplicationInfo()
configured oplog size: 10240MB
log length start to end: 370018secs (102.78hrs)
oplog first event time: Fri Sep 06 2024 10:54:09 GMT+0800 (CST)
oplog last event time: Tue Sep 10 2024 17:41:07 GMT+0800 (CST)
now: Tue Sep 10 2024 17:41:14 GMT+0800 (CST)
mongo-baa26600-replica0:SECONDARY> rs.status()
{"set" : "mongo-baa26600-replica0","date" : ISODate("2024-09-10T09:42:31.340Z"),"myState" : 2,"term" : NumberLong(162),"syncSourceHost" : "10.10.98.127:3692","syncSourceId" : 2,"heartbeatIntervalMillis" : NumberLong(2000),"majorityVoteCount" : 2,"writeMajorityCount" : 2,"votingMembersCount" : 3,"writableVotingMembersCount" : 3,"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"lastCommittedWallTime" : ISODate("2024-09-10T09:42:26.430Z"),"readConcernMajorityOpTime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"readConcernMajorityWallTime" : ISODate("2024-09-10T09:42:26.430Z"),"appliedOpTime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"durableOpTime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"lastAppliedWallTime" : ISODate("2024-09-10T09:42:26.430Z"),"lastDurableWallTime" : ISODate("2024-09-10T09:42:26.430Z")},"electionParticipantMetrics" : {"votedForCandidate" : true,"electionTerm" : NumberLong(162),"lastVoteDate" : ISODate("2024-09-10T08:55:47.309Z"),"electionCandidateMemberId" : 2,"voteReason" : "","lastAppliedOpTimeAtElection" : {"ts" : Timestamp(1725958536, 1),"t" : NumberLong(161)},"maxAppliedOpTimeInSet" : {"ts" : Timestamp(1725958536, 1),"t" : NumberLong(161)},"priorityAtElection" : 1,"newTermStartDate" : ISODate("2024-09-10T08:55:47.311Z"),"newTermAppliedDate" : ISODate("2024-09-10T08:55:48.546Z")},"members" : [{"_id" : 0,"name" : "10.10.98.123:7315","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 4539,"optime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"optimeDurable" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"optimeDate" : ISODate("2024-09-10T09:42:26Z"),"optimeDurableDate" : ISODate("2024-09-10T09:42:26Z"),"lastHeartbeat" : ISODate("2024-09-10T09:42:30.831Z"),"lastHeartbeatRecv" : ISODate("2024-09-10T09:42:30.318Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncSourceHost" : "10.10.98.127:3692","syncSourceId" : 2,"infoMessage" : "","configVersion" : 111017,"configTerm" : -1},{"_id" : 1,"name" : "10.10.98.125:4933","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 86381,"optime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"optimeDate" : ISODate("2024-09-10T09:42:26Z"),"syncSourceHost" : "10.10.98.127:3692","syncSourceId" : 2,"infoMessage" : "","configVersion" : 111017,"configTerm" : -1,"self" : true,"lastHeartbeatMessage" : ""},{"_id" : 2,"name" : "10.10.98.127:3692","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 86378,"optime" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"optimeDurable" : {"ts" : Timestamp(1725961346, 1),"t" : NumberLong(162)},"optimeDate" : ISODate("2024-09-10T09:42:26Z"),"optimeDurableDate" : ISODate("2024-09-10T09:42:26Z"),"lastHeartbeat" : ISODate("2024-09-10T09:42:30.830Z"),"lastHeartbeatRecv" : ISODate("2024-09-10T09:42:31.332Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","electionTime" : Timestamp(1725958547, 1),"electionDate" : ISODate("2024-09-10T08:55:47Z"),"configVersion" : 111017,"configTerm" : -1}],"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1725961346, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1725961346, 1)
}