All Products
Search
Document Center

ApsaraDB for MongoDB:Connection and network

Last Updated:May 19, 2025

This topic describes how to troubleshoot issues about connections to an ApsaraDB for MongoDB instance. We recommend that you refer to the Common errors section first to quickly identify and resolve your connection issue. If you cannot find a matching error message, refer to the Connection troubleshooting steps section to perform a diagnostics and resolve the connection issues.

Connection troubleshooting steps

Step 1: Network connectivity

Troubleshooting method

Use the following commands to test network connectivity between your client and your ApsaraDB for MongoDB instance.

  • ping <Domain name>. Example: ping dds-xxxx.mongodb.rds.aliyuncs.com.

    image

    This figure shows that a network connection is established between the client and the ApsaraDB for MongoDB instance.

  • telnet <Domain name> <Port number>. Example: telnet dds-xxxx.mongodb.rds.aliyuncs.com 3717.

    测试端口This figure shows that the URL can be resolved and port 3717 works as expected.

Check list

If the network connectivity test fails, you can use one of the following methods to troubleshoot and resolve connection issues:

Check the instance status. The instance status must be Running.

Check whitelist settings. You must add the client IP address to an IP address whitelist of the ApsaraDB for MongoDB instance.

  • Troubleshooting method:

    Temporarily add the IP address 0.0.0.0/0 to the whitelist of the ApsaraDB for MongoDB instance. If you can access the ApsaraDB for MongoDB instance after configuration, issues occur in the whitelist settings.

    Important

    The IP address 0.0.0.0/0 indicates that all devices are allowed to access the ApsaraDB for MongoDB instance. Use this IP address with caution and promptly modify it after testing.

  • Solution:

Add the correct client IP address to the whitelist of the ApsaraDB for MongoDB instance. For more information, see Modify a whitelist for an instance.

Check the network environment and instance endpoints. ApsaraDB for MongoDB supports private and public network connections. Different network environments require different endpoints.

You can log on to the ApsaraDB for MongoDB console and view the endpoints of the ApsaraDB for MongoDB instance on the Database Connections page.

Step 2: Authentication and identity verification

Confirm the provided account name and password, and check whether the password contains special characters.

Check the authentication database. A database account must be authenticated in the database to which the account belongs.

Check instance endpoints. For a replica set instance that requires write operations, we recommend that you use a connection string URI to connect to the instance.

  • Symptom: The instance suddenly becomes inaccessible without changing the used instance endpoint.

  • Cause: You can perform write operations only on the primary node. If you previously use the endpoint of the primary node to connec to the instance, the connected node becomes a secondary node and write operations fail after a primary/secondary switchover.

  • Solution:

    • If your application runs in a production environment, we recommend that you use a connection string URI to connect your application to the instance. When a node fails, read/write operations on your application are not affected by a primary/secondary switchover. For more information about instance endpoints, see the following topics:

    • Manually switch the role of a node. Switch the node whose endpoint is used for connection to the new primary node.

    We recommend that you ensure that your application can automatically reconnect to an ApsaraDB for MongoDB instance after it is disconnected and handle exceptions to ensure business continuity.

Step 3: Other issues

Check whether the instance performance meets business requirements.

Check third-party tools.

When a third-party tool (such as Navicat or Compass) cannot connect to an ApsaraDB for MongoDB instance, the failed connection may be caused by incorrect instance settings or third-party tool settings. We recommend that you perform the following steps to troubleshoot this issue:

  1. Use the mongo shell to test the instance.

    We recommend that you use the mongo shell to connect to the connection string URI of the instance for testing.

  2. Troubleshoot this issue based on the test result.

    • Successful connection: This test result indicates that the ApsaraDB for MongoDB instance is accessible, and this issue may be caused by incorrect third-party tool settings. Check the settings of the used third-party tool.

    • Failed connection: If you cannot use the mongo shell to connect to the ApsaraDB for MongoDB instance, this issue may be caused by incorrect instance settings. Refer to this topic to check the instance settings, such as the network connectivity, authentication, identity verification.

