This topic describes how to query the connection usage and how to specify the maximum number of connections for a connection pool. You can use Data Management (DMS) or the mongo shell to log on to ApsaraDB for MongoDB databases.
Query the number of current connections
db.serverStatus().connections
command. mgset-123456:PRIMARY> db.serverStatus().connections
{
"current" : 1,
"available" : 999,
"internal_current" : 10,
"internal_available" : 990,
"totalCreated" : 632
}
- "current": the number of established connections.
- "available": the number of available connections.
Query the source IP addresses of the current connections
- 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
- 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 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.
mongo "mongodb://test:****@dds-bp19f409d7512****-pub.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****-pub.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****&maxPoolSize=10"