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()
响应字段的意义及其对应值的整理:
字段 | 意义 | 示例值 |
| 副本集的名称 |
|
| 响应生成的时间 |
|
| 当前节点的状态(1: PRIMARY, 2: SECONDARY, 3: RECOVERING等): 常见的状态包括:
|
|
| 当前选举周期 |
|
| 最近一次选举的原因 |
|
| 副本集成员的详细信息 | 数组,包含各个节点的信息 |
| 节点的健康状态(1: 健康, 0: 不健康) |
|
| 节点的状态描述(如 PRIMARY, SECONDARY, ROLLBACK等) |
|
| 节点的运行时间(秒) |
|
| 最近一次操作的时间戳 |
|
| 最近一次持久化操作的时间戳 |
|
| 最近一次操作的日期 |
|
| 最近一次持久化操作的日期 |
|
| 最近一次心跳信号的时间 |
|
| 最近一次接收到心跳信号的时间 |
|
| 节点的延迟(毫秒) |
|
| 最近一次心跳的消息 |
|
| 当前节点正在同步的目标节点 |
|
| 当前节点的同步源主机 |
|
| 当前节点的同步源ID |
|
| 额外的信息消息 |
|
| 配置版本号 |
|
| 集群时间信息 | 包含 |
| 最近一次操作的时间 |
|
使用场景
- 故障排查: 当副本集出现问题时,使用
rs.status()
可以快速定位故障节点。 - 性能监控: 定期检查副本集状态,以确保所有节点正常运行并及时发现性能瓶颈。
- 维护操作: 在进行维护或升级操作前,确认副本集的健康状况。
- 选举监控: 监控选举过程,确保主节点的选举和切换正常进行。