All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to a replica set instance

Last Updated:Sep 04, 2024

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. Go to the Replica Set Instances page. In the top navigation bar, select the region in which the instance resides. Then, find the instance and click the ID of the instance.

  2. In the left-side navigation pane of the instance details page, click Database Connections.

Connection strings and URIs

Network types of endpoints

Network type

Description

Private network

  • VPC: A Virtual Private Cloud (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 cloud services is blocked only by using security groups or whitelists. New instances do not support the classic network type. For more information, see [Notice] Classic network unavailability for new ApsaraDB for MongoDB instances.

Internet

If you connect to an ApsaraDB for MongoDB instance over the Internet, the instance may be exposed to security risks. Therefore, no public endpoints are provided for ApsaraDB for MongoDB instances by default. 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 replica set instance.

Endpoint types

Endpoint type

Description

Primary endpoint

The endpoint of the primary node in an instance. The primary endpoint can be used to read data from and write data to databases in the instance.

Important

We recommend you do not connect to the primary node in the production environment by using its endpoint. Otherwise, your connection is interrupted if a primary/secondary switchover triggers a connection string change for the node. In this case, you must connect to the primary node by using a new connection string to ensure that the read and write operations are not affected.

Secondary endpoint

The endpoint of the primary node in an instance. The secondary endpoint can be used only to read data from databases in the instance.

Read-only endpoint

The endpoint of the read-only node in an instance. The read-only endpoint can be used only to read data from databases in the instance.

Note

The read-only endpoint is displayed only when the instance contains a read-only node.

Connection string URI

The HA connection string URI of an instance that ensures load balancing and HA. The connection string URI can be used to read data from and write data to databases in the instance. The URI allows you to connect to a node in the instance. We recommend that you connect to the node by using the URI in the production environment.

Important

If you connect to the node by using the URI, all requests are processed by the primary node. Read and write operations are not affected by a primary/secondary switchover.

Read-only connection string URI

The read-only connection string URI of an instance. The URI allows you to connect to a read-only node in the instance. If your application needs to frequently read data from an instance that contains multiple read-only nodes, we recommend that you connect to the instance by using the URI.

Note
  • You can view the read-only connection string URI only if your instance contains one or more read-only nodes.

  • If you connect to an instance by using the URI, read requests are sent to read-only nodes.

  • If one read-only node in an instance fails, read requests are switched to the next read-only node to ensure connectivity. If all read-only nodes in an instance fail, a connection exception occurs when you continue using the URI to connect to the instance.

The following section describes the format, parameters, and examples of each endpoint:

Primary endpoint

Format:

<host>:<port>

The following table describes the parameters that are included in the preceding format.

Parameter

Description

<host>

The domain name used to connect to the primary node.

<port>

The port used to connect to the primary node.

Example:

dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

Secondary endpoint

Format:

<host>:<port>

The following table describes the parameters that are included in the preceding format.

Parameter

Description

<host>

The domain name used to connect to the secondary node.

<port>

The port used to connect to the secondary node.

Example:

dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

Read-only endpoint

Format:

<host>:<port>

The following table describes the parameters that are included in the preceding format.

Parameter

Description

<host>

The domain name used to connect to the read-only node.

<port>

The port used to connect to the read-only node.

Example:

dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

Connection string URI

Format:

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

The following table describes the parameters that are included in the preceding format.

Parameter

Description

<username>

The name of the database account used to log on to the database. Default value: root.

<password>

The password of the database account.

<host>

The domain name used to connect to the primary, secondary, or read-only node.

<port>

The port used to connect to the primary, secondary, or read-only node.

<database>

The name of the database to which you want to connect. Default value: admin.

replicaSet=<replicaSet_value>

Specifies that read requests are sent to all nodes of the replica set instance. <replicaSet_value> indicates the unique ID of the HA endpoint of the instance.

authSource=<authenticationDatabase> (optional)

The database to which the specified database account belongs. <authenticationDatabase> indicates 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.

readPreference=[primary|primaryPreferred|secondary | secondaryPreferred] (optional)

Specifies that write requests are sent to the primary nodes and read requests are randomly sent to secondary and read-only nodes. This parameter ensures read and write splitting and load balancing. Valid values:

  • readPreference=primary: Read requests are sent only to the primary nodes.

  • readPreference=primaryPreferred: Read requests are preferentially sent to the primary node.

  • readPreference=secondary: Read requests are sent only to secondary and read-only nodes. If the secondary and read-only nodes fail, read requests fail and are not sent to the primary node.

  • readPreference=secondaryPreferred: Read requests are preferentially sent to secondary and read-only nodes. If secondary and read-only nodes fail, read requests are sent to the primary node.

readPreferenceTags=<readonly_Tags> (optional)

Specifies that read requests are preferentially sent to the node corresponding to the specified tag. <readonly_Tags> indicates the tag keys and tag values of the read-only node and contains the role:electable and role:readonly options. Valid values:

  • readPreferenceTags=role:electable: Data is preferentially read from the secondary node.

    Note

    This option is available only for instances that use cloud disks.

  • readPreferenceTags=role:readonly: Data is preferentially read from the read-only node.

The readPreference and readPreferenceTags parameters are available for instances that use cloud disks. The combinations of different tags can meet the requirements of various scenarios. The following table describes parameter combinations in different scenarios.

Method used to process failed read requests

Node to receive read requests

Parameter combination solution

Send failed read requests to the primary node

Preferentially read data from the primary node

readPreference=primaryPreferred

Preferentially read data from secondary and read-only nodes

readPreference=secondaryPreferred

Preferentially read data from secondary nodes

readPreference=secondaryPreferred&readPreferenceTags=role:electable

Preferentially read data from read-only nodes

readPreference=secondaryPreferred&readPreferenceTags=role:readonly

Do not send failed read requests to the primary node

Read data only from secondary and read-only nodes

readPreference=secondary

Read data only from secondary nodes

readPreference=secondary&readPreferenceTags=role:electable

Read data only from read-only nodes

readPreference=secondary&readPreferenceTags=role:readonly

Example:

mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****

Read-only connection string URI

Format:

mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]

The following table describes the parameters that are included in the preceding format.

Parameter

Description

<username>

The name of the database account used to log on to the database. Default value: root.

<password>

The password of the database account.

<host>

The domain name used to connect to the read-only node.

<port>

The port 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&readPreferenceTags=role:readonly

Specifies that read requests are sent to the read-only node. readPreferenceTags indicates the tag keys and tag values corresponding to the read-only node. The value is fixed to role:readonly.

replicaSet=<replicaSet_value>

Specifies that read requests are sent to all nodes of the replica set instance. <replicaSet_value> indicates the unique ID of the HA endpoint of the instance.

authSource=<authenticationDatabase> (optional)

The database to which the specified database account belongs. <authenticationDatabase> indicates 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.

Example:

mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=mgset-6108****

Log on to a specific database of a replica set instance

  1. Obtain the following information:

    • The connection strings or URI of the replica set instance. For more information, see View the connection strings and URIs of a replica set instance.

    • 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 the accounts based on your business requirements. For more information, see Manage the permissions on MongoDB database users.

    • The password of the account that is used to connect to a database of the replica set instance. If you did not configure a password for the account or have forgotten the password, configure or reset 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