db.killOp()

On this page本页内容

Description描述

db.killOp(opid)

Terminates an operation as specified by the operation ID. 终止操作ID指定的操作。To find operations and their corresponding IDs, see $currentOp or db.currentOp().要查找操作及其相应的ID,请参阅$currentOpdb.currentOp()

The db.killOp() method has the following parameter:db.killOp()方法具有以下参数:

Parameter参数Type类型Description描述
opnumberAn operation ID.手术ID。
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分片群集

Kill Read Operations终止读取操作

Starting in MongoDB 4.0从MongoDB 4.0开始

The db.killOp() method can be run on a mongos and can kill queries (read operations) that are running on more than one shard in a cluster.db.killOp()方法可以在mongos上运行,并可以终止在群集中多个分片上运行的查询(读取操作)。

For example, to kill a query operation on a MongoDB 4.0+ sharded cluster:例如,要终止MongoDB 4.0+分片集群上的查询操作:

  1. On the same mongos where the client issued the query, find the opid of the query operation to kill by running the aggregation pipeline $currentOp with the localOps: true:在客户端发出查询的同一个mongos上,通过使用localOps:true运行聚合管道$currentOp,找到要终止的查询操作的opid:

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true, localOps: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                       // e.g. { op: "getmore", "command.collection": "someCollection" }
    ] )
    Important重要

    You must issue this aggregation operation on the same mongos where the client issued the query.您必须在客户端发出查询的同一个mongos上发出此聚合操作。

  2. Once you find the query operation to kill, issue db.killOp() with the opid on the mongos找到要终止的查询操作后,在mongos上发出带有opid的db.killOp()

    db.killOp(<opid of the query to kill>)
Tip提示

See also: 另请参见:

The localOps parameter in $currentOp$currentOp中的localOps参数

Alternatively, you can find and kill the read operation from a shard member where the operation is running. 或者,您可以从运行操作的分片成员中找到并终止读取操作。MongoDB 4.0+ propagates the kill operation to the other shards and mongos instances:MongoDB 4.0+将kill操作传播到其他shard和mongos实例:

  1. On one of the shards where the operation is running, find the opid of the query operation to kill:在运行操作的其中一个分片上,找到要终止的查询操作的opid:

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                        // e.g. { op: "getmore", "command.collection": "someCollection" }
    ] )
  2. Once you find the query operation to kill, issue db.killOp() with the opid on the shard member:找到要终止的查询操作后,在分片成员上发出带有opid的db.killOp()

    db.killOp(<opid of the query to kill>)

    MongoDB 4.0+ propagates the kill operation to the other shards and mongos instances.MongoDB 4.0+将kill操作传播到其他shard和mongos实例。

For MongoDB 3.6 and earlier对于MongoDB 3.6及更早版本

To kill a query running on 3.6 (or earlier) sharded clusters, you must kill the operation on all the shards associated with the query.要终止在3.6(或更早版本)分片集群上运行的查询,必须终止与查询关联的所有分片上的操作。

  1. From a mongos, run the aggregation pipeline $currentOp to find the opid(s) of the query operation on the shards:mongos中,运行聚合管道$currentOp以查找分片上查询操作的opid:

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
    
                                       // e.g. { op: "getmore", "command.collection": "someCollection" }
    ] )

    When run on a mongos, $currentOp returns the opids in the format of "<shardName>:<opid on that shard>"; e.g.mongos上运行时,$currentOp"<shardName>:<opid on that shard>"的格式返回opid;如。

    {
       "shard" : "shardB",
       ..
       "opid" : "shardB:79014",
       ...
    },
    {
       "shard" : "shardA",
       ..
       "opid" : "shardA:100813",
       ...
    },
  2. Using the opid information, issue db.killOp() on the mongos to kill the operation on the shards.使用opid信息,在mongos上发出db.killOp()以终止对分片的操作。

    db.killOp("shardB:79014");
    db.killOp("shardA:100813");

Kill Write Operations终止写操作

Within a Session在一段时间内

Starting in MongoDB 3.6, MongoDB drivers associate all operations with a server session, with the exception of unacknowledged writes.从MongoDB 3.6开始,MongoDB驱动程序将所有操作与服务器会话相关联,未确认的写入除外。

If the write operation is associated with a session, you can use the killSessions command on the mongos to kill the write operation across shards.如果写入操作与会话相关联,则可以在mongos上使用killSessions命令来终止跨分片的写入操作。

  1. Run the aggregation pipeline $currentOp on the mongos to find the lsid (logical session id).mongos上运行聚合管道$currentOp以找到lsid(逻辑会话id)。

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true, localOps: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                        // e.g. { "op" : "update", "ns": "mydb.someCollection" }
    ] )
  2. Using the returned lsid information, issue the killSessions command on the mongos to kill the operation on the shards.使用返回的lsid信息,在mongos上发出killSessions命令以终止分片上的操作。

    db.adminCommand( { killSessions: [
       { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
    ] } )
  1. Run the aggregation pipeline $currentOp on the mongos or the individual shards find the lsid (logical session id).mongos上运行聚合管道$currentOp,或者单个分片找到lsid(逻辑会话id)。

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                        // e.g. { "op" : "update", "ns": "mydb.someCollection" }
    ] )
  2. Using the returned lsid information, issue the killSessions command on the mongos to kill the operation on the shards.使用返回的lsid信息,在mongos上发出killSessions命令以终止分片上的操作。

    db.adminCommand( { killSessions: [
       { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
    ] } )
Without a Session没有会话

If the write operation is not associated with a session, you must find and kill the operation on all the shards associated with the write.如果写入操作未与会话关联,则必须在与写入操作关联的所有分片上查找并终止该操作。

  1. From a mongos, run the aggregation pipeline $currentOp to find the opid(s) of the query operation on the shards:mongos中,运行聚合管道$currentOp以查找分片上查询操作的opid:

    use admin
    db.aggregate( [
       { $currentOp : { allUsers: true } },
       { $match : <filter condition> } // Optional.  Specify the condition to find the op.
    ] )

    When run on a mongos, $currentOp returns the opids in the format of "<shardName>:<opid on that shard>"; e.g.mongos上运行时,$currentOp"<shardName>:<opid on that shard>"的格式返回opid;如。

    {
       "shard" : "shardB",
       ..
       "opid" : "shardB:79214",
       ...
    },
    {
       "shard" : "shardA",
       ..
       "opid" : "shardA:100913",
       ...
    },
  2. Using the opid information, issue db.killOp() on the mongos to kill the operation on the shards.使用opid信息,在mongos上发出db.killOp()以终止对分片的操作。

    db.killOp("shardB:79014");
    db.killOp("shardA:100813");

Access Control访问控制

On systems running with authorization, to kill operations not owned by the user, the user must have access that includes the killop privilege action.授权运行的系统上,要终止非用户所有的操作,用户必须具有包括killop权限操作的访问权限。

Changed in version 3.2.9.在版本3.2.9中更改

On mongod instances, users can kill their own operations even without the killop privilege action.mongod实例上,即使没有killop权限操作,用户也可以终止自己的操作。

Tip提示
See also: 参阅:
←  db.hostInfo()db.listCommands() →