All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to a sharded cluster instance

Last Updated:Dec 28, 2023

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

  1. Log on to the ApsaraDB for MongoDB console.

  2. In the left-side navigation pane, click Sharded Cluster Instances.

  3. In the upper-left corner of the page, select the resource group and region to which the instance belongs.

  4. Click the ID of an instance, or click More icon in the Actions column corresponding to the instance and select Manage.

  5. 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

  • Private endpoint:

    • VPC: Virtual Private Cloud (VPC) endpoints are used for communication over VPCs. A VPC is an isolated network that provides higher security and performance than the classic network. By default, ApsaraDB for MongoDB provides VPC endpoints for instances to ensure high security and performance.

    • Classic network: Cloud services in the classic network are not isolated. Unauthorized access to a cloud service is blocked by using security groups or whitelists.

  • Public endpoint: Public endpoints are used for communication over the Internet. If you connect to an ApsaraDB for MongoDB instance over the Internet, the instance may be exposed to security risks. By default, no public endpoints are provided for ApsaraDB for MongoDB instances. If you want to connect to the instance over the Internet, you must apply for a public endpoint. For more information, see Apply for a public endpoint for an ApsaraDB for MongoDB instance.

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:

<host>:<port>

The following table describes the parameters used in the syntax.

  • <host>: the domain name information of each node.

  • <port>: the port number of each node.

Example:

s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
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:

mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]

The following table describes the parameters used in the syntax.

  • <username>: the name of the database account. Default value: root.

  • <password>: the password of the database account.

  • <host>: the domain name information of each node.

  • <port>: the port number of each node.

  • <database>: the name of the database to which you want to connect. Default value: admin.

  • authSource=<authenticationDatabase>: specifies the database to which the specified database account belongs. This parameter is optional. <authenticationDatabase>: the name of the database used for authentication. If you do not specify the authentication database, the database specified by <database> is used as the authentication database.

    Important

    For the authentication to succeed, the specified database account must belong to the specified authentication database.

Example:

The database account is test and the database is admin.

mongodb://test:****@s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717,s-bp1773180e38****.mongodb.rds.aliyuncs.com:3717/admin

Log on to a specific database of a sharded cluster instance

  1. 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.

      Important

      We 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.

  2. Log on to the specified database. by using one of the following methods:

Common connection scenarios

FAQ