All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to a sharded cluster instance

Last Updated:Apr 29, 2024

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

  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 Sharded Cluster Instances page, select the resource group and region to which the desired instance belongs.

  4. Click the ID of the instance or click Manage in the Actions column.

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

  • 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 high 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, apply for a public endpoint. For more information, see (Optional) Apply for a public endpoint for an ApsaraDB for MongoDB instance.

SRV high availability endpoint

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.

  • SRV high-availability endpoints can simplify the maintenance and management of sharded cluster instances. You do not need to modify the endpoints when mongos nodes are added or removed. Clients can interact with the instances without service interruptions, which simplifies the design and maintenance of applications.

  • By default, SRV high-availability endpoints are not displayed in the ApsaraDB for MongoDB console. To use an SRV high-availability endpoint, click Apply for Private SRV Address or Apply for Public SRV Address on the Database Connections page.

  • Before you apply for a public SRV endpoint, activate the public endpoints of the mongos nodes. After you use a public SRV endpoint to connect to a sharded cluster instance, your client automatically redirect requests to mongos nodes whose public endpoints are activated.

  • SRV high-availability endpoints are available only for sharded cluster instances that use cloud disks.

Format:

mongodb+srv://<username>:<password>@<srv-host>/<database>?<options>

Parameters in the preceding command:

  • mongodb+srv://: the prefix of the connection string, indicating that DNS SRV records are used for service discovery.

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

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

  • <srv-host>: the name of the host.

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

  • <option>: other optional parameters.

    Note

    The default value of the ssl parameter in an SVR high-availability endpoint is true. To establish a successful connection to a sharded cluster instance, add the ssl parameter that is set to false to the connection string of the instance because the instance does not support SSL. For more information about the optional parameters, see MongoDB documentation.

Example:

In the following sample command, the username is test and the database is admin.

mongodb+srv://root:****@dds-2zef4c23xxxx-srv.mongodb.pre.nosql.aliyuncs.com/admin?ssl=false

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:

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

Parameters in the preceding command:

  • <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 that you want to access. Default value: admin.

  • authSource=<authenticationDatabase>: (Optional) the database to which the database account belongs. <authenticationDatabase>: the name of the authentication database. If you do not specify the <authenticationDatabase> parameter, the value of the <database> is used as the authentication database name.

    Important

    The database account must be authenticated under the database to which the account belongs.

Example:

In the following sample command, the username is test and the database is admin.

mongodb://test:****@s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717,s-bp1773180e38****.mongodb.rds.aliyuncs.com:3717/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:

<host>:<port>

Parameters in the preceding command:

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

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

Example:

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

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

      Important

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

  2. Log on to a database For more information, see the following topics:

Common connection scenarios

FAQ