转载请注明出处:

rs.status()
命令用于获取MongoDB副本集的状态信息。它提供了关于副本集中各个节点的详细信息,包括节点的健康状况、角色、选举状态等。

以下是查看一个mongo集群状态返回的参数:

rs0:PRIMARY>rs.status()
{
"set" : "rs0","date" : ISODate("2024-09-14T06:44:36.882Z"),"myState" : 1,"term" : NumberLong(510),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"heartbeatIntervalMillis" : NumberLong(2000),"majorityVoteCount" : 2,"writeMajorityCount" : 2,"optimes": {"lastCommittedOpTime": {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),"appliedOpTime": {"ts" : Timestamp(1726296270, 1),"t" : NumberLong(510)
},
"durableOpTime": {"ts" : Timestamp(1726296270, 1),"t" : NumberLong(510)
},
"lastAppliedWallTime" : ISODate("2024-09-14T06:44:30.859Z"),"lastDurableWallTime" : ISODate("2024-09-14T06:44:30.859Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1725300368, 3),"lastStableCheckpointTimestamp" : Timestamp(1725300368, 3),"electionCandidateMetrics": {"lastElectionReason" : "electionTimeout","lastElectionDate" : ISODate("2024-09-14T06:28:20.630Z"),"electionTerm" : NumberLong(510),"lastCommittedOpTimeAtElection": {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection": {"ts" : Timestamp(1726284227, 1),"t" : NumberLong(509)
},
"numVotesNeeded" : 2,"priorityAtElection" : 2,"electionTimeoutMillis" : NumberLong(10000),"numCatchUpOps" : NumberLong(0),"newTermStartDate" : ISODate("2024-09-14T06:28:20.830Z")
},
"members": [
{
"_id" : 0,"name" : "mongo1:27017","health" : 1,"state" : 9,"stateStr" : "ROLLBACK","uptime" : 987,"optime": {"ts" : Timestamp(1726197065, 1),"t" : NumberLong(505)
},
"optimeDurable": {"ts" : Timestamp(1726197065, 1),"t" : NumberLong(505)
},
"optimeDate" : ISODate("2024-09-13T03:11:05Z"),"optimeDurableDate" : ISODate("2024-09-13T03:11:05Z"),"lastHeartbeat" : ISODate("2024-09-14T06:44:35.841Z"),"lastHeartbeatRecv" : ISODate("2024-09-14T06:44:36.665Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncingTo" : "mongo2:27017","syncSourceHost" : "mongo2:27017","syncSourceId" : 1,"infoMessage" : "","configVersion" : 1950478},
{
"_id" : 1,"name" : "mongo2:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 990,"optime": {"ts" : Timestamp(1726296270, 1),"t" : NumberLong(510)
},
"optimeDate" : ISODate("2024-09-14T06:44:30Z"),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","electionTime" : Timestamp(1726295300, 1),"electionDate" : ISODate("2024-09-14T06:28:20Z"),"configVersion" : 1950478,"self" : true,"lastHeartbeatMessage" : ""},
{
"_id" : 2,"name" : "mongo3:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 987,"optime": {"ts" : Timestamp(1726197065, 1),"t" : NumberLong(505)
},
"optimeDurable": {"ts" : Timestamp(1726197065, 1),"t" : NumberLong(505)
},
"optimeDate" : ISODate("2024-09-13T03:11:05Z"),"optimeDurableDate" : ISODate("2024-09-13T03:11:05Z"),"lastHeartbeat" : ISODate("2024-09-14T06:44:34.930Z"),"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),"pingMs" : NumberLong(1),"lastHeartbeatMessage" : "","syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","configVersion" : 1829326}
],
"ok" : 1,"$clusterTime": {"clusterTime" : Timestamp(1726296270, 1),"signature": {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1726296270, 1)
}
rs0:PRIMARY
>

以下是
rs.status()
响应字段的意义及其对应值的整理:

字段 意义 示例值
set 副本集的名称 "rs0"
date 响应生成的时间 ISODate("2024-09-14T06:44:36Z")
myState 当前节点的状态(1: PRIMARY, 2: SECONDARY, 3: RECOVERING等):

常见的状态包括:


  • PRIMARY (1): 当前节点是主节点,负责处理所有写入操作。
  • SECONDARY (2): 当前节点是从节点,复制主节点的数据并提供读取服务。
  • ARBITER (7): 当前节点是仲裁者,不存储数据,仅参与选举过程。
  • OTHER (8): 当前节点的状态不属于上述任何一种,通常是由于配置或网络问题。
  • RECOVERING (9): 当前节点正在恢复中,通常是从不健康状态恢复。
  • DOWN (10): 当前节点不可用,可能是由于网络问题或故障。
  • STARTUP (11): 当前节点正在启动,尚未完成初始化。
  • STARTUP2 (12): 当前节点在启动的第二阶段,正在进行数据同步。
  • UNKNOWN (13): 当前节点的状态未知,可能是由于网络分区或其他问题。
1
term 当前选举周期 510
lastElectionReason 最近一次选举的原因 "electionTimeout"
members 副本集成员的详细信息 数组,包含各个节点的信息
health 节点的健康状态(1: 健康, 0: 不健康) 1
stateStr 节点的状态描述(如 PRIMARY, SECONDARY, ROLLBACK等) "SECONDARY"
uptime 节点的运行时间(秒) 987
optime 最近一次操作的时间戳 Timestamp(1726197065, 1)
optimeDurable 最近一次持久化操作的时间戳 Timestamp(1726197065, 1)
optimeDate 最近一次操作的日期 ISODate("2024-09-13T03:11:05Z")
optimeDurableDate 最近一次持久化操作的日期 ISODate("2024-09-13T03:11:05Z")
lastHeartbeat 最近一次心跳信号的时间 ISODate("2024-09-14T06:44:34.930Z")
lastHeartbeatRecv 最近一次接收到心跳信号的时间 ISODate("1970-01-01T00:00:00Z")
pingMs 节点的延迟(毫秒) NumberLong(1)
lastHeartbeatMessage 最近一次心跳的消息 ""
syncingTo 当前节点正在同步的目标节点 ""
syncSourceHost 当前节点的同步源主机 ""
syncSourceId 当前节点的同步源ID -1
infoMessage 额外的信息消息 ""
configVersion 配置版本号 1829326
$clusterTime 集群时间信息 包含
clusterTime

signature
operationTime 最近一次操作的时间 Timestamp(1726296270, 1)

使用场景

  • 故障排查: 当副本集出现问题时,使用
    rs.status()
    可以快速定位故障节点。
  • 性能监控: 定期检查副本集状态,以确保所有节点正常运行并及时发现性能瓶颈。
  • 维护操作: 在进行维护或升级操作前,确认副本集的健康状况。
  • 选举监控: 监控选举过程,确保主节点的选举和切换正常进行。

标签: none

添加新评论