Docs HomeMongoDB Manual

db.shutdownServer()

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.您必须对管理数据库发出db.shutdownServer()操作。

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

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

The method takes these fields:该方法采用以下字段:

Field字段Description描述
forceOptional.可选的。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上正在进行的任何操作,并可能导致意外行为。
timeoutSecsOptional.可选的。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收到关闭请求,则primary:
  1. Attempts to step down to a secondary.尝试降级到secondary
    If the step down fails and 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 secondarymongos关闭请求,在请求关闭后输入静止期。
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. In MongoDB versions earlier than 5.0, mongos shuts down immediately and does not use timeoutSecs.新操作被发送到其他mongos节点。在MongoDB 5.0之前的版本中,mongos会立即关闭并且不使用timeoutSecs
For a mongod primary in MongoDB 4.4 and earlier, timeoutSecs specifies the time in seconds that the primary waits for a secondary to catch up for the shutdownServer command. 对于MongoDB 4.4及更早版本中的mongod-primary,timeoutSecs指定primary等待secondary赶上shutdownServer命令的时间(以秒为单位)。If no secondaries catch up within timeoutSecs, the shutdownServer command fails. 如果在timeoutSecs内没有任何辅助设备赶上,则shutdownServer命令将失败。

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

Behavior行为

For a mongod started with Authentication, you must run db.shutdownServer() over an authenticated connection. 对于以身份验证启动的mongod,必须在经过身份验证的连接上运行db.shutdownServer()See Access Control for more information.有关详细信息,请参阅访问控制

For a mongod started without Authentication, 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 force the member to interrupt those operations and shut down.您可以指定force: true来强制成员中断这些操作并关闭。

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收到关闭请求,则primary:

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

    If the step down fails and 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 secondarymongos关闭请求,在请求关闭后输入静止期。

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. timeoutSecs指定以秒为单位的时间段。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.在MongoDB 5.0之前的版本中,mongos会立即关闭并且不使用timeoutSecs

For a mongod primary in MongoDB 4.4 and earlier, timeoutSecs specifies the time in seconds that the primary waits for a secondary to catch up for the shutdownServer command. 对于MongoDB 4.4及更早版本中的mongod-primarytimeoutSecs指定primary等待secondary赶上shutdownServer命令的时间(以秒为单位)。If no secondaries catch up within timeoutSecs, the shutdownServer command fails.如果在timeoutSecs内没有任何辅助设备赶上,则shutdownServer命令将失败。

In MongoDB 4.4 and earlier, if running db.shutdownServer() against the replica set primary, the operation implicitly uses replSetStepDown to step down the primary before shutting down the mongod. 在MongoDB 4.4及更早版本中,如果对副本集primary运行db.shutdownServer(),则该操作会在关闭mongod之前隐式使用replSetStepDown来逐步关闭primary。If no secondary in the replica set can catch up to the primary within 10 seconds, the shutdown operation fails. 如果复制副本集中没有辅助副本能够在10秒内赶上主副本,则关闭操作将失败。You can issue db.shutdownServer() with force: true to shut down the primary even if the step down fails.您可以使用force: true发出db.shutdownServer()来关闭主服务器,即使关闭失败。

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, 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 })