You can use DMS or the mongo shell to log on to ApsaraDB for MongoDB. This topic describes how to query the connection usage and how to set the maximum number of connections for a connection pool.

Query the number of current connections

The maximum number of connections depends on the specifications of the ApsaraDB for MongoDB instance you purchased. For more information, see Instance specifications.
Note The maximum number of connections takes effect on each node in the instance. For example, if you have purchased a three-node replica set instance with one core and 2 GB memory, the maximum number of connections of the primary and secondary nodes of the instance is 500, respectively. The hidden node does not provide services for external systems due to its special architecture.
Use the mongo shell to connect to the instance. For more information, see Connect to an ApsaraDB for MongoDB instance. Then, run the db.serverStatus().connections command.
mgset-123456:PRIMARY> db.serverStatus().connections
{
        "current" : 1,
        "available" : 999,
        "internal_current" : 10,
        "internal_available" : 990,
        "totalCreated" : 632
}             
Note You need to pay attention to the following parameters and their values.
  • "current": indicates the number of established connections.
  • "available": indicates the number of available connections.

Query the source IP addresses of 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. Then, switch to the admin database.
    use admin
  2. Run the db.runCommand({currentOp: 1, $all: true}) command.
    mgset-123456:PRIMARY> db.runCommand({currentOp: 1, $all:[{"active" : true}]})                    

You can query the source IP address of each connection by analyzing the command output. In this way, you can learn the number of connections that have been established between each terminal and the ApsaraDB for MongoDB instance. For more informations, see db.currentOp().

How to query and limit the number of connections

You can use a connection string URI to connect to ApsaraDB for MongoDB. If you use a connection string URI to connect to a database, append &maxPoolSize=<integer> to the URI. In this way, you can set the maximum number of connections in the connection pool.

The following code is an example of using the mongo shell to connect to the database (the maximum number of connections is set to 10):
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=10"
Note For more information about how to limit the number of connections in the connection pool for clients in different programming languages, see MongoDB API documentation.