Database Manual / Reference / mongosh Methods / Databases

db.shutdownServer() (mongosh method方法)

Changed in version 5.0.在版本5.0中的更改。

db.shutdownServer()

Shuts down the current mongod or mongos process cleanly and safely. 干净安全地关闭当前的mongodmongos进程。You must issue the db.shutdownServer() operation against the admin database.您必须对admin数据库发出db.shutdownServer()操作。

db.shutdownServer() has this syntax:具有以下语法:

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

The method takes these fields:该方法接受以下字段:

Field字段Description描述
force

Optional. 可选。Specify true to force the mongod or mongos to shut down. Force shutdown interrupts any ongoing operations on the mongod or mongos and may result in unexpected behavior.指定true可强制关闭mongodmongos。强制关机会中断对mongodmongos的任何正在进行的操作,并可能导致意外行为。

You can pause and resume in-progress index builds using force. See db.shutdownServer() on Replica Set Members for more information.您可以使用force暂停和恢复正在进行的索引构建。有关详细信息,请参阅副本集成员上的db.shutdownServer()

timeoutSecs

Optional.可选。

Starting in MongoDB 5.0, mongod and mongos enter a quiesce period to allow any ongoing database operations to complete before shutting down.从MongoDB 5.0开始,mongodmongos进入一个停顿期,以便在关闭之前完成任何正在进行的数据库操作。

If a mongod primary receives a shut down request, the primary:如果mongodprimary收到关闭请求,则主服务器:

  1. Attempts to step down to a secondary.试图降级到secondary

    If the step down fails and a:如果降级失败,并且a:

  2. Enters the quiesce period.进入停顿期。
  3. Ends any remaining database operations.结束所有剩余的数据库操作。
  4. Shuts down.关闭。

For a mongod secondary or mongos shut down request, the quiesce period is entered after a shut down was requested.对于mongodsecondarymongos关闭请求,在请求关闭后进入静默期。

The quiesce period is specified by the:停顿期由以下因素指定:

Clients cannot open new connections to a mongod or mongos that is shutting down.客户端无法打开与正在关闭的mongodmongos的新连接。

timeoutSecs specifies a time period in seconds. The default is:timeoutSecs指定以秒为单位的时间段。默认值为:

  • 15 seconds starting in MongoDB 5.0.从MongoDB 5.0开始15秒。
  • 10 seconds in MongoDB versions earlier than 5.0.在5.0之前的MongoDB版本中为10秒。

mongod uses timeoutSecs as follows:使用timeoutSecs如下:

  • If the current node is the primary node of a replica set, mongod waits for a period of up to the number of seconds specified by the timeoutSecs field for an electable node to catch up before stepping down the primary node. 如果当前节点是副本集的primary节点,mongod将等待timeoutSecs字段指定的秒数,以便可选择的节点赶上,然后再关闭主节点。For details about the catch up time, see replication lag.有关追赶时间的详细信息,请参阅复制延迟
  • If the current node is in the SECONDARY state after stepping down from being the primary, any remaining time specified in timeoutSecs is used for a quiesce period, which allows existing operations to complete. New operations are sent to other replica set nodes.如果当前节点在从主节点降级后处于SECONDARY状态,则timeoutSecs中指定的任何剩余时间都将用于停顿期,这允许完成现有操作。新操作被发送到其他副本集节点。

Starting in MongoDB 5.0, mongos uses timeoutSecs as a quiesce period, which allows existing operations to complete. 从MongoDB 5.0开始,mongos使用timeoutSecs作为停顿期,这允许完成现有的操作。New operations are sent to other mongos nodes. 新的操作被发送到其他mongos节点。In MongoDB versions earlier than 5.0, mongos shuts down immediately and does not use timeoutSecs.在早于5.0的MongoDB版本中,mongos会立即关闭并且不使用timeoutSecs

This operation provides a wrapper around the shutdown command.此操作为shutdown命令提供了一个包装。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Behavior行为

For a mongod started with Authentication on Self-Managed Deployments, you must run db.shutdownServer() over an authenticated connection. 对于以自我管理部署身份验证开始的mongod,您必须在经过身份验证的连接上运行db.shutdownServer()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. 对于在自我管理部署上没有身份验证mongod,您必须从连接到localhost接口的客户端运行db.shutdownServer()For example, run mongosh with the --host "127.0.0.1" option on the same host machine as the mongod.例如,在与mongod相同的主机上运行带有--host "127.0.0.1"选项的mongosh

