ApsaraDB for MongoDB allows you connect to mongos, shard, and Configserver nodes of a sharded cluster instance by using separate connection strings or connection string URIs. For high availability, we recommend that you use connection string URIs to connect your application to a sharded cluster instance. This topic provides an overview of sharded cluster instance connections.

View connection information

  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 Connection to view connection information.
    Connection strings

Description of connection information

Item Description
Connection type
  • Intranet Connection - Classic Network: Cloud services on the classic network are not isolated. Unauthorized access can be blocked only by security groups or whitelists.
  • Intranet Connection - VPC: A virtual private cloud (VPC) is an isolated virtual network that provides higher security and performance than the classic network. By default, ApsaraDB for MongoDB provides connection strings that apply to VPCs.
  • Public IP Connection: By default, ApsaraDB for MongoDB does not provide public endpoints for instances. This is because security risks may arise if you connect to instances over the Internet. If you want to connect to an ApsaraDB for MongoDB instance from a device outside of Alibaba Cloud such as an on-premises machine, you must apply for a public endpoint. For more information, see Apply for a public endpoint for a sharded cluster instance.
Connection string ApsaraDB for MongoDB provides separate connection strings of primary and secondary nodes for each mongos, shard, and Configserver node. A connection string is in the following format:
<host>:<port>
  • <host>: the endpoint that is used to connect to a node of a sharded cluster instance.
  • <port>: the port number that is used to connect to a node of a sharded cluster instance.
Note In tests, we recommend that you connect to a mongos node by using the corresponding connection string. To connect to a shard node or a Configserver node, you must apply for the connection string of the node. For more information, see Apply for a connection string of a shard or Configserver node.
Connection string URI A connection string URI is in the following format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][? options]]
  • mongodb://: the prefix that indicates a connection string URI.
  • username:password@: the username and password used to log on to a node of the sharded cluster instance. Separate the username and password with a colon (:).
  • hostX:portX: the endpoint and port number that are used to connect to a node of a sharded cluster instance.
  • /database: the name of the authentication database to which the database account belongs.
  • ? options: additional connection options.
Note If your application is in a production environment, we recommend that you use a connection string URI to connect the application to the instance. Then, your client can automatically distribute your requests to multiple mongos nodes to balance loads. When a mongos node fails, your client automatically redirects requests to another mongos node in the normal status.

Log on to a database of an ApsaraDB for MongoDB instance

  1. Obtain the connection information and the following information:
    • The username that is used to log on to the database. The initial username is root.
      Note We recommend that you do not log on to a database as the root user in a production environment. You can create accounts and grant permissions to the accounts. For more information, see Manage user permissions on MongoDB databases.
    • The password that is used to log on to the database. If you forget the password of the root user, you can reset the password. For more information, see Reset the password.
    • The name of the authentication database to which the database account belongs. If the database username is root, the database name is admin.
  2. Log on to the database.

Common connection scenarios

References