All Products
Search
Document Center

How do I query and limit the number of connections?

Last Updated: Aug 11, 2021

Overview

This topic describes how to query the connection usage and how to specify the maximum number of connections for a connection pool.

Background information

Alibaba Cloud reminds you that:

  • Before you perform high-risk operations such as modifying instance configurations or data, we recommend that you check the disaster recovery and fault tolerance capabilities of the instances to ensure data security.
  • You can modify the configurations and data of instances including Elastic Compute Service (ECS) and ApsaraDB RDS instances. We recommend that you create snapshots or enable RDS log backup before you modify instance configurations or data.
  • If you have authorized or submitted sensitive information such as the logon account and password in the Alibaba Cloud Management console, we recommend that you modify the information in a timely manner.

Query the number of current connections

The maximum number of connections varies based on the specifications of the ApsaraDB for MongoDB instance that you purchase. For more information, see Instance types.
Note: The maximum number of connections applies to each node of the instance. For example, if you purchase a three-node replica set instance that has 1 vCPU and 2 GiB memory, up to 500 connections are allowed for each of the primary and secondary nodes of the instance. The hidden node does not provide services for external systems due to the special architecture of hidden nodes.
  1. Use the mongo shell to connect to the instance. For more information, see Connect to an ApsaraDB for MongoDB instance
  2. Execute the following SQL statement to query the number of connections of the current instance:
    db.serverStatus().connections
    A similar output is returned:
    {
            "current" : 1,
            "available" : 999,
            "internal_current" : 10,
            "internal_available" : 990,
            "totalCreated" : 632
    }             
    Note: Take note of the following parameters and the values of the parameters.
    • "current": the number of established connections.
    • "available": the number of available connections.

Query the source IP addresses of the current connections

  1. Use the mongo shell to connect to an ApsaraDB for MongoDB instance. For more information, see Connect to an ApsaraDB for MongoDB instance.
  2. Execute the following SQL statement to switch to the admin database:
    use admin
  3. Execute the following SQL statement to query the source IP addresses of the current connections:
    db.runCommand({currentOp: 1, $all:[{"active" : true}]})
    Note: You can query the source IP address of each connection in the command output. This way, you can obtain the number of connections that are established between each terminal and the ApsaraDB for MongoDB instance. For more information, visit db.currentOp().

Limit the number of connections

You can use a connection string URI to connect to an ApsaraDB for MongoDB instance. If you use a connection string URI to connect to a database, append &maxPoolSize=<integer> to the URI. maxPoolSize specifies the maximum number of connections in the connection pool. The following code shows how to use the mongo shell to connect to the database:

mongo "mongodb://root:xxxxxx@dds-bpxxxxxxxx-pub.mongodb.rds.aliyuncs.com:3717,dds-bpxxxxxxxx-pub.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-xxxxxx&maxPoolSize=[$Integer]"

Note:

  • For more information about how to limit the number of connections in the connection pool for clients in different programming languages, visit Start Developing with MongoDB.
  • [$Integer] specifies the maximum number of connections for a connection pool.

Application scope

  • ApsaraDB for MongoDB