After the number of connections to an ApsaraDB for MongoDB instance reaches the limit, new connection requests cannot be responded. This topic describes how to handle database connection failures after the number of connections reaches the upper limit.

The maximum number of connections varies depending on ApsaraDB for MongoDB instance types. For more information, see Instance specifications.

Symptom

  • The application fails to connect to the database.
  • The whitelist has been properly set. However, the following error message is displayed when you use the mongo shell to connect to the database:
    2019-07-10T10:30:43.597+0800 E QUERY    [js] Error: network error while attempting to run command 'isMaster' on host 'dds-bpxxxxxxxx.mongodb.rds.aliyuncs.com:3717'  :
    connect@src/mongo/shell/mongo.js:328:13
    @(connect):1:6
    exception: connect failed
  • The whitelist has been properly set. However, the following error message is displayed when you use DMS to connect to the database.

Before you begin

To obtain the required monitoring information, we recommend that you set the monitoring granularity of the ApsaraDB for MongoDB instance to seconds. For more information, see Set the monitoring granularity.

Check whether the number of connections has reached the upper limit

  1. Log on to the ApsaraDB for MongoDB console.
  2. In the upper-left corner of the page, select the region of the instance.
  3. In the left-side navigation pane, click Replica Set Instances or Sharding Instances.
  4. Find the target instance and click its ID.
  5. In the left-side navigation pane, click Monitoring Info.
  6. On the Monitoring Info page, check the Connections information. The following figure shows that the number of connections to the instance is 500.
    Note If the instance is a sharded cluster instance, you must select the Mongos node in use in the upper-right corner on the page.
  7. In the left-side navigation pane, click Basic Information.
  8. On the Basic Information page, query the maximum number of connections corresponding to the current instance specifications. In this example, the number is 500.
    Note Based on the number of current connections, you can confirm that the number of connections has reached the upper limit.

Solution

You can restart the instance to temporarily release all connections. For more information, see Restart an instance. To prevent this problem from occurring again, we recommend that you perform the following operations after restarting the instance:
Note If you restart the instance, all instance nodes are restarted one by one. Each node has a transient disconnection of about 30 seconds. If there are a large number of collections (more than 10,000), the transient disconnections last longer. Before restarting the instance, arrange your business and ensure that your application has a reconnection mechanism.