Common errors

Error message

Cause and solution

network error while attempting to run command 'isMaster' on host 'dds-xxxx.mongodb.rds.aliyuncs.com:3717' :exception: connect failed

  • Timed out after 3000ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. exception=(com.mongodb.MongoSocketReadException: Prematurely reached end of stream)

  • Socket recv() errno:54 Connection reset by peer x.x.x.x:27017

The number of connections to the ApsaraDB for MongoDB instance may have reached the upper limit, and no new connections can be established.

Solution:

  1. Check whether the instance connections are exhausted. For more information, see How do I query the number of connections to my instance?

  2. Optimize connection usage. For more information, see What do I do if the number of connections to my instance reaches the upper limit?

MongoDB.Driver.MongoWaitQueueFullException: The wait queue for acquiring a connection to server xxx is full.

The wait queue of the MongoDB driver is full, which may be caused by a small number of connections specified for a connection pool or high concurrent requests. Therefore, no available connections are provided.

Solution:

  1. We recommend that you first check the connection pool settings of the program for which this error is reported. For example, make sure that a reasonable number of connections is specified for a connection pool. For more information, see How do I limit the number of connections from my client?

  2. If this issue persists after you adjust the program, check whether the ApsaraDB for MongoDB instance has exhausted its connections. For more information, see the following topics:

    1. How do I query the number of connections to my instance?

    2. What do I do if the number of connections to my instance reaches the upper limit?

(TooManyLogicalSessions) Unable to add session into the cache because the number of active sessions is too high.

When an excessive number of concurrent connections are established, available sessions may be exhausted.

Solution:

  1. Troubleshoot failed connection issues caused by exhausted connections. For more information, see the following topics:

    1. How do I query the number of connections to my instance?

    2. What do I do if the number of connections to my instance reaches the upper limit?

  2. If no issues occur in the terms of the number of connections, check whether other instance performance items cannot meet business requirements.

    1. Use the node monitoring feature to check the usage of common resources (such as CPU utilization and memory usage) and then determine whether the instance specifications meet business requirements.

    2. If the instance specifications are excessively small but the load is high, you can change the instance configurations during off-peak hours.

  • getaddrinfo failed.

  • No suitable servers found (`serverSelectionTryOnce` set).

Check whether the used instance endpoint is correct. For more information about instance endpoints, see the following topics:

  • Failed to connect to 10.*.*.8:3717 after 5000 milliseconds, giving up.Error: couldn't connect to server 10.*.*.8:3717 (10.*.*.8), connection attempt failed

  • pymongo.errors.ServerSelectionTimeoutError: dds-xxxx.mongodb.rds.aliyuncs.com:3717: [Errno 113] No route to host,dds-xxxx.mongodb.rds.aliyuncs.com:3717

  • InvalidInstanceId.NotFound: The instance not in current vpc.

The ECS instance fails to connect to the ApsaraDB for MongoDB instance over a private network.

Solution:

  1. Check whitelist settings. Make sure that the private IP address of the ECS instance is added to an IP address whitelist of the ApsaraDB for MongoDB instance.

  2. Ensure network connectivity between the ECS instance and the ApsaraDB for MongoDB instance.

    If the ECS instance and the ApsaraDB for MongoDB instance are in the same VPC, the two instances can directly interconnect over a private network. To connect the ECS instance to the ApsaraDB for MongoDB instance across VPCs, you can use one of the following methods:

org.springframework.data.mongodb.UncategorizedMongoDbException: Timeout while receiving message; nested exception is com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message

  • "errmsg": "not master", "code": 10107, "codeName": "NotMaster"

  • "errmsg": "not master", "code": 10107, "codeName": "NotWritablePrimary"

  • Time out after 30000ms while waiting for a server that matches writableServerSelector.

  • Command failed with error 10107 (NotWritablePrimary): 'not primary' on server xxx.

  • Explain's child command cannot run on this node. Are you explaining a write command on a secondary?

  • not master and slaveOk=false.

  • MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server xxx.

  • reason: TopologyDescription { type: 'ReplicaSetNoPrimary',...}

