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 upper-left corner of the page, select the resource group and the region of the target instance.
  3. In the left-side navigation pane, click Sharded Cluster Instances.
  4. Find the target instance and click its ID.
  5. In the left-side navigation pane, click Database Connections. On the page that appears, view the connection strings and URI of the sharded cluster instance.
    Connection strings and URI

Introduction to connection strings and URIs

Item Description
Connection address type
  • Classic network endpoint: Classic network endpoints are used for communication over the classic network. In the classic network, Alibaba Cloud services are not isolated. To block unauthorized traffic, you must configure security groups or IP address whitelists.
  • VPC endpoint: Virtual private cloud (VPC) endpoints are used for communication over VPCs. A VPC is an isolated network that provides higher security and higher performance than the classic network. By default, ApsaraDB for MongoDB provides VPC endpoints for ApsaraDB for MongoDB instances to ensure high security and high performance.
  • 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, ApsaraDB for MongoDB does not provide public endpoints for ApsaraDB for MongoDB instances. If you want to connect to an ApsaraDB for MongoDB instance from a device that resides outside Alibaba Cloud, you must apply for a public endpoint. These devices include on-premises machines. For more information, see Apply for a public endpoint for a sharded cluster instance.
Connection string ApsaraDB for MongoDB provides a connection string for each of the primary and secondary nodes of the mongos nodes, shard nodes, and Configserver node in a sharded cluster instance. A connection string is in the following format:
<host>:<port>
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
Note
  • The <host> parameter specifies the endpoint that is used to connect to the sharded cluster instance.
  • The <port> parameter specifies the port number that is used to connect to the sharded cluster instance.
  • For routine tests, you need to connect only to a mongos node. If you need to connect to a shard node or the Configserver node, you must apply for a connection string for the node. For more information, see Apply for a connection string of a shard or Configserver node.
URI The URI of a sharded cluster instance is in the following format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Example:
mongodb://root:****@s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717,s-bp1773180e38****.mongodb.rds.aliyuncs.com:3717,s-bp10e3b0d02f****.mongodb.rds.aliyuncs.com:3717/admin
Note
  • The mongodb:// part is the prefix of the URI.
  • The username:password@ parameter specifies the username and password of the account that is used to connect to the sharded cluster instance. You must separate the username and password with a colon (:).
  • The hostX:portX parameter specifies the endpoint and port number that are used to connect to the sharded cluster instance.
  • The /database parameter specifies the name of the database on which the specified account has permissions.
  • ?The options parameter specifies the additional options that are used to connect to the sharded cluster instance.
  • 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 multiple mongos nodes to balance the loads within the sharded cluster instance. If a mongos node fails, your client can automatically redirect requests to the other healthy mongos nodes.

Log on to a specific database of a sharded cluster instance

  1. Obtain the following information:
    • The connection strings and URI of the sharded cluster instance. For more information, see the "View the connection strings and URI of a sharded cluster instance" section of this topic.
    • The username of the account that is used to connect to the sharded cluster instance. The initial account is the root account.
      Note We recommend that you do not use the credentials of the root account to connect to the sharded cluster instance in a 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 you forget the password of the root account, you can reset the password. For more information, see Reset the password.
    • The name of the database on which the specified account has permissions. If the root account is specified, you can log on to the database named admin.
  2. Log on to the specified database. by using one of the following methods:

Common connection scenarios

FAQ