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

dropConnections(数据库命令)

dropConnections

dropConnections命令删除mongod /mongos 实例到指定主机的传出连接。dropConnections 必须针对admin 数据库运行。

此命令可用于以下环境中托管的部署:

该命令的语法如下:

db.adminCommand(
{
dropConnections: 1,
hostAndPort : [ "host1:port1", "host2:port2", ... ],
comment: <any>
}
)

该命令需要以下字段:

字段
类型
说明

hostAndPort

阵列

每个数组元素代表远程计算机的主机名和端口。

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

If the deployment enforces authentication/authorization, the dropConnections command requires the dropConnections action on the cluster resource.

admin数据库中创建用户定义的角色,其中privilege数组包含以下文档:

{ "resource" : { "cluster" : true } }, "actions" : [ "dropConnections" ] }

For example, the following operation creates a user-defined role on the admin database with the privileges to support dropConnections:

db.getSiblingDB("admin").createRole(
{
"role" : "dropConnectionsRole",
"privileges" : [
{
"resource" : { "cluster" : true },
"actions" : [ "dropConnections" ]
}
],
"roles" : []
}
)

将自定义角色分配给admin数据库中的用户:

db.getSiblingDB("admin").createUser(
{
"user" : "dropConnectionsUser",
"pwd" : "replaceThisWithASecurePassword",
"roles" : [ "dropConnectionsRole" ]
}
)

创建的用户可以执行dropConnections

有关用户创建的更多示例,请参阅在自托管部署上创建用户。 有关向现有数据库用户添加权限的教程,请参阅修改现有用户的访问权限。

dropConnectionshostAndPort静默忽略不包含远程计算机主机名和端口的 元素。

考虑一个副本集,其成员最近在oldhost.example.com:27017 中删除。对每个活动的副本集节点运行以下dropConnections 命令,可确保没有指向oldhost.example.com:27017 的剩余传出连接:

db.adminCommand(
{
"dropConnections" : 1,
"hostAndPort" : [
"oldhost.example.com:27017"
]
}
)

该命令返回类似以下的输出:

{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1551375968, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : Long(0)
}
},
"operationTime" : Timestamp(1551375968, 1)
}

您可以使用 命令确认mongodmongos connPoolStats连接池的状态。