This topic describes how to connect a local client to an ApsaraDB for MongoDB instance over the Internet.
Prerequisites
The public IP address of the local client is obtained.
The public IP address of the local client is added to a whitelist of the instance.
Step 1: Apply for and view a public endpoint
Go to the Replica Set Instances or Sharded Cluster Instances page. In the top navigation bar, select the region in which an instance resides. Then, find the instance and click its instance ID.
In the left-side navigation pane of the instance details page, click Database Connections.
In the upper-right corner of the Public Connections section, click Apply for Public Connection String.
In the Apply for Public Connection String panel, click OK.
After the instance status changes from Establishing Connection to Running, you can view the applied public endpoint.
NoteIf the instance is in the Establishing Connection state, it runs as expected.
The applied public endpoint is visible only after you configure a whitelist.
We recommend that you use a connection string URI to connect to the instance.
Step 2: Connect to an instance
This section provides examples on how to use the mongo shell to connect to an instance.
Run the following command on the local client:
Connection string URI (recommended)
Sample command:
mongo "<Connection string URI>"
The following table describes the parameter in the preceding command.
Parameter | Description |
| The connection string URI of the instance that is displayed in the ApsaraDB for MongoDB console. Note We recommend that you do not use the root account for database logon. You can use Data Management (DMS) to create a database account and then connect to a database. 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 connection string
Sample command:
mongo --host <host> --port <port> -u <username> -p --authenticationDatabase <authenticationDatabase>
The following table describes parameters in the preceding command.
Parameter | Description |
| The endpoint and domain name of the node that are displayed in the ApsaraDB for MongoDB console. Example: |
| The name of the database account. Note We recommend that you do not use the root account for database logon. You can use DMS to create a database account and then connect to a database. |
| The name of the authentication database to which the database account belongs. Important For the authentication to succeed, the specified database account must belong to the specified authentication database. |
Example:
In this example, the database account is python-test and the authentication database is admin.
mongo --host dds-bp19f409d7512****.mongodb.rds.aliyuncs.com --port 3717 -u test -p --authenticationDatabase testdb
When Enter password:
is displayed, enter the password of the database user and press the Enter key.