db.shutdownServer()
On this page本页内容
Changed in version 5.0.5.0版更改。
db.shutdownServer()
-
Shuts down the current干净安全地关闭当前mongod
ormongos
process cleanly and safely.mongod
或mongos
进程。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描述force
Optional.可选的。Specify指定true
to force themongod
ormongos
to shut down.true
以强制关闭mongod
或mongos
。Force shutdown interrupts any ongoing operations on the强制关闭会中断mongod
ormongos
and may result in unexpected behavior.mongod
或mongos
上正在进行的任何操作,并可能导致意外行为。timeoutSecs
Optional.可选的。Starting in MongoDB 5.0,从MongoDB 5.0开始,mongod
andmongos
enter a quiesce period to allow any ongoing database operations to complete before shutting down.mongod
和mongos
进入一个停顿期,允许在关闭之前完成任何正在进行的数据库操作。
If a如果mongod
primary receives a shut down request, the primary:mongod
primary收到关闭请求,则primary:Attempts to step down to a secondary.尝试降级到secondary。
If the step down fails and a:如果降压失败,并且出现:shutdown
ordb.shutdownServer()
command was run,mongod
only continues the shut down steps if the force field is true, or ashutdown
或db.shutdownServer()
命令运行时,mongod
仅在force
字段为true
,或SIGTERM
signal was sent to信号被发送到mongod
,mongod
always continues the shut down steps.mongod
,mongod
总是继续关闭步骤。
Enters the quiesce period.进入静止期。Ends any remaining database operations.结束所有剩余的数据库操作。Shuts down.关闭。
For a对于mongod
secondary ormongos
shut down request, the quiesce period is entered after a shut down was requested.mongod
secondary或mongos
关闭请求,在请求关闭后输入静止期。
The quiesce period is specified by the:静止期由以下内容指定:timeoutSecs field if ashutdown
ordb.shutdownServer()
command was run, ortimeoutSecs
字段(如果运行了shutdown
或db.shutdownServer()
命令),或者shutdownTimeoutMillisForSignaledShutdown
server parameter if a服务器参数,如果SIGTERM
signal was sent tomongod
, orSIGTERM
信号被发送到mongod
,或者mongosShutdownTimeoutMillisForSignaledShutdown
server parameter if aSIGTERM
signal was sent tomongos
.mongosShutdownTimeoutMillisForSignaledShutdown
服务器参数(如果向mongos
发送了SIGTERM
信号)。
Clients cannot open new connections to a客户端无法打开与正在关闭的mongod
ormongos
that is shutting down.mongod
或mongos
的新连接。
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,如果当前节点是副本集的primary节点,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.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,从MongoDB 5.0开始,mongos
uses timeoutSecs as a quiesce period, which allows existing operations to complete.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对于MongoDB 4.4及更早版本中的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 theshutdownServer
command.mongod
-primary,timeoutSecs
指定primary等待secondary赶上shutdownServer
命令的时间(以秒为单位)。If no secondaries catch up within如果在timeoutSecs
, theshutdownServer
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()
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
mongos
Starting in MongoDB 5.0, 从MongoDB 5.0开始,mongod
and mongos
enter a quiesce period to allow any ongoing database operations to complete before shutting down.mongod
和mongos
进入一个停顿期,允许在关闭之前完成任何正在进行的数据库操作。
If a 如果mongod
primary receives a shut down request, the primary:mongod
primary收到关闭请求,则primary:
Attempts to step down to a secondary.尝试降级到secondary。If the step down fails and a:如果降级失败,并且出现:shutdown
ordb.shutdownServer()
command was run,mongod
only continues the shut down steps if the force field is true, or ashutdown
或db.shutdownServer()
命令运行时,mongod
仅在force
字段为true
或以下情况时继续关闭步骤:SIGTERM
signal was sent tomongod
,mongod
always continues the shut down steps.SIGTERM
信号被发送到mongod
,mongod
总是继续关闭步骤。
Enters the quiesce period.进入静止期。Ends any remaining database operations.结束所有剩余的数据库操作。Shuts down.关闭。
For a 对于mongod
secondary or mongos
shut down request, the quiesce period is entered after a shut down was requested.mongod
secondary或mongos
关闭请求,在请求关闭后输入静止期。
The quiesce period is specified by the:静止期由指定:
timeoutSecs field if ashutdown
ordb.shutdownServer()
command was run, ortimeoutSecs
字段(如果运行了shutdown
或db.shutdownServer()
命令),或者如果shutdownTimeoutMillisForSignaledShutdown
server parameter if aSIGTERM
signal was sent tomongod
, orSIGTERM
信号被发送到mongod
,则为shutdownTimeoutMillisForSignaledShutdown
服务器参数,或者如果向mongosShutdownTimeoutMillisForSignaledShutdown
server parameter if aSIGTERM
signal was sent tomongos
.mongos
发送了SIGTERM
信号,则为mongosShutdownTimeoutMillisForSignaledShutdown
服务器参数。
Clients cannot open new connections to a 客户端无法打开与正在关闭的mongod
or mongos
that is shutting down.mongod
或mongos
的新连接。
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,如果当前节点是副本集的primary节点,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.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, 从MongoDB 5.0开始,mongos
uses timeoutSecs as a quiesce period, which allows existing operations to complete. mongos
使用timeoutSecs
作为一个静止期,允许现有操作完成。New operations are sent to other 新操作被发送到其他mongos
nodes. mongos
节点。In MongoDB versions earlier than 5.0, 在MongoDB 5.0之前的版本中,mongos
shuts down immediately and does not use timeoutSecs.mongos
会立即关闭并且不使用timeoutSecs
。
For a 对于MongoDB 4.4及更早版本中的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. mongod
-primary,timeoutSecs
指定primary等待secondary赶上shutdownServer
命令的时间(以秒为单位)。If no secondaries catch up within 如果在timeoutSecs
, the shutdownServer
command fails.timeoutSecs
内没有任何辅助设备赶上,则shutdownServer
命令将失败。
In MongoDB 4.4 and earlier, if running 在MongoDB 4.4及更早版本中,如果对副本集primary运行db.shutdownServer()
against the replica set primary, the operation implicitly uses replSetStepDown
to step down the primary before shutting down the mongod
. 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()
来关闭主服务器,即使关闭失败。
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
mongod
db.getSiblingDB("admin").shutdownServer()
Force Shut Down a mongod
强制关闭一个mongod
mongod
db.getSiblingDB("admin").shutdownServer({ "force" : true })
Shut Down a Primary mongod
With Longer Timeout关闭超时时间较长的主mongod
mongod
With Longer Timeoutdb.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })