DBAtlas · 数据库运维图谱

副本集滚动维护(重启、操作系统补丁、小版本升级)

MongoDB副本集

命令

// 1) Before starting: all members healthy, low lag, and an oplog window longer than the maintenance
rs.printSecondaryReplicationInfo()
// 2) Clean shutdown on the SECONDARY under maintenance (then OS/binary work, restart with the same config):
db.getSiblingDB("admin").shutdownServer({ timeoutSecs: 60 })
// 3) Wait until the member is SECONDARY and lag has closed, then move to the next secondary:
rs.status().members.map(m => ({ host: m.name, state: m.stateStr, health: m.health }))
// 4) When all secondaries are done, on the PRIMARY: hand over to a secondary that catches up within 30 s
rs.stepDown(120, 30)
// 5) The old primary is now a SECONDARY: apply steps 2 and 3 to it

作用

给出逐个成员进入维护的顺序:先 secondary,最后 primary,从而在不让副本集停止服务的情况下完成重启、操作系统补丁或二进制更新。

适用场景

计划内的操作系统/内核更新、小版本升级、mongod 参数变更或磁盘/硬件维护期间。

注意

切勿同时关闭超过一个成员;在 3 成员副本集中失去两个成员就会失去多数派。每处理完一个成员,确认其为 SECONDARY 状态且延迟已追平。stepDown 会造成短暂的写入中断;确保驱动通过 retryable writes 重新连接。若有 arbiter,按相同顺序处理。

在 DBAtlas 中打开 →

相关内容