Data is not written to the primary node in the replica set instance.

Cause: You can perform write operations only on the primary node. If you previously used the endpoint of the primary node for connection, the connected node becomes a secondary node and write operations fail after a primary/secondary switchover.

Solution:

  • If your application runs in a production environment, we recommend that you use a connection string URI to connect your application to the instance. When a node fails, read/write operations on your application are not affected by a primary/secondary switchover. For more information about instance endpoints, see the following topics:

  • Manually switch the role of a node. Switch the node whose endpoint is used for connection to the new primary node.

We recommend that you ensure that your application can automatically reconnect to an ApsaraDB for MongoDB instance after it is disconnected and handle exceptions to protect business continuity.

[Unauthorized] cloud instance error, disk locked, plz check and upgrade your disk quota,

The ApsaraDB for MongoDB instance is locked due to exhausted disk space.

Solution: For more information, see What do I do if my instance is locked or data cannot be written to the instance due to exhausted disk space?

(AuthenticationFailed) Authentication failed.

  • !xxx@dds-xxx.mongodb.rds.aliyuncs.com: event not found

The password of the provided database account contains special characters. Special characters include ! @ # $ % ^ & * ( ) _ + =

Solution: For more information, see How do I resolve failed connection issues due to special characters in the account name or password in a connection string?

error getting cluster ID: (CommandNotFound) replSetGetConfig is forbidden by cloud provider for security reason

ApsaraDB for MongoDB does not support the replSetGetConfig command. For more information, see What commands are supported and not supported by ApsaraDB for MongoDB?

FAQ

How do I query the number of connections to my instance?

The maximum number of connections varies based on the instance type of your ApsaraDB for MongoDB instance. For more information, see Instance types.

Note

The maximum number of connections is specific to each node in an ApsaraDB for MongoDB instance. For example, if you purchase a three-node replica set instance with 1 CPU core and 2 GB of memory, the maximum number of connections for the primary or secondary node in the instance is 500. Hidden nodes in the instance do not provide services due to their special architecture.

View connections in the ApsaraDB for MongoDB console

  1. Go to the Replica Set Instances or Sharded Cluster Instances page of the ApsaraDB for MongoDB console, select a region at the top, find the instance that you want to manage, and then click the instance ID.

  2. In the left-side navigation pane of the instance details page, click Monitoring Data.

  3. On the Node Monitoring tab, view the Connections value.

    If the instance is a sharded cluster instance, you must select the Mongos node that is currently connected in the upper-right corner of the tab.

View connections by using a command

  1. Connect to the desired instance by using the mongo shell.

  2. View the number of connections to the instance.

    Run the db.serverStatus().connections command.

    mgset-123456:PRIMARY> db.serverStatus().connections
    {
            "current" : 1,
            "available" : 999,
            "internal_current" : 10,
            "internal_available" : 990,
            "totalCreated" : 632
    }             

    Parameters:

    • "current": The number of connections that are currently established.

    • "available": The number of connections that are currently available.

  3. Query the current connection sources.

    1. 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}]})                    

    By analyzing the command output, you can query the source IP address of each connection to obtain the number of connections from each client. For more information, see db.currentOp().

How do I limit the number of connections from my client?

When connecting to a MongoDB database by using a connection string URI, you can specify the number of connections in a connection pool by adding &maxPoolSize=<integer> at the end of the URI. The following sample code shows an example on how to use the mongo shell to connect to a MongoDB database. In this example, the number of connections to the database is set to 10, the database account is test, and the database is admin.

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"
Note

For more information about how to limit the number of connections in a connection pool from clients in different languages, see Start Developing with MongoDB.

What do I do if the number of connections to my instance reaches the upper limit?

You can restart your instance, its components, or its nodes to temporarily release all connections.

Important

When you restart an ApsaraDB for MongoDB instance, all its nodes are restarted in turn and each node has a 30-second disconnection. If the instance houses more than 10,000 collections, the disconnection last longer. Therefore, we recommend that you restart an ApsaraDB for MongoDB instance during off-peak hours or make sure that your application can reconnect to the instance after it is disconnected.

To prevent this issue from reoccurring, we recommend that you use one of the following methods for adjustments after you restart the instance.

How do I resolve failed connection issues due to special characters in the account name or password in a connection string?

When connecting to an ApsaraDB for MongoDB instance through the mongo shell or program code, failed connection issues occur if the account or password in the used connection string contains special characters. These special characters cannot be recognized during connection. Special characters include ! @ # $ % ^ & * ( ) _ + =

Solution: You must escape the special characters in the connection string based on the following rules:

! : %21
@ : %40
# : %23
$ : %24
% : %25
^ : %5e
& : %26
* : %2a
( : %28
) : %29
_ : %5f
+ : %2b
= : %3d

Example: If the password is ab@#c, escape the special characters in the used connection string. The password is ab%40%23c after escaping.

How do I specify an authentication database in a connection?

The authentication database is used to store database accounts and passwords. A database account must be authenticated in the database to which the account belongs.

Assume that both the account and password are test, and the test account belongs to the testdb database.

High-availability endpoint

When using a connection string URI or read-only connection string URI to connect to a MongoDB database, you must set the authSource parameter to specify the database to which the database account belongs. If you do not configure this parameter, the database specified by the <database> parameter is used as the authentication database.

Example:

  • After the test account is authenticated in the testdb database, the account is switched to the <database> database.

mongo "mongodb://test:test@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>&authSource=testdb"
  • After the test account is authenticated in the testdb database, the account is switched to the testdb database.

mongo "mongodb://test:test@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/testdb?replicaSet=<replicaSet_value>"

Single-node endpoint

You can run one of the following commands to log on to a MongoDB database:

mongo --host $myhost --port $myport -u test -p test --authenticationDatabase testdb

Or

    mongo --host $myhost --port $myport
    mongo> use testdb
    mongo> db.auth("test", "test")

For more information about the parameters in an endpoint, see Connect to a replica set instance and Connect to a sharded cluster instance.

How do I log on to an ApsaraDB for MongoDB instance by using DMS?

For more information about how to connect to ApsaraDB for MongoDB instances of different architectures, see the following topics:

What client languages are supported by ApsaraDB for MongoDB for connections?

ApsaraDB for MongoDB is fully compatible with official MongoDB. This means that all clients supported by official MongoDB are supported by ApsaraDB for MongoDB.

For example, C, C++, C#, Java, Node.js, Python, PHP, Perl, and other languages are supported by ApsaraDB for MongoDB. For more information, see Start Developing with MongoDB.

For more information about sample program code, see the following topics:

Note

To ensure normal authentication when connecting to an ApsaraDB for MongoDB instance, use a MongoDB driver of V3.0 or later.

Does ApsaraDB for MongoDB support public network access?

Does ApsaraDB for MongoDB support password-free access?

ApsaraDB for MongoDB no longer allows you to enable the password-free access feature in a VPC network.

For instances with the password-free access feature enabled, you can disable this feature. However, it cannot be re-enabled once disabled. For more information about how to disable this feature, see Disable password-free access over VPC.

How do I install and use the mongo shell in a Windows operating system?

  • Install the mongo shell. Visit the MongoDB Shell Download page and follow the page instructions for download and installation.

  • Use the mongo shell.

    • Open a command prompt window.

    • Connect to your ApsaraDB for MongoDB instance.

      Example:

      mongosh "mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****"    

      For more information about the parameters in an endpoint, see Connect to a replica set instance and Connect to a sharded cluster instance.

Common connection scenarios