ApsaraDB for MongoDB provides a connection string for the primary node, each secondary node, or each 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: a connection string URI and a read-only connection string URI. This topic describes how to view the connection strings and HA URIs of a replica set instance and how to log on to a specific database of the instance.
Connection strings and URIs
Select endpoints
We recommend that you use a connection string URI to connect to an instance to achieve load balancing and high availability in the production environment. If an instance contains read-only nodes, we recommend that you use a read-only connection string URI to connect your application that only read data to the instance. A primary/secondary switchover may cause node roles to change. We recommend that you do not use the connection string of a node to connect to the instance.
Endpoint types
Endpoint type | Note |
Connection string URI | The HA connection string URI of the 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 connection string URI allows you to connect to a node in the instance. Important
|
SRV HA endpoint | SRV HA endpoints can simplify the maintenance and management of instances. You do not need to modify the endpoints when nodes are added or removed. Your client can interact with the instances without service interruptions, which simplifies the design and maintenance of your application. If your application is deployed in a production environment, we recommend that you use a connection string URI to connect to an instance. This way, your client can automatically distribute requests to multiple nodes to balance the loads within the instance. If a node fails, your client can automatically redirect requests to other healthy nodes. Important
|
Read-only connection string URI | The read-only connection string URI of the instance. The read-only connection string URI allows you to connect to a read-only node in the instance. If you use the read-only connection string URI to connect to the node in the instance, read requests are sent to the node. Note
|
Primary endpoint | The endpoint of the primary node in the instance. The primary endpoint can be used to read data from and write data to databases in the instance. Important We recommend that you do not use the primary endpoint to connect your application in the production environment to the instance. When a primary/secondary switchover is triggered for the instance, the primary endpoint changes. In this case, you must connect to the new primary node to ensure that read/write operations are not affected. |
Secondary endpoint | The endpoint of a secondary node in the instance. A secondary endpoint can be used only to read data from databases in the instance. Important We recommend that you do not use a secondary endpoint to connect your application in the production environment to the instance. When a primary/secondary switchover is triggered for the instance, a secondary node may become the primary node. The primary endpoint supports write operations, which causes the security issues of data and permissions. |
Read-only endpoint | The endpoint of a read-only node in the instance. A read-only endpoint can be used only to read data from databases in the instance. Note A read-only endpoint is displayed only when the instance contains a read-only node. |
Endpoint formats
Connection string URIs
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>][&readPreference=<readPreference_value>][&readPreferenceTags=<readonly_Tags>]
Parameters:
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 instance. |
| The database to which the specified database account belongs. |
| Specifies that write requests are sent to the primary node and read requests are sent to nodes specified by the parameter. This parameter ensures read/write splitting and load balancing. Valid values:
Note If read requests are sent to secondary and read-only nodes, uneven loads may occur. To further specify nodes that receive the read requests, configure the readPreferenceTags parameter. |
| Specifies that read requests are preferentially sent to the node corresponding to the specified tag. In most cases, the parameter is specified together with the readPreference parameter and is incompatible with the
|
The readPreference
and readPreferenceTags
parameters are available for instances. The combinations of different tags can meet the requirements of various scenarios. The following table describes parameter combinations in different scenarios.
Response policy of the primary node to receive 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 Note If secondary nodes fail, read requests are sent to the primary node. |
| |
Preferentially read data from read-only nodes Note If read-only nodes fail, read requests are sent to the primary node. |
| |
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 Note If secondary nodes fail, read operations fail. |
| |
Read data only from read-only nodes Note If read-only nodes fail, read operations fail. |
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****
SRV HA endpoints
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?<options>
Parameters
Parameter | Description |
| The prefix of the connection string, which indicates that DNS SRV records are used to identify services. |
| The name of the database account. |
| The password of the database account. |
| The hostnames. |
| The name of the database to which you want to connect. Default value: admin. |
| Other optional parameters. Note The default value of the ssl parameter in an SRV HA endpoint is true. If you do not need to use SSL to encrypt connections, add the |
Example:
In the following sample command, the username is test and the database is admin.
mongodb+srv://test:****@dds-bp19215393a0****-srv.mongodb.nosql.aliyuncs.com/admin?ssl=false
Read-only connection string URIs
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]
Parameters:
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 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****
Primary endpoints
Format:
<host>:<port>
Parameters:
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 endpoints
Format:
<host>:<port>
Parameters:
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 endpoints
Format:
<host>:<port>
Parameters:
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
Network types of endpoints
Network type | Note |
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 instance. |
View the connection strings and URIs of the 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.
Log on to a specific database of the instance
Obtain the following information:
The connection strings and HA URIs of the 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 specific database of the instance. The initial account is the root account.
NoteWe recommend that you do not connect to a specific 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 of MongoDB database users.
The password of the account that is used to connect to a specific database of the 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 specific database of the instance. For more information, see the following topics: