ApsaraDB for MongoDB supports both connection strings and connection string URIs. You can use a connection string to connect to one mongos, and use a connection string URI to connect to more mongos. For high availability, we recommend that you use connection string URIs to connect your application to more mongos. This topic provides an overview of sharded cluster instance connections.

View connection strings

  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 strings.
    Connection strings

Description of connection strings

Item Description
Type
  • Intranet Connection - Classic Network: Cloud services on a classic network are not isolated. Unauthorized access can only be blocked by using security groups or whitelists.
  • Intranet Connection - VPC: A VPC is an isolated network with higher security and performance than a classic network. By default, an ApsaraDB for MongoDB instance provides VPC connection addresses.
  • Public IP Connection: Connecting to a sharded cluster instance over the Internet is risky. Therefore, ApsaraDB for MongoDB does not provide public endpoints. If you want to connect to a sharded cluster instance from a device outside Alibaba Cloud (for example, a local client), you must apply for a public endpoint. For more information, see Apply for a public endpoint for a sharded cluster instance.
mongos ID The connection string of a mongos is in the following format:
<host>:<port>
  • <host>: the endpoint used to connect to the sharded cluster instance.
  • <port>: the port used to connect to the sharded cluster instance.
Note During regular tests, you can use a connection string to directly connect to a mongos.
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, indicating a connection string URI.
  • username:password@: the username and password used to log on to a database of the sharded cluster instance. You must separate the username and password with a colon (:).
  • hostX:portX: the endpoint and port of a mongos in the sharded cluster instance.
  • /database: the database corresponding to the username and password if authentication is enabled.
  • ?options: additional connection options.
Note If your application is in a production environment, we recommend that you use a connection string URI to connect to the instance. Then your client can automatically distribute your requests to multiple mongos to balance loads. When a mongos fails, your client automatically redirects requests to other mongos in the normal state.

Log on to a database of the sharded cluster instance

  1. Obtain the connection strings and the following information:
    • The username 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 them based on your needs. For more information, see Manage MongoDB users through DMS.
    • The password of the database user. If you forget the password of the root user, you can reset it. For more information, see Set a password for a sharded cluster instance.
    • The name of the database corresponding to the username if authentication is enabled. If the username is root, enter admin.
  2. Log on to the database.

Common connection scenarios

FAQ