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 URI of a sharded cluster instance. This topic also describes how to log on to a specific database of a sharded cluster instance.
View the connection strings and URI 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 page, select the resource group and region to which the instance belongs.
Click the ID of an instance, or click in the Actions column corresponding to the instance and select Manage.
In the left-side navigation pane of the instance details page, click Database Connections. Obtain the connection information of the node.
Introduction to connection strings and URIs
Item | Description |
Connection address type |
|
Connection string | For mongos, shard, and Configserver nodes, connection strings are provided for both primary and secondary nodes. For shard modes, connection strings are also provided for read-only nodes. Note The connection string of a read-only node is displayed only if the instance contains the read-only node. Format:
The following table describes the parameters used in the syntax.
Example:
Note For routine tests, you can connect only to either primary or secondary node of a mongos node. To connect to a shard node or a Configserver node, you must apply for a connection string for the node. For more information, see Apply for an endpoint for a shard or Configserver node |
URI | Important If your application is deployed in a production environment, we recommend that you use the URI to connect to the sharded cluster instance. This way, your client can automatically distribute requests to primary and secondary nodes of mongos nodes to balance the loads within the sharded cluster instance. If the primary or secondary node of the mongos node fails, your client can automatically redirect requests to the other healthy node. Format:
The following table describes the parameters used in the syntax.
Example: The database account is test and the database is admin.
|
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 the "View the connection strings and URIs of a sharded cluster instance" section of this topic.
The account that is used to connect to the sharded cluster instance. The initial account is the root account.
ImportantWe recommend that you do not log on to a database as the root account in the production environment. You can create accounts and grant permissions to the accounts based on your business requirements. For more information, see Manage user permissions on MongoDB databases.
The password of the account that is used to connect to the sharded cluster instance. If no password is set for the account or you forget the password of the account, you can set or change the password of the account. For more information, see Reset the password.
The name of the database to which you want to connect. If the account is root, enter admin.
Log on to the specified database. by using one of the following methods:
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