Help Center/ Document Database Service/ FAQs/ Database Connection/ How Do I Query and Limit the Number of Connections?
Updated on 2023-01-18 GMT+08:00

How Do I Query and Limit the Number of Connections?

The following uses a replica set instance as an example to describe how to query the connection status and set the number of connections in the connection pool.

Querying the Number of Connections

The maximum number of connections varies according to the instance specifications.

The maximum number of connections refers to the maximum number of connections of each node in an instance.

Example: If a replica set instance has two vCPUs and 4 GB memory for each node, the maximum number of connections of the primary and secondary nodes is 1000 respectively. The hidden node does not provide services because of its architecture features.

Use Mongo Shell to connect to the primary node, and run the db.serverStatus().connections command to query the number of connections on the node.

replica:PRIMARY> db.serverStatus().connections
{ "current" : 7, "available" : 398, "totalCreated" : 818364 }

Pay attention to the following parameters and their values:

  • current: Existing connections
  • available: Number of available connections.

Querying the Source of Connections

  1. Use Mongo Shell to connect to the primary node and switch to the admin database.

    replica:PRIMARY> use admin

  2. Run the db.runCommand({currentOp: 1, $all: true}) command to query the connection source.

    By analyzing the command output, you can query the source IP address of each connection. In this way, the number of connections between each client and DDS DB instance is obtained.

    Figure 1 Command output

Limiting the Number of Connections

DDS allows you to log in to the database using Connection String URI. When logging in to the database using Connection String URI, you can add &maxPoolSize=<integer> to the end of the URI to set the number of connections in the connection pool.

Example: When Mongo Shell is used to connect replica set instances, run the following command to set the number of connections in the connection pool to 10:

mongo "mongodb://rwuser:xxxxxxxxxx@192.168.168.116:8635,192.168.200.147:8635/test?authSource=admin&replicaSet=replica&maxPoolSize=10"

Figure 2 Limiting the number of connections

For details about how to limit the number of connection pools on clients in different languages, see the API documents of clients in different languages on the MongoDB official website.