ApsaraDB for MongoDB provides a connection string for each mongos node, each shard node, and the ConfigServer node in a sharded cluster instance. ApsaraDB for MongoDB also provides a Uniform Resource Identifier (URI) for each sharded cluster instance. The URI of a sharded cluster instance is used to establish a highly available connection between your application and the instance. This topic describes how to view the connection strings and URIs of a sharded cluster instance and how to log on to a specific database of a sharded cluster instance.
View the connection strings and URIs of a sharded cluster instance
Log on to the ApsaraDB for MongoDB console.
In the left-side navigation pane, click Sharded Cluster Instances.
In the upper-left corner of the Sharded Cluster Instances page, select the resource group and region to which the desired instance belongs.
Click the ID of the instance or click Manage in the Actions column.
In the left-side navigation pane of the instance details page, click Database Connections. In the page that appears, view the connection information of a node in the instance.
Connection strings and URIs
Item | Description |
Connection address type |
|
SRV high availability endpoint | Important
Format:
Parameters in the preceding command:
Example: In the following sample command, the username is test and the database is admin.
|
URI | Important If your application is deployed in a production environment, we recommend that you use a URI to connect to the sharded cluster instance. This way, your client can automatically distribute requests to mongos nodes to balance the loads within the sharded cluster instance. If a mongos node fails, your client can automatically redirect requests to other healthy mongos nodes. Format:
Parameters in the preceding command:
Example: In the following sample command, the username is test and the database is admin.
|
Connection string | For mongos, shard, and ConfigServer nodes, connection strings are provided for primary and secondary nodes. For shard nodes, connection strings are provided for read-only nodes. Note You can view the connection strings of read-only nodes only when the sharded cluster instance includes the nodes. Format:
Parameters in the preceding command:
Example:
Note When you perform routine testing, you need only to connect to a mongos node. To connect to a shard or ConfigServer node, apply for an endpoint for the node. For more information, see Apply for an endpoint for a shard or ConfigServer node. |
Log on to a specific database of a sharded cluster instance
Obtain the following information:
The connection strings or URIs of the sharded cluster instance. For more information, see View the connection strings and URIs of a sharded cluster instance.
The database account that is used to connect to the sharded cluster instance. The initial account is the root account.
ImportantWe recommend that you do not connect to a database in the production environment by using the root account. You can create database accounts and grant permissions to the accounts based on your business requirements. For more information, see Manage the permissions of MongoDB database users.
The password of the database account that is used to connect to the sharded cluster instance. If you do not specify a password for the database account or you forget the password of the account, you can set or change the password of the account. For more information, see (Optional) Reset a password.
The name of the database to which you want to connect. If the username is root, enter admin.
Log on to a database For more information, see the following topics:
Common connection scenarios
Connect to an ApsaraDB for MongoDB instance over the Internet
Connect an ECS instance to an ApsaraDB for MongoDB instance when their network types are different
Connect an ECS instance in a region to an ApsaraDB for MongoDB instance in another region
Connect an ECS instance to an ApsaraDB for MongoDB instance in another Alibaba Cloud account