dropConnections
New in version 4.2.
The dropConnections
command drops themongod
/mongos
instance’s outgoingconnections to the specified hosts. The dropConnections
must be run against the admin
database.
The command has following syntax:
- db.adminCommand({
- dropConnections: 1,
- hostAndPort : [ "host1:port1", "host2:port2", ... ]
- })
The command requires the following field:
FieldTypeDescriptionhostAndPort
arrayEach array element represents the hostname andport of a remote machine.
Access Control
If the deployment enforcesauthentication/authorization,the dropConnections
command requires thedropConnections
action on thecluster resource.
Create a user-defined role in the admin
database where the privilege
array includes the following document:
- { "resource" : { "cluster" : true } }, "actions" : [ "dropConnections" ] }
- Use
db.createUser()
to create a user on theadmin
database with the custom role.
or
- Use
db.grantRolesToUser()
to grant the role to an existinguser on theadmin
database.
For example, the following operation creates auser-defined role on the admin
database with the privilegesto support 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:
- db.getSiblingDB("admin").createUser(
- {
- "user" : "dropConnectionsUser",
- "pwd" : "replaceThisWithASecurePassword",
- "roles" : [ "dropConnectionsRole" ]
- }
- )
The created user can execute dropConnections
.
For more examples of user creation, see Add Users.For a tutorial on adding privileges to an existing database user, seeModify Access for an Existing User.
Behavior
dropConnections
silently ignores hostAndPort
elementsthat do not include both the hostname and port of the remote machine.
Example
Consider a replica set with a recently removed member atoldhost.example.com:27017
. Running the followingdropConnections
command against each activereplica set member ensures there are no remaining outgoing connectionsto 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" : NumberLong(0)
- }
- },
- "operationTime" : Timestamp(1551375968, 1)
- }
You can confirm the status of the connection pool for themongod
or mongos
using theconnPoolStats
command.