Docs 菜单

db.shutdownServer()

5.0 版本中的更改

db.shutdownServer()

Shuts down the current mongod or mongos process cleanly and safely. You must issue the db.shutdownServer() operation against the 管理库.

db.shutdownServer() has this syntax:

db.shutdownServer({
force: <boolean>,
timeoutSecs: <int>
})

The method takes these fields:

字段
说明

可选。指定 true 以强制关闭 mongodmongos。强制关闭会中断 mongodmongos 上正在进行的任何操作,并可能导致意外行为。

您可以使用 force 暂停和恢复正在进行的索引构建。 db.shutdownServer()有关更多信息,请参阅有关副本集节点的

可选。

从 MongoDB 5.0 开始,mongodmongos 进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。

如果一个 mongod 主节点收到关闭请求,则主节点:

  1. 尝试降级到从节点。

    如果降级失败,并且:

    • 运行了 shutdowndb.shutdownServer() 命令,如果字段为 true,mongod 仅继续执行关闭步骤,或者

    • SIGTERM 信号已发送到 mongodmongod 始终继续执行关闭步骤。

  2. 进入静默期。

  3. 结束所有剩余的数据库操作。

  4. 关闭。

对于 mongod 从节点mongos 关闭请求,在请求关闭之后进入静默期。

静止期由以下各项指定:

mongodmongos 关闭时,客户端无法新建相关连接。

timeoutSecs 指定一个时间段(以秒为单位)。默认为:

  • 从 MongoDB 5.0 开始为 15 秒。

  • 在 MongoDB 5.0 以前的版本中为 10 秒。

mongod使用 timeoutSecs,如下所示:

从MongoDB5.0 开始,mongos 使用 timeoutSecs 作为静默期,以允许完成现有操作。新操作将发送到其他mongos 节点。在 5.0 之前的MongoDB版本中, mongos会立即关闭且不使用 timeoutSecs

This operation provides a wrapper around the shutdown command.

此方法可用于以下环境中托管的部署:

For a mongod started with 自主管理部署的身份验证, you must run db.shutdownServer() over an authenticated connection. See 访问控制 for more information.

For a mongod started without 自主管理部署的身份验证, 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() 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.

从 MongoDB 5.0 开始,mongodmongos 进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。

如果一个 mongod 主节点收到关闭请求,则主节点:

  1. 尝试降级到从节点。

    如果降级失败,并且:

  2. 进入静默期。

  3. 结束所有剩余的数据库操作。

  4. 关闭。

对于 mongod 从节点mongos 关闭请求,在请求关闭之后进入静默期。

静止期由以下各项指定:

mongodmongos 关闭时,客户端无法新建相关连接。

timeoutSecs指定了一个时间段(以秒为单位)。默认为:

  • 从 MongoDB 5.0 开始为 15 秒。

  • 在 MongoDB 5.0 以前的版本中为 10 秒。

mongod 按如下方式使用 timeoutSecs

  • 如果当前节点是副本集的主节点,则 mongod 会等待 timeoutSecs 字段指定的秒数,以便可选举节点跟上,然后再降级主节点。有关追赶时间的详情,请参阅复制延迟。

  • 如果当前节点在退出主节点后处于SECONDARY状态,则 timeoutSecs 中指定的任何剩余时间将作为静默期,以允许完成现有操作。新操作将发送到其他副本集节点。

从 MongoDB 5.0 开始,mongos 使用 TimeoutSecs 作为静默期,这会允许现有操作完成。新操作将发送到其他 mongos 节点。在 MongoDB 5.0 以前的版本中,mongos 立即关闭并且不使用 timeoutSecs

警告

强制关闭主节点可能会导致所有尚未复制到从节点的写入操作发生回滚

To run db.shutdownServer() on a mongod enforcing 自主管理部署的身份验证, the authenticated user must have the db.shutdownServer() privilege. For example, a user with the built-in role hostManager has the appropriate permissions.

db.getSiblingDB("admin").shutdownServer()
db.getSiblingDB("admin").shutdownServer({ "force" : true })
db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })