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