对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.killOp()(mongosh方法)

db.killOp(opid)

终止操作 ID 所指定的操作。要查找操作及其对应的 ID,请参阅 $currentOpdb.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:用于云中 MongoDB 部署的完全托管服务

重要

MongoDB Atlas 将此方法的使用限制为运行操作的 MongoDB 用户。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

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.

例如,要终止分片集群上的查询操作:

  1. 在客户端发出查询时所在的那个 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 上发出此聚合操作。

  2. Once you find the query operation to kill, issue db.killOp() with the opid on the mongos:

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

提示

$currentOp 中的 localOps 参数。

或者,您可以从正在运行读取操作的分片节点中查找并终止该操作。MongoDB 将终止操作传播到其他分片和 mongos 实例:

  1. 在运行操作的其中一个分片上,找到要终止的查询操作的 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:

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

    MongoDB 将终止操作传播到其他分片和 mongos 实例。

会话内

MongoDB 驱动程序将所有操作与服务器会话相关联,但未确认的写入除外。

如果写入操作与某个会话相关联,您可以在 mongos 上使用 killSessions 命令来终止跨分片的写入操作。

  1. 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" }
    ] )
    1. 使用返回的 lsid 信息,在 mongos 上发出 killSessions 命令以终止分片上的操作。

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

如果该写入操作与会话关联,则必须在与该操作关联的所有分片上查找并终止该操作。

  1. 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",
    ...
    },
  2. Using the opid information, issue db.killOp() on the mongos to kill the operation on the shards.

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

在使用authorization运行的系统上,要终止不属于用户的操作,用户必须具有包括killop特权操作的访问权限。

mongod实例上,即使没有killop特权操作,用户也可以终止自己的操作。

给本页内容打分