Docs HomeMongoDB Manual

Terminate Running Operations终止正在运行的操作

Overview概述

MongoDB provides two facilitates to terminate running operations: maxTimeMS() and db.killOp(). MongoDB提供了两个终止运行操作的便利:maxTimeMS()db.killOp()Use these operations as needed to control the behavior of operations in a MongoDB deployment.根据需要使用这些操作来控制MongoDB部署中操作的行为。

Available Procedures可用程序

maxTimeMS

The maxTimeMS() method sets a time limit for an operation. maxTimeMS()方法设置操作的时间限制。When the operation reaches the specified time limit, MongoDB interrupts the operation at the next interrupt point.当操作达到指定的时间限制时,MongoDB会在下一个中断点中断操作。

Terminate a Query终止查询

From mongosh, use the following method to set a time limit of 30 milliseconds for this query:mongosh中,使用以下方法为此查询设置30毫秒的时间限制:

db.location.find( { "town": { "$regex": "(Pine Lumber)",
"$options": 'i' } } ).maxTimeMS(30)

Terminate a Command终止命令

Consider a potentially long running operation using distinct to return each distinct collection field that has a city key:考虑一个可能长期运行的操作,使用distinct返回每个具有城市键的剔除重复collection字段:

db.runCommand( { distinct: "collection",
key: "city" } )

You can add the maxTimeMS field to the command document to set a time limit of 45 milliseconds for the operation:您可以将maxTimeMS字段添加到命令文档中,为操作设置45毫秒的时间限制:

db.runCommand( { distinct: "collection",
key: "city",
maxTimeMS: 45 } )

Operations that reach maxTimeMS will return a MaxTimeMSExpired error.达到maxTimeMS的操作将返回MaxTimeMSExpired错误。

killOp

The db.killOp() method interrupts a running operation at the next interrupt point. db.killOp() identifies the target operation by operation ID.db.killOp()方法在下一个中断点中断正在运行的操作。db.killOp()通过操作ID标识目标操作。

db.killOp(<opId>)
Warning

Terminate running operations with extreme caution. 终止运行操作时要格外小心。Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.仅使用db.killOp()终止客户端启动的操作,而终止内部数据库操作。

Sharded Cluster分片集群

Starting in MongoDB 4.0, the killOp command can be run on a mongos and can kill queries (i.e. read operations) that span shards in a cluster. 从MongoDB 4.0开始,killOp命令可以在mongos上运行,并可以终止跨集群中分片的查询(即读取操作)。The killOp command from the mongos does not propagate to the shards when the operation to be killed is a write operation.当要终止的操作是写操作时,mongoskillOp命令不会传播到分片。

For more information on killing operations on a sharded cluster, see:有关在分片集群上终止操作的更多信息,请参阅:

For information on how to list sharding operations that are active on a mongos, see the localOps parameter in $currentOp.有关如何列出mongos上活动的分片操作的信息,请参阅$currentOp中的localOps参数。