db.killOp()
Description
db.
killOp
(opid)- Terminates an operation as specified by the operation ID. To findoperations and their corresponding IDs, see
$currentOp
ordb.currentOp()
.
The db.killOp()
method has the following parameter:
ParameterTypeDescriptionop
numberAn operation ID.
Warning
Terminate running operations with extreme caution. Only usedb.killOp()
to terminate operations initiated by clientsand do not terminate internal database operations.
Sharded Cluster
Kill Read Operations
- Starting in MongoDB 4.0
- The
db.killOp()
method can be run on amongos
and can kill queries (read operations) that are running on morethan one shard in a cluster.
For example, to kill a query operation on a MongoDB 4.0+ shardedcluster:
- From the mongos Instance
- From a shard member
- On the same
mongos
where the client issued thequery, find the opid of the query operation to kill by running theaggregation pipeline$currentOp
with thelocalOps:true
:
- 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 samemongos
where the client issued the query.
- Once you find the query operation to kill, issue
db.killOp()
with the opid on themongos
:
- db.killOp(<opid of the query to kill>)
See also
The localOps
parameter in $currentOp
.
Alternatively, you can find and kill the read operation from ashard member where the operation is running. MongoDB 4.0+propagates the kill operation to the other shards andmongos
instances:
- On one of the shards where the operation is running, find the opidof the query operation to kill:
- 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" }
- ] )
- Once you find the query operation to kill, issue
db.killOp()
with the opid on the shard member:
- db.killOp(<opid of the query to kill>)
MongoDB 4.0+ propagates the kill operation to theother shards and mongos
instances.
- For MongoDB 3.6 and earlier
To kill a query running on 3.6 (or earlier) sharded clusters, you must kill theoperation on all the shards associated with the query.
- From a
mongos
, run the aggregation pipeline$currentOp
to find the opid(s) of the query operation onthe shards:
- From a
- 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 thatshard>"
; e.g.
- {
- "shard" : "shardB",
- ..
- "opid" : "shardB:79014",
- ...
- },
- {
- "shard" : "shardA",
- ..
- "opid" : "shardA:100813",
- ...
- },
- Using the opid information, issue
db.killOp()
on themongos
to kill the operation on the shards.
- db.killOp("shardB:79014");
- db.killOp("shardA:100813");
Kill Write Operations
- Within a Session
- Starting in MongoDB 3.6, MongoDB drivers associate all operationswith a server session, with theexception of unacknowledged writes.
If the write operation is associated with a session, you can use thekillSessions
command on the mongos
tokill the write operation across shards.
- MongoDB 4.0+
- MongoDB 3.6
- Run the aggregation pipeline
$currentOp
onthemongos
to find thelsid
(logical session 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" }
- ] )
- Using the returned
lsid
information, issue thekillSessions
command on themongos
to kill the operation on the shards.
- db.adminCommand( { killSessions: [
- { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
- ] } )
- Run the aggregation pipeline
$currentOp
onthemongos
or the individual shards find thelsid
(logical session 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" }
- ] )
- Using the returned lsid information, issue the
killSessions
command on themongos
to kill the operation on theshards.
- 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 theoperation on all the shards associated with the write.
- From a
mongos
, run the aggregation pipeline$currentOp
to find the opid(s) of the query operation onthe shards:
- From a
- 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 thatshard>"
; e.g.
- {
- "shard" : "shardB",
- ..
- "opid" : "shardB:79214",
- ...
- },
- {
- "shard" : "shardA",
- ..
- "opid" : "shardA:100913",
- ...
- },
- Using the opid information, issue
db.killOp()
on themongos
to kill the operation on the shards.
- db.killOp("shardB:79014");
- db.killOp("shardA:100813");
Access Control
On systems running with authorization
, to killoperations not owned by the user, the user must have access thatincludes the killop
privilege action.
Changed in version 3.2.9: On mongod
instances, users can kill their own operationseven without the killop
privilege action.
See also