Database Manual / Reference / Database Commands / Administration

dropConnections (database command数据库命令)

Definition定义

dropConnections
The dropConnections command drops the mongod / mongos instance's outgoing connections to the specified hosts.dropConnections命令删除mongod/mongos实例到指定主机的传出连接。 The dropConnections must be run against the admin database.dropConnections必须针对管理员数据库运行。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has following syntax:该命令具有以下语法:

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

Command Fields命令字段

The command requires the following field:该命令需要以下字段:

Field字段Type类型Description描述
hostAndPortarray数组Each array element represents the hostname and port of a remote machine.每个数组元素代表远程计算机的主机名和端口。
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

Access Control访问控制

If the deployment enforces authentication/authorization, the dropConnections command requires the dropConnections action on the cluster resource.如果部署强制执行身份验证/授权,则dropConnections命令需要对群集资源执行dropConnections操作。

Create a user-defined role in the admin database where the privilege array includes the following document:在管理员数据库中创建一个用户定义的角色,其中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:例如,以下操作在admin数据库上创建了一个用户定义的角色,该角色具有支持dropConnections的权限:

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

Assign the custom role to a user on the admin database:将自定义角色分配给admin数据库上的用户:

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

The created user can execute dropConnections.创建的用户可以执行dropConnections

For more examples of user creation, see Create a User on Self-Managed Deployments. 有关用户创建的更多示例,请参阅在自我管理部署上创建用户For a tutorial on adding privileges to an existing database user, see Modify Access for an Existing User.有关向现有数据库用户添加权限的教程,请参阅修改现有用户的访问权限

Behavior行为

dropConnections silently ignores hostAndPort elements that do not include both the hostname and port of the remote machine.默认忽略不包括远程计算机的主机名和端口的hostAndPort元素。

Example示例

Consider a replica set with a recently removed member at oldhost.example.com:27017. Running the following dropConnections command against each active replica set member ensures there are no remaining outgoing connections to oldhost.example.com:27017:考虑在oldhost.example.com:27017处具有最近删除的成员的副本集。对每个活动副本集成员运行以下dropConnections命令可确保没有剩余的到oldhost.example.com:27017的传出连接:

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

The command returns output similar to the following:该命令返回类似于以下内容的输出:

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

You can confirm the status of the connection pool for the mongod or mongos using the connPoolStats command.您可以使用connPoolStats命令确认mongodmongos的连接池状态。