说明
db.killOp(opid)终止操作 ID 所指定的操作。要查找操作及其对应的 ID,请参阅
$currentOp或db.currentOp()。The
db.killOp()method has the following parameter:Parameter类型说明op数字
操作 ID。
警告
Terminate running operations with extreme caution. Only use
db.killOp()to terminate operations initiated by clients and do not terminate internal database operations.
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
MongoDB Atlas limits the use of this method to the MongoDB user who ran the operation. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
分片集群
终止读操作
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.
例如,要终止分片集群上的查询操作:
在客户端发出查询时所在的那个
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" } ] ) 重要
您必须在客户端发出查询的同一
mongos上发出此聚合操作。Once you find the query operation to kill, issue
db.killOp()with the opid on themongos:db.killOp(<opid of the query to kill>)
提示
$currentOp 中的 localOps 参数。
或者,您可以从正在运行读取操作的分片节点中查找并终止该操作。MongoDB 将终止操作传播到其他分片和 mongos 实例:
在运行操作的其中一个分片上,找到要终止的查询操作的 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" } ] ) 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 将终止操作传播到其他分片和
mongos实例。
终止写入操作
- 会话内
MongoDB 驱动程序将所有操作与服务器会话相关联,但未确认的写入除外。
如果写入操作与某个会话相关联,您可以在
mongos上使用killSessions命令来终止跨分片的写入操作。在
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" } ] ) 使用返回的
lsid信息,在mongos上发出killSessions命令以终止分片上的操作。db.adminCommand( { killSessions: [ { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") } ] } )
- 无会话
如果该写入操作不与会话关联,则必须在与该操作关联的所有分片上查找并终止该操作。
从
mongos运行聚合管道$currentOp以查找分片上查询操作的操作 ID:use admin db.aggregate( [ { $currentOp : { allUsers: true } }, { $match : <filter condition> } // Optional. Specify the condition to find the op. ] ) 在
mongos上运行时,$currentOp以"<shardName>:<opid on that shard>"格式返回操作 ID,例如{ "shard" : "shardB", .. "opid" : "shardB:79214", ... }, { "shard" : "shardA", .. "opid" : "shardA:100913", ... }, Using the opid information, issue
db.killOp()on themongosto kill the operation on the shards.db.killOp("shardB:79014"); db.killOp("shardA:100813");
访问控制
在使用authorization运行的系统上,要终止不属于用户的操作,用户必须具有包括killop特权操作的访问权限。