All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to a MongoDB instance from a local client over the public network

Last Updated:Jan 21, 2026

This topic describes how to connect your local client to a MongoDB instance over the public network.

Prerequisites

Step 1: Apply for and view a public endpoint

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

  2. In the navigation pane on the left, click Database Connections.

  3. In the Public Network Connection section, click Apply for Public Endpoint.

  4. In the Apply for Public Endpoint dialog box, click OK.

  5. Wait for the instance status to change from Creating Network Connection to Running. You can then view the public endpoint of the MongoDB instance.

    Note
    • The instance operates normally while it is in the Creating Network Connection state.

    • The endpoint is visible only after you configure the whitelist.

    • Use the high-availability (HA) ConnectionStringURI endpoint.

Step 2: Connect to the MongoDB instance

This section uses the Mongo Shell tool to demonstrate how to connect to a MongoDB instance.

Run the following command on your local client to connect to the MongoDB instance.

ConnectionStringURI for high availability (Recommended)

Command:

mongo "<ConnectionStringURI for high availability>"

Parameters:

Parameter

Description

<ConnectionStringURI for high availability>

The ConnectionStringURI for high availability that you obtained from the MongoDB console.

Note

Do not connect to the database using the root account. Instead, create a database account in DMS before you connect.

When you use the endpoint, 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 endpoint

Command:

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

Parameters:

Parameter

Description

<host> and <port>

The endpoint and port of the node that you obtained from the MongoDB console.

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

<username>

The name of the database account.

Note

Do not connect to the database using the root account. Instead, create a database account in DMS before you connect.

<authenticationDatabase>

The name of the authentication database where the database account is located.

Important

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

Example:

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 the Enter key.

FAQ

Why can't I connect even after adding my IP to the whitelist?

If you are unable to connect to MongoDB, you may be using an incorrect public IP address. Follow these steps to find the public IP address of your on-premises device:

  1. Temporarily add the IP address 0.0.0.0/0 to the whitelist of the MongoDB instance.

    Warning

    The IP address 0.0.0.0/0 allows access from any IP address and poses a security risk. Use this IP address with caution. If you use it, remove it from the whitelist promptly.

  2. Log on to the MongoDB instance using Mongo Shell.

    If you still cannot connect to the MongoDB instance after you add the IP address 0.0.0.0/0 to the whitelist, try to connect to the MongoDB instance using DMS. Check whether the account, password, and authentication database are correct.

  3. Run the following command to query information about the Mongo Shell client.

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

    Example:客户端IP查询

    Note

    If you log on to the MongoDB instance using other methods, you can run the following command to query information about all clients.

    db.runCommand({currentOp: 1, "active" : true})
  4. Add the obtained IP address to the whitelist of the MongoDB instance, and then delete the IP address 0.0.0.0/0 from the whitelist.

What if my IP address changes and I can no longer connect?

References