db.shutdownServer() on Replica Set Members副本集成员上的db.shutdownServer()

db.shutdownServer() fails if the mongod replica set member is running certain operations such as index builds. 如果mongod副本集成员正在运行某些操作,如索引构建db.shutdownServer()将失败。You can specify force: true to save index build progress to disk. 您可以指定force: true将索引构建进度保存到磁盘。The mongod recovers the index build when it restarts and continues from the saved checkpoint.mongod在重新启动时恢复索引构建,并从保存的检查点继续。

Shutting Down the Replica Set Primary, Secondary, or mongos关闭主副本集、次副本集或mongos副本集

Starting in MongoDB 5.0, mongod and mongos enter a quiesce period to allow any ongoing database operations to complete before shutting down.从MongoDB 5.0开始,mongodmongos进入一个停顿期,以便在关闭之前完成任何正在进行的数据库操作。

If a mongod primary receives a shut down request, the primary:如果mongodprimary收到关闭请求,则主服务器:

  1. Attempts to step down to a secondary.试图降级到secondary

    If the step down fails and a:如果降级失败,并且a:

  2. Enters the quiesce period.进入停顿期。
  3. Ends any remaining database operations.结束所有剩余的数据库操作。
  4. Shuts down.关闭。

For a mongod secondary or mongos shut down request, the quiesce period is entered after a shut down was requested.对于mongodsecondarymongos关闭请求,在请求关闭后进入静默期。

The quiesce period is specified by the:停顿期由以下因素指定:

Clients cannot open new connections to a mongod or mongos that is shutting down.客户端无法打开与正在关闭的mongodmongos的新连接。

timeoutSecs specifies a time period in seconds. The default is:指定以秒为单位的时间段。默认值为:

  • 15 seconds starting in MongoDB 5.0.从MongoDB 5.0开始15秒。
  • 10 seconds in MongoDB versions earlier than 5.0.在5.0之前的MongoDB版本中为10秒。

mongod uses timeoutSecs as follows:使用timeoutSecs如下:

  • If the current node is the primary node of a replica set, mongod waits for a period of up to the number of seconds specified by the timeoutSecs field for an electable node to catch up before stepping down the primary node. For details about the catch up time, see replication lag.如果当前节点是副本集的primary节点,mongod将等待timeoutSecs字段指定的秒数,以便可选择的节点赶上,然后再关闭主节点。有关追赶时间的详细信息,请参阅复制延迟
  • If the current node is in the SECONDARY state after stepping down from being the primary, any remaining time specified in timeoutSecs is used for a quiesce period, which allows existing operations to complete. New operations are sent to other replica set nodes.如果当前节点在从主节点降级后处于SECONDARY状态,则timeoutSecs中指定的任何剩余时间都将用于停顿期,这允许完成现有操作。新操作被发送到其他副本集节点。

Starting in MongoDB 5.0, mongos uses timeoutSecs as a quiesce period, which allows existing operations to complete. 从MongoDB 5.0开始,mongos使用timeoutSecs作为停顿期,这允许完成现有的操作。New operations are sent to other mongos nodes. 新的操作被发送到其他mongos节点。In MongoDB versions earlier than 5.0, mongos shuts down immediately and does not use timeoutSecs.在早于5.0的MongoDB版本中,mongos会立即关闭并且不使用timeoutSecs

Warning

Force shutdown of the primary can result in the rollback of any writes not yet replicated to a secondary.强制关闭主服务器可能会导致任何尚未复制到辅助服务器的写入回滚

Access Control访问控制

To run db.shutdownServer() on a mongod enforcing Authentication on Self-Managed Deployments, the authenticated user must have the db.shutdownServer() privilege. 要在mongod上运行db.shutdownServer(),强制对自我管理部署进行身份验证,经过身份验证的用户必须具有db.shutdownServer()权限。For example, a user with the built-in role hostManager has the appropriate permissions.例如,具有内置角色hostManager的用户具有适当的权限。

Examples示例

Shut down a mongod关掉mongod

db.getSiblingDB("admin").shutdownServer()

Force Shut Down a mongod强制关闭mongod

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

Shut Down a Primary mongod With Longer Timeout关闭一个主mongod,超时时间更长

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