This topic describes how to connect your local client to a MongoDB instance over the public network.
Prerequisites
The public IP address of your local client has been obtained.
The public IP address of your local client has been added to the MongoDB whitelist.
Step 1: Apply for and view a public endpoint
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.
In the navigation pane on the left, click Database Connections.
In the Public Network Connection section, click Apply for Public Endpoint.
In the Apply for Public Endpoint dialog box, click OK.
Wait for the instance status to change from Creating Network Connection to Running. You can then view the public endpoint of the MongoDB instance.
NoteThe 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 |
| 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 |
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 |
| The endpoint and port of the node that you obtained from the MongoDB console. Example: |
| 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. |
| 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 testdbAt the Enter password: prompt, enter the password for the database account and press the Enter key.
