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
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.
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 |
|
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
|
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 |
| The domain name used to connect to the primary node. |
| 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 |
| The domain name used to connect to the secondary node. |
| 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 |
| The domain name used to connect to the read-only node. |
| 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 |
| The name of the database account used to log on to the database. Default value: root. |
| The password of the database account. |
| The domain name used to connect to the primary, secondary, or read-only node. |
| The port used to connect to the primary, secondary, or read-only node. |
| The name of the database to which you want to connect. Default value: admin. |
| Specifies that read requests are sent to all nodes of the replica set instance. |
| The database to which the specified database account belongs. |
| 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:
|
| Specifies that read requests are preferentially sent to the node corresponding to the specified tag.
|
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 |
|
Preferentially read data from secondary and read-only nodes |
| |
Preferentially read data from secondary nodes |
| |
Preferentially read data from read-only nodes |
| |
Do not send failed read requests to the primary node | Read data only from secondary and read-only nodes |
|
Read data only from secondary nodes |
| |
Read data only from read-only nodes |
|
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 |
| The name of the database account used to log on to the database. Default value: root. |
| The password of the database account. |
| The domain name used to connect to the read-only node. |
| The port used to connect to the read-only node. |
| The name of the database to which you want to connect. Default value: admin. |
| Specifies that read requests are sent to the read-only node. |
| Specifies that read requests are sent to all nodes of the replica set instance. |
| The database to which the specified database account belongs. |
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
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.
NoteWe 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.
Log on to a database For more information, see the following topics: