Database Manual / Reference / Database Commands / Administration

shutdown (database command数据库命令)

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

shutdown
The shutdown command cleans up all database resources and then terminates the process. You must issue the shutdown command against the admin database.shutdown命令清理所有数据库资源,然后终止进程。您必须对admin数据库发出shutdown命令。

Compatibility兼容性

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

  • 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的源代码可用、免费使用和自我管理版本

Important

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

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
shutdown: 1,
force: <boolean>
timeoutSecs: <int>,
comment: <any>
}
)

Command Fields命令字段

The command takes these fields:该命令包含以下字段:

Field字段Description描述
shutdown

Specify 1.指定1

force

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

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

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:指定以秒为单位的时间段。默认值为:

  • 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

comment

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

Behavior行为

For a mongod started with Authentication on Self-Managed Deployments, you must run shutdown over an authenticated connection. See Access Control for more information.对于以自我管理部署身份验证开始的mongod,您必须在经过身份验证的连接上运行关机。有关更多信息,请参阅访问控制。

For a mongod started without Authentication on Self-Managed Deployments, you must run shutdown from a client connected to the localhost interface. 对于在自我管理部署上没有身份验证的mongod,您必须从连接到localhost接口的客户端运行关机。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

shutdown on Replica Set Members在副本集成员上shutdown

shutdown fails if the replica set member is running certain operations such as index builds. 如果副本集成员正在运行某些操作(如索引构建),则失败。You can specify force: true to force the member 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:如果mongod主服务器收到关闭请求,则主服务器:

  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.对于mongod二级或mongos关闭请求,在请求关闭后进入静默期。

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. 如果当前节点是副本集的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. 如果当前节点在从主节点降级后处于SECONDARY状态,则timeoutSecs中指定的任何剩余时间都将用于停顿期,这允许完成现有操作。New operations are sent to other replica set nodes.新操作被发送到其他副本集节点。

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 shutdown on a mongod enforcing Authentication on Self-Managed Deployments, the authenticated user must have the shutdown privilege. 要在mongod上运行shutdown,并在自我管理部署上强制执行身份验证,经过身份验证的用户必须具有关机权限。For example, a user with the built-in role hostManager has the appropriate permissions.例如,具有内置角色hostManager的用户具有适当的权限。

Examples示例

Shut down a mongod关掉一只mongod

db.adminCommand({ "shutdown" : 1 })

Force Shut Down a mongod强制关闭一只mongod

db.adminCommand({ "shutdown" : 1, "force" : true })

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

db.adminCommand({ "shutdown" : 1, timeoutSecs: 60 })