db.shutdownServer()
5.0 版本中的更改。
db.shutdownServer()
Shuts down the current
mongod
ormongos
process cleanly and safely. You must issue thedb.shutdownServer()
operation against the 管理库.db.shutdownServer()
has this syntax:db.shutdownServer({ force: <boolean>, timeoutSecs: <int> }) The method takes these fields:
字段说明可选。指定
true
以强制关闭mongod
或mongos
。强制关闭会中断mongod
或mongos
上正在进行的任何操作,并可能导致意外行为。您可以使用
force
暂停和恢复正在进行的索引构建。db.shutdownServer()
有关更多信息,请参阅有关副本集节点的 。可选。
从 MongoDB 5.0 开始,
mongod
和mongos
进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。尝试降级到从节点。
如果降级失败,并且:
进入静默期。
结束所有剩余的数据库操作。
关闭。
对于
mongod
从节点或mongos
关闭请求,在请求关闭之后进入静默期。静止期由以下各项指定:
shutdownTimeoutMillisForSignaledShutdown
服务器参数(如果SIGTERM
信号已发送到mongod
),或者mongosShutdownTimeoutMillisForSignaledShutdown
服务器参数(如果将SIGTERM
信号发送到mongos
)。
mongod
或mongos
关闭时,客户端无法新建相关连接。timeoutSecs 指定一个时间段(以秒为单位)。默认为:
从 MongoDB 5.0 开始为 15 秒。
在 MongoDB 5.0 以前的版本中为 10 秒。
如果当前节点是副本集的主节点 (primary node in
mongod
the replica set)节点,则 会等待 timeoutSecs字段指定的秒数,让可选举节点赶上主节点,然后再降级主节点 (primary node in the replica set)节点。有关追赶时间的详细信息,请参阅复制延迟。如果当前节点在降级主节点 (primary node in
SECONDARY
the replica set)节点后处于 状态,则 timeoutSecs 中指定的任何剩余时间将用于静默期,以便完成现有操作。新操作将发送到其他副本集节点。
从MongoDB5.0 开始,
mongos
使用 timeoutSecs 作为静默期,以允许完成现有操作。新操作将发送到其他mongos
节点。在 5.0 之前的MongoDB版本中,mongos
会立即关闭且不使用 timeoutSecs 。
This operation provides a wrapper around the shutdown
command.
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas集群不支持此命令。 有关Atlas支持所有命令的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
For a mongod
started with 自主管理部署的身份验证, you
must run db.shutdownServer()
over an authenticated connection.
See 访问控制 for more information.
For a mongod
started without 自主管理部署的身份验证, you
must run db.shutdownServer()
from a client connected to the
localhost interface. For example, run mongosh
with the
--host "127.0.0.1"
option on the same host
machine as the mongod
.
db.shutdownServer()
(针对副本集节点)
db.shutdownServer()
fails if the mongod
replica
set member is running certain operations such as index builds. You can specify force: true to save index build progress to disk.
The mongod
recovers the index build when it restarts
and continues from the saved checkpoint.
关闭副本集主节点、从节点或 mongos
从 MongoDB 5.0 开始,mongod
和 mongos
进入静默期,以允许所有正在进行的数据库操作在关闭之前完成。
尝试降级到从节点。
如果降级失败,并且:
进入静默期。
结束所有剩余的数据库操作。
关闭。
对于 mongod
从节点或 mongos
关闭请求,在请求关闭之后进入静默期。
静止期由以下各项指定:
timeoutSecs field if a
shutdown
ordb.shutdownServer()
command was run, orshutdownTimeoutMillisForSignaledShutdown
服务器参数(如果SIGTERM
信号已发送到mongod
),或者mongosShutdownTimeoutMillisForSignaledShutdown
服务器参数(如果将SIGTERM
信号发送到mongos
)。
mongod
或 mongos
关闭时,客户端无法新建相关连接。
timeoutSecs指定了一个时间段(以秒为单位)。默认为:
从 MongoDB 5.0 开始为 15 秒。
在 MongoDB 5.0 以前的版本中为 10 秒。
mongod
按如下方式使用 timeoutSecs:
如果当前节点是副本集的主节点,则
mongod
会等待 timeoutSecs 字段指定的秒数,以便可选举节点跟上,然后再降级主节点。有关追赶时间的详情,请参阅复制延迟。如果当前节点在退出主节点后处于
SECONDARY
状态,则 timeoutSecs 中指定的任何剩余时间将作为静默期,以允许完成现有操作。新操作将发送到其他副本集节点。
从 MongoDB 5.0 开始,mongos
使用 TimeoutSecs 作为静默期,这会允许现有操作完成。新操作将发送到其他 mongos
节点。在 MongoDB 5.0 以前的版本中,mongos
立即关闭并且不使用 timeoutSecs。
警告
强制关闭主节点可能会导致所有尚未复制到从节点的写入操作发生回滚。
访问控制
To run db.shutdownServer()
on a mongod
enforcing 自主管理部署的身份验证, the authenticated user must have the
db.shutdownServer()
privilege. For example, a user with the
built-in role hostManager
has the appropriate permissions.
示例
关闭 mongod
db.getSiblingDB("admin").shutdownServer()
强制关闭 mongod
db.getSiblingDB("admin").shutdownServer({ "force" : true })
关闭超时时间较长的主节点 mongod
db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })