db.shutdownServer()

On this page本页内容

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. 指定true以强制mongodmongos关闭。Force shutdown interrupts any ongoing operations on the mongod or mongos and may result in unexpected behavior.强制关闭会中断mongodmongos上正在进行的任何操作,并可能导致意外行为。

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收到关闭请求,则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:静止期由以下参数指定:

The timeoutSecs field defaults to 15 seconds in MongoDB 5.0.在MongoDB 5.0中,timeoutSecs字段默认为15秒。

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

In MongoDB 4.4 and earlier, for a mongodprimary, timeoutSecs is the number of seconds the primary should wait for a secondary to catch up. 在MongoDB 4.4及更早版本中,对于mongod primarytimeoutSecs是主服务器应该等待辅助服务器赶上的秒数。If no secondaries catch up within the specified time, the command fails. 如果在指定的时间内没有辅助设备赶上,则命令将失败。Defaults to 10 seconds.默认为10秒。

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. 对于以Authentication启动的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() 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.尝试降级到次要级别

    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:静止期由以下参数指定:

The timeoutSecs field defaults to 15 seconds in MongoDB 5.0.在MongoDB 5.0中,timeoutSecs字段默认为15秒。

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

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及更早版本中,如果对副本集主服务器运行db.shutdownServer(),则操作会在关闭mongod之前隐式使用replSetStepDown逐步关闭主服务器。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 ifthe 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

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

Force Shut Down a 强制关闭mongod

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

Shut Down a Primary mongod With Longer Timeout关闭超时较长的主mongod

db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })
←  db.setProfilingLevel()db.stats() →