Database Manual / Administration / Management

Terminate Running Operations终止运行操作

Overview概述

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

Available Procedures可用程序

maxTimeMS

The maxTimeMS() method sets a time limit for an operation. When the operation reaches the specified time limit, MongoDB interrupts the operation at the next interrupt point.maxTimeMS()方法为操作设置时间限制。当操作达到指定的时间限制时,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返回每个具有city键的不同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分片集群

The killOp command can be run on a mongos and can kill queries (i.e. read operations) that span shards in a cluster. The killOp command from the mongos does not propagate to the shards when the operation to be killed is a write operation.killOp命令可以在mongos上运行,并可以终止跨越集群中分片的查询(即读取操作)。当要终止的操作是写操作时,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参数。