ApsaraDB for MongoDB replica set instances provide separate connection strings for primary nodes, secondary nodes, and read-only nodes, along with high availability (HA) connection string URIs, connection string SRV URIs, and read-only connection string URIs for application connections. This topic describes how to view the connection strings and URIs of a replica set instance and how to log on to a specific database of the instance.
Connection strings and URIs
How to choose connection strings
In the production environment, we recommend that you use the connection string URI or connection string SRV URI to connect to the instance to achieve load balancing and high availability. If the instance contains read-only nodes, we recommend that you use the read-only connection string URI to connect your application that only reads 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. For differences between connection types, see the following table.
Connection string types
Type | Description |
Connection string SRV URI [Recommended] | In MongoDB, SRV is a type of DNS record that provides a mechanism to simplify MongoDB connection strings and supports automatic discovery of replica set cluster members. When nodes are added to or removed from a replica set instance, using the SRV URI allows seamless interaction with the instance, simplifying application design and maintenance. Important
|
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. You can use this URI when low-version drivers do not support SRV URIs or automatic node discovery. 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 The read-only endpoint is displayed only when the instance contains a read-only node. |
Connection string format description
Connection string SRV URI
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?[authSource=<authenticationDatabase>]<other_options>
Field:
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 hostname. |
| The name of the database to which you want to connect. Default value: admin. |
| The database to which the specified database account belongs. Note The TXT record of the SRV connection string for ApsaraDB for MongoDB specifies |
| Other optional parameters. Note The default value of the SSL option for SRV connections is true. If you do not need to use SSL-encrypted connections, you must add the |
Example:
The database account is test and the database is admin.
mongodb+srv://test:****@dds-bp19215393a0****-srv.mongodb.nosql.aliyuncs.com/admin?ssl=false
Connection string URI
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>][&readPreference=<readPreference_value>][&readPreferenceTags=<readonly_Tags>]
Field:
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 Information of the primary node, secondary node, and read-only node. |
| The Port of the primary node, secondary node, and 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 node and read requests are sent to nodes specified by the parameter. This parameter ensures read/write splitting and load balancing. Valid values:
Note When read requests are sent to secondary and read-only nodes, uneven load may occur. If you need to further specify nodes, set the readPreferenceTags parameter. |
| Specifies that read requests are preferentially sent to the node corresponding to the specified tag. This parameter is typically used with the readPreference parameter and is incompatible with the
|
MongoDB instances can flexibly use the readPreference
and readPreferenceTags
parameters. Different tag combinations can meet different scenario requirements:
Primary node response strategy for read requests | Nodes to which read requests are sent | Parameter combination |
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****
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>]
Field:
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 Information of the read-only node. |
| The Port of 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****
Primary endpoint
Format:
<host>:<port>
Field:
Parameter | Description |
| The Domain Name Information of the primary node. |
| The Port of the primary node. |
Example:
dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717
Secondary endpoint
Format:
<host>:<port>
Field:
Parameter | Description |
| The Domain Name Information of the secondary node. |
| The Port of the secondary node. |
Example:
dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717
Read-only endpoint
Format:
<host>:<port>
Field:
Parameter | Description |
| The Domain Name Information of the read-only node. |
| The Port of the read-only node. |
Example:
dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717
Network types of connection strings
Network type | Description |
Private network |
|
Public network | 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 need to connect over the public network, you can manually apply for a public endpoint. |
View the connection strings and URIs of a replica set instance
Visit the ApsaraDB for MongoDB Replica Set Instance List, select a resource group and region at the top, and then click the target instance ID.
In the left-side navigation pane of the target instance page, click Database Connections.
Log on to a specific database of the instance
Obtain the following information:
The account that is used to connect to 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 a database account and assign permissions.
The password of the account that is used to connect to the replica set instance. If you have not set a password or forget the password, you can set or reset the password.
The name of the database to which you want to connect. If the username is root, enter admin.
Log on to the database. You can use any of the following methods: