edit-icon download-icon

Connection number issue

Last Updated: Jan 29, 2018

You can use DMS or Mongo shell to connect to ApsaraDB for MongoDB. The following scenarios use Mongo shell to connect to ApsaraDB for MongoDB:

How can I view the current number of connections?

  • The maximum number of connections varies depending on the type of the MongoDB instance that you purchased. For more information about the maximum number of connections, see Instance type.
  • Run the following command to connect to the instance using Mongo shell (current indicates the number of established connections, available indicates the number of available connections, and internal indicates connections for internal management, which can be ignored):

    1. mongo-test:PRIMARY> db.serverStatus().connections
    2. {
    3. "current" : 0,
    4. "available" : 2000,
    5. "internal_current" : 3,
    6. "internal_available" : 497,
    7. "totalCreated" : NumberLong(21)
    8. }

How can I view the ECS instances where the current connections come from?

When you use Mongo shell or DMS to connect to the instance (if the number of connections reaches the upper limit, you can view the connection status only by logging on to DMS), run db.runCommand({currentOp: 1, $all: true}) to output the status of all connections that are established to the instance. The client field contains the IP address information of the ECS instance.

  1. > db.runCommand({currentOp: 1, $all: true})
  2. {
  3. "inprog" : [
  4. {
  5. "desc" : "conn20",
  6. "threadId" : "140353731274496",
  7. "connectionId" : 20,
  8. "client" : "10.1.2.7:28788",
  9. "active" : false
  10. },
  11. ...

With the preceding information, you can further analyze the source IP addresses to know the number of connections between each ECS instance and the MongoDB instance.

The number of connections in the production environment almost reaches the upper limit. How can I limit the number of connections between each ECS instance and the MongoDB instance?

Generally, all MongoDB drivers support using connection string URI to connect to instances. Connection URIs of ApsaraDB are generated on the console. You can directly use the URIs after adding passwords.

If you use a URI to connect to the instance, append &maxPoolSize=xx to the URI to restrict the number of connections to the instance. For example, if you have 10 ECS instances for concurrent access and the maximum number of connections to the instance is 1000, limit the number of connection pools on each ECS instance within 100.

Clients of different languages encapsulate different connection methods, but certain methods can be used to limit the number of connection pools. For more information, see the API document for the client of the corresponding language.

Thank you! We've received your feedback.