ApsaraDB for MongoDB provides a connection string for each primary, secondary, and read-only node in a replica set instance. ApsaraDB for MongoDB also provides the following high availability (HA) Uniform Resource Identifiers (URIs) for application connection: the connection string URI and the read-only connection string URI. This topic describes how to view the connection strings and URIs of a replica set instance. This topic also describes how to log on to a specific database of a replica set instance.

View the connection strings and URIs of a replica set instance

  1. Log on to the ApsaraDB for MongoDB console.
  2. In the left-side navigation pane, click Replica Set 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.

Connection strings and URIs

Item Description
Connection address type
  • Private endpoint:
    • VPC endpoint: 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 endpoint: Cloud services in the classic network are not isolated. Unauthorized access to cloud services is blocked only 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 an ApsaraDB for MongoDB instance over the Internet, you must apply for a public endpoint. For more information, see (Optional) Apply for a public endpoint for an ApsaraDB for MongoDB instance.
Role
  • Primary node: The primary node of a replica set instance can perform both read and write operations on instance databases.
    Important We recommend you do not connect to the primary node in the production environment by using its connection string. Otherwise, your connection will be interrupted if a primary/secondary switchover triggers a connection string change for the node. In this case, you must connect to the new connection string to ensure that the read and write operations are not affected.
  • Secondary node: The secondary node of a replica set instance can perform only read operations on instance databases.
  • Read-only node: The read-only node of a replica set instance can perform only read operations on instance databases.
    Note The connection string of a read-only node is displayed only if the replica set instance contains the read-only node.
  • Read-only connection string URI: an HA connection string URI that ensures load balancing and HA. If you use this URI to connect to a replica set instance, you can perform read and write operations on instance databases. This URI allows you to connect to a read-only node in the instance. If your instance contains multiple read-only nodes, we recommend that you connect to this URI in scenarios where a large number of read operations must be handled.
    Note
    • This URI can be viewed only if the instance contains at least one read-only node.
    • If you use the read-only connection string URI to connect to a replica set instance, read requests are preferentially sent to read-only nodes.
    • If one read-only node fails, read requests are switched to the next read-only node to ensure connectivity. If all read-only nodes fail, read requests are switched to a secondary node.
  • Connection string URI: an HA connection string URI that ensures load balancing and HA. If you use this URI to connect to a replica set instance, you can perform read and write operations on instance databases. This URI allows you to connect to a node in the instance. We recommend that you connect to this URI in the production environment.
    Important If you connect to this URI, all requests are processed by the primary node. Read and write operations are not affected by primary/secondary switchover.
Connection string of a primary, secondary, or read-only node Format:
<host>:<port>
Parameters:
  • <host>: the domain name information used to connect to the primary, secondary, or read-only node.
  • <port>: the port number used to connect to the primary, secondary, or read-only node.
Example:
dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717
Read-only connection string URI Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?readPreference=[secondary | secondaryPreferred][&readPreferenceTags=<readonly_Tags>]&replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]
Parameters:
  • <username>: the name of the database account used to log on to the database. Default value: root.
  • <password>: the password of the database account used to log on to the database.
  • <host>: the domain name information used to connect to the read-only node.
  • <port>: the port number used to connect to the read-only node.
  • <database>: the name of the database to which you want to connect. Default value: admin.
  • readPreference=[secondary | secondaryPreferred]: sends write requests to the primary node and read requests to a random secondary or read-only node to ensure read/write splitting and load balancing. Valid values:
    • readPreference=secondary: does not send read requests to the primary node if all secondary and read-only nodes fail.
    • readPreference=secondaryPreferred: sends read requests to the primary node if all secondary and read-only nodes fail.
    Note For more information about readPreference, see Read Preference.
  • readPreferenceTags=<readonly_Tags>: preferentially sends read requests to read-only nodes that are added with specified tags. This parameter is optional. <readonly_Tags>: the list of tags that can be added to read-only nodes. The value is fixed at role:readonly and cannot be modified.
    Note For more information about readPreferenceTags, see Connection String URI Format.
  • replicaSet=<replicaSet_value>: sends read requests to all nodes in the replica set instance. <replicaSet_value>: the unique URI ID used to connect to the replica set instance.
  • 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:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=mgset-6108****
Connection string URI Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]
Parameters:
  • <username>: the name of the database account used to log on to the database. Default value: root.
  • <password>: the password of the database account used to log on to the database.
  • <host>: the domain name information used to connect to the primary, secondary, or read-only node.
  • <port>: the port number used to connect to the primary, secondary, or read-only node.
  • <database>: the name of the database to which you want to connect.
  • replicaSet=<replicaSet_value>: sends read requests to all nodes in the replica set instance. <replicaSet_value>: the unique URI ID used to connect to the replica set instance.
  • 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:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****

Log on to a specific database of a replica set instance

  1. Obtain the following information:
    • The connection strings or URIs of the replica set instance. For more information, see the "View the connection strings and URIs of a replica set instance" section of this topic.
    • The account that is used to connect to a database of the replica set instance. The initial account is the root account.
      Note We recommend that you do not connect to a database in the production environment by using the root account. You can create accounts and grant permissions to them based on your requirements. For more information, see Manage user permissions on MongoDB databases.
    • The password of the account that is used to connect to a database of the replica set instance. If you did not set a password for the account or have forgotten the account password, 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 account is root, enter admin.
  2. Log on to the specified database. For more information, see the following topics:

Common connection scenarios

FAQ