在版本5.0中进行了更改。
db.shutdownServer()Shuts down the current
mongodormongosprocess cleanly and safely. You must issue thedb.shutdownServer()operation against the admin database.db.shutdownServer()采用以下语法:db.shutdownServer({ force: <boolean>, timeoutSecs: <int> }) 该方法采用以下字段:
字段说明可选。指定
true以强制关闭mongod或mongos。强制关闭会中断mongod或mongos上正在进行的任何操作,并可能导致意外行为。You can pause and resume in-progress index builds using
force. Seedb.shutdownServer()on Replica Set Members for more information.可选。
从 MongoDB 5.0 开始,
mongod和mongos进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。进入静默期。
结束所有剩余的数据库操作。
关闭。
对于
mongod从节点或mongos关闭请求,在请求关闭之后进入静默期。静止期由以下各项指定:
timeoutSecs field if a
shutdownordb.shutdownServer()command was run, orshutdownTimeoutMillisForSignaledShutdown服务器参数(如果SIGTERM信号已发送到mongod),或者mongosShutdownTimeoutMillisForSignaledShutdown服务器参数(如果将SIGTERM信号发送到mongos)。
mongod或mongos关闭时,客户端无法新建相关连接。timeoutSecs指定了一个时间段(以秒为单位)。默认为:
从 MongoDB 5.0 开始为 15 秒。
在 MongoDB 5.0 以前的版本中为 10 秒。
mongod按如下方式使用 timeoutSecs:如果当前节点是副本集的主节点,则
mongod会等待 timeoutSecs 字段指定的秒数,以便可选举节点跟上,然后再降级主节点。有关追赶时间的详情,请参阅复制延迟。如果当前节点在退出主节点后处于
SECONDARY状态,则 timeoutSecs 中指定的任何剩余时间将作为静默期,以允许完成现有操作。新操作将发送到其他副本集节点。
从MongoDB5.0 开始,
mongos使用 timeoutSecs 作为静默期,以允许完成现有操作。新操作将发送到其他 节点。在.mongos之前的MongoDB版本中,5 0mongos会立即关闭并且不使用 timeoutSecs。
该操作为 shutdown 命令提供一个包装器。
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
For a mongod started with Authentication on Self-Managed Deployments, you must run db.shutdownServer() over an authenticated connection. See Access Control for more information.
For a mongod started without Authentication on Self-Managed Deployments, you must run db.shutdownServer() from a client connected to the localhost interface. For example, run mongosh with the --host "127.0.0.1" option on the same host machine as the mongod.
db.shutdownServer() (针对副本集节点)
db.shutdownServer() fails if the mongod replica set member is running certain operations such as index builds. You can specify force: true to save index build progress to disk. The mongod recovers the index build when it restarts and continues from the saved checkpoint.
关闭副本集主节点、从节点或 mongos
从 MongoDB 5.0 开始,mongod 和 mongos 进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。
进入静默期。
结束所有剩余的数据库操作。
关闭。
对于 mongod 从节点或 mongos 关闭请求,在请求关闭之后进入静默期。
静止期由以下各项指定:
timeoutSecs field if a
shutdownordb.shutdownServer()command was run, orshutdownTimeoutMillisForSignaledShutdown服务器参数(如果SIGTERM信号已发送到mongod),或者mongosShutdownTimeoutMillisForSignaledShutdown服务器参数(如果将SIGTERM信号发送到mongos)。
mongod 或 mongos 关闭时,客户端无法新建相关连接。
timeoutSecs指定了一个时间段(以秒为单位)。默认为:
从 MongoDB 5.0 开始为 15 秒。
在 MongoDB 5.0 以前的版本中为 10 秒。
mongod 按如下方式使用 timeoutSecs:
如果当前节点是副本集的主节点,则
mongod会等待 timeoutSecs 字段指定的秒数,以便可选举节点跟上,然后再降级主节点。有关追赶时间的详情,请参阅复制延迟。如果当前节点在退出主节点后处于
SECONDARY状态,则 timeoutSecs 中指定的任何剩余时间将作为静默期,以允许完成现有操作。新操作将发送到其他副本集节点。
Starting in MongoDB 5.0, mongos uses timeoutSecs as a quiesce period, which allows existing operations to complete. New operations are sent to other mongos nodes. In MongoDB versions earlier than 5.0, mongos shuts down immediately and does not use timeoutSecs.
警告
强制关闭主节点可能会导致所有尚未复制到从节点的写入操作发生回滚。
访问控制
To run db.shutdownServer() on a mongod enforcing Authentication on Self-Managed Deployments, the authenticated user must have the db.shutdownServer() privilege. For example, a user with the built-in role hostManager has the appropriate permissions.
示例
关闭 mongod
db.getSiblingDB("admin").shutdownServer()
强制关闭 mongod
db.getSiblingDB("admin").shutdownServer({ "force" : true })
关闭超时时间较长的主节点 mongod
db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })