All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to MongoDB over the public internet

Last Updated:Sep 09, 2026

This topic describes how to connect to an ApsaraDB for MongoDB instance over the public internet.

Prerequisites

Step 1: Apply for a public connection string

  1. Go to the Replica Set Instances or Sharded Cluster Instances page. In the top navigation bar, select the desired resource group and region. Then, click the ID of the target instance.

  2. In the left-side navigation pane of the instance details page, click Database Connections.

  3. In the Public IP Connection section, click Apply for Public IP.

  4. In the Apply for Public IP dialog box, click Confirm.

  5. Wait for the instance status to change from Establishing Network Connection to Running. You can then view the public connection string of the instance.

    Note
    • The instance continues to run normally while its status is Establishing Network Connection.

    • The connection string is visible only after you configure the allowlist.

    • Use the ConnectionStringURI for a high-availability connection.

Step 2: Connect to the instance

This example uses the MongoDB Shell (mongosh) to connect to the instance.

ConnectionStringURI (Recommended)

Command:

mongo "<ConnectionStringURI>"

Parameters:

Parameter

Description

<ConnectionStringURI>

The ConnectionStringURI for your instance, available in the ApsaraDB for MongoDB console.

Note

As a security best practice, do not connect to the database using the root account. Create a database account in Data Management (DMS) before you connect.

When you connect, replace root:**** with the username and password of the new account, such as test:PassWord**. Replace /admin with the authentication database, such as /testdb.

Example:

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

Single-node connection string

Command:

mongo --host <host> --port <port> -u <username> -p --authenticationDatabase <authenticationDatabase>

Parameters:

Parameter

Description

<host> and <port>

The Connection Address and Port of the node, available in the ApsaraDB for MongoDB console.

Example: dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

<username>

The name of the database account.

Note

As a security best practice, do not use the root account to connect to the database. Create a database account in Data Management (DMS) before you connect.

<authenticationDatabase>

The database used to authenticate the specified database account.

Important

For authentication to succeed, the database account must be authenticated against the database to which it belongs.

Example:

The following example assumes the database account is test and the authentication database is testdb.

mongo --host dds-bp19f409d7512****.mongodb.rds.aliyuncs.com --port 3717 -u test -p --authenticationDatabase testdb

At the Enter password: prompt, enter the password for the database account and press Enter.

FAQ

Why can't I connect to my instance even after adding my public IP address to the allowlist?

This issue can occur if you added an incorrect public IP address to the allowlist. Follow these steps to find the correct public IP address of your local client:

  1. Add the IP address 0.0.0.0/0 to the allowlist of the MongoDB instance.

    Warning

    0.0.0.0/0 allows any device to access the MongoDB instance. This poses a security risk and must be used with caution. If you use this entry, you must promptly remove it from the allowlist.

  2. Use MongoDB Shell (mongosh) to connect to the instance.

    If you still cannot connect to MongoDB after setting the allowlist to 0.0.0.0/0, you can connect to MongoDB by using Data Management (DMS) to check whether the username, password, and authentication database are correct.

  3. Run the following command to query the client information for your MongoDB Shell (mongosh) session.

    db.currentOp({"appName" : "MongoDB Shell","active" : true})

    After the command is run, the client field in the result indicates the public IP address of the client. Example output:

    mgset-xxx:PRIMARY> db.currentOp({"appName" : "MongoDB Shell","active" : true})
    {
        "inprog" : [
            {
                "desc" : "conn1591052",
                "threadId" : "48008022501120",
                "connectionId" : 1591052,
                "client" : "47.xxx.xxx.xxx:40602",
                "appName" : "MongoDB Shell",
                "clientMetadata" : {
                    "application" : {
                        "name" : "MongoDB Shell"
                    },
                    "driver" : {
                        "name" : "MongoDB Internal Client",
                        "version" : "4.0.5"
                    },
                    "os" : {
                        "type" : "Linux",
                        "name" : "Ubuntu",
                        "architecture" : "x86_64",
                        "version" : "16.04"
                    }
                },
            },
            "active" : true,
    
    Note

    If you connect to the instance using a different client, run the following command to query information for all active clients.

    db.runCommand({currentOp: 1, "active" : true})
  4. Add the obtained IP address to the MongoDB instance allowlist, and remove the IP address 0.0.0.0/0.

My local machine has a dynamic public IP address that changes, causing connection failures. What is the solution?

What do I do if access is slow, the connection freezes, or an ECONNRESET error occurs when I connect to MongoDB over the public network?

Troubleshoot a public network connection from your local client based on the symptom:

  • Access is slow and you suspect network fluctuations: Restarting the instance does not resolve a network-side issue. Observe the network conditions first. If the issue persists, troubleshoot your local network or the connection provided by your Internet service provider (ISP).

  • The TCP connection is established, but an ECONNRESET error occurs during the handshake: Check whether the instance allowlist is correctly configured. For a sharded cluster instance, check whether the number of connections to the Mongos nodes has reached the upper limit. If the limit is reached, restart the instance to release connections or upgrade the instance specifications. Then, temporarily connect by using the single-node connection string to rule out a load balancing issue. Use the single-node connection string only for troubleshooting, not as the regular connection method.

  • A client such as Apache SeaTunnel reports No server chosen or a connection timeout: The driver may fail to identify the primary node. Use the complete ConnectionStringURI for a high-availability connection, which includes the replicaSet parameter. Alternatively, temporarily specify only the connection string of the primary node for testing. After you identify the issue, resume using the complete ConnectionStringURI.

  • A data synchronization task freezes: Check the client logs for out-of-memory (OOM) or connection loss errors. Then, analyze the MongoDB connection logs to determine whether the client timed out because it could not identify the primary node. Change the connection configuration to use the complete ConnectionStringURI with the replicaSet parameter for a high-availability connection, or temporarily connect only to the primary node for testing.

Related documents