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.admin数据库发出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. Force shutdown interrupts any ongoing operations on themongodormongosand may result in unexpected behavior.true可强制关闭mongod或mongos。强制关机会中断对mongod或mongos的任何正在进行的操作,并可能导致意外行为。You can pause and resume in-progress index builds using您可以使用force. Seedb.shutdownServer()on Replica Set Members for more information.force暂停和恢复正在进行的索引构建。有关详细信息,请参阅副本集成员上的db.shutdownServer()。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收到关闭请求,则主服务器:Attempts to step down to a secondary.试图降级到secondary。If the step down fails and a:如果降级失败,并且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对于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 aSIGTERMsignal was sent tomongod, ormongod发送了SIGTERM信号,则shutdownTimeoutMillisForSignaledShutdown服务器参数,或mongosShutdownTimeoutMillisForSignaledShutdownserver parameter if aSIGTERMsignal was sent tomongos.mongosShutdownTimeoutMillisForSignaledShutdown服务器参数(如果向mongos发送了SIGTERM信号)。
Clients cannot open new connections to a客户端无法打开与正在关闭的mongodormongosthat is shutting down.mongod或mongos的新连接。timeoutSecs specifies a time period in seconds. The default is:timeoutSecs指定以秒为单位的时间段。默认值为: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. New operations are sent to other replica set nodes.SECONDARY状态,则timeoutSecs中指定的任何剩余时间都将用于停顿期,这允许完成现有操作。新操作被发送到其他副本集节点。
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.mongos节点。In MongoDB versions earlier than 5.0,在早于5.0的MongoDB版本中,mongosshuts down immediately and does not use timeoutSecs.mongos会立即关闭并且不使用timeoutSecs。
This operation provides a wrapper around the 此操作为shutdown command.shutdown命令提供了一个包装。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- 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的源代码可用、免费使用和自我管理版本
Behavior行为
For a 对于以自我管理部署身份验证开始的mongod started with Authentication on Self-Managed Deployments, you must run db.shutdownServer() over an authenticated connection. mongod,您必须在经过身份验证的连接上运行db.shutdownServer()。See Access Control for more information.有关更多信息,请参阅访问控制。
For a 对于在自我管理部署上没有身份验证的mongod started without Authentication on Self-Managed Deployments, 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 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副本集
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收到关闭请求,则主服务器:
Attempts to step down to a secondary.试图降级到secondary。If the step down fails and a:如果降级失败,并且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.mongodsecondary或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, ormongod发送了SIGTERM信号,则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. 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. For details about the catch up time, see replication lag.mongod将等待timeoutSecs字段指定的秒数,以便可选择的节点赶上,然后再关闭主节点。有关追赶时间的详细信息,请参阅复制延迟。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. New operations are sent to other replica set nodes.SECONDARY状态,则timeoutSecs中指定的任何剩余时间都将用于停顿期,这允许完成现有操作。新操作被发送到其他副本集节点。
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, 在早于5.0的MongoDB版本中,mongos shuts down immediately and does not use timeoutSecs.mongos会立即关闭并且不使用timeoutSecs。
Access Control访问控制
To run 要在db.shutdownServer() on a mongod enforcing Authentication on Self-Managed Deployments, 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 })