After you create an ApsaraDB for MongoDB instance, you must connect to it using a client tool.
Prerequisites
You have configured a whitelist as described in the Quick Start.
The instance is in the Running state.
Procedure
DMS
Go to the ApsaraDB for MongoDB replica set instances page or the ApsaraDB for MongoDB sharded cluster instances page. At the top of the page, select a resource group and a region. Then, click the ID of the target instance.
In the upper-right corner of the page, click Log On.
For a replica set instance: In the drop-down list, select Primary. You are redirected to the Data Management (DMS) console.
NoteSecondary nodes support only read operations. To write data, select the primary node.
For a sharded cluster instance: In the drop-down list, select any node. You are redirected to the DMS console.
For a standalone instance: No selection is required. You are redirected to the DMS console.
In the Log on to Database Instance dialog box in the DMS console, enter the Database Account and Database Password. Keep the other settings as their defaults.
NoteIf you have forgotten the password or did not set one when you created the instance, go to the ApsaraDB for MongoDB console to reset the password.
Click Test Connection. In the success dialog box, click OK.
Click Login.
When you query data using DMS, the maximum size of a single document is 512 KB. This limit cannot be adjusted. To query larger documents, you can use the MongoDB Shell.
MongoDB Shell
Install MongoDB Shell on a local device or an ECS instance
MongoDB provides two command-line interface (CLI) tools: MongoDB Shell (mongosh) and mongo shell. The mongo shell is the legacy client tool, and MongoDB Shell is the upgraded tool. This topic uses MongoDB Shell as an example.
On your client, download MongoDB Shell.
Obtain a connection address
Go to the ApsaraDB for MongoDB replica set instances page or the ApsaraDB for MongoDB sharded cluster instances page. At the top of the page, select a resource group and a region. Then, click the ID of the target instance.
In the navigation pane on the left, click Database Connections.
On the Database Connections page, obtain the required connection address.
This topic uses the High-availability Connection String URI in the Private Connection - VPC section as an example. To connect to the instance over the internet, you must first request a public endpoint.
mongodb://root:****@dds-bp1d9a7c2908e****.mongodb.rds.aliyuncs.com:3717,dds-bp1d9a7c2908e****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-8970****Replace
root:****with your database account and password. If you have forgotten the password or did not set one when you created the instance, go to the ApsaraDB for MongoDB console to reset the password.
Connect to the database
On the client, run the following command to connect to the MongoDB instance.
If you use mongo shell, replace mongosh with mongo in the command.
mongosh "<High-availability connection string URI>"The following example shows how to connect to the instance. The database account is root, the password is PassWord123!, and the authentication database is admin.
mongosh "mongodb://root:PassWord123%21@dds-bp1d9a7c2908e****.mongodb.rds.aliyuncs.com:3717,dds-bp1d9a7c2908e****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-8970****"Special characters in the connection string password must be escaped. For example, the exclamation point (!) in the example password must be escaped as %21. For more information, see How do I resolve a connection failure that is caused by special characters in the username or password of a connection string?
Program code
Next step
References
Do not use the root account to connect to an instance in a production environment. Instead, create other database accounts and grant them the required permissions. If you connect to an instance using an account that does not have the required permissions, you cannot perform any database operations. An error message such as Exception authenticating... is returned.