ApsaraDB for MongoDB replica set instances provide separate endpoints for primary, secondary, and read-only nodes. They also provide high-availability connection string URIs, such as ConnectionStringURI, ConnectionStringURI SRV, and ReadOnly ConnectionStringURI, for application connections. This topic describes how to retrieve these endpoints and provides connection instructions.
Endpoint details
Choose an endpoint
In a production environment, connect to your instance using a ConnectionStringURI or ConnectionStringURI SRV address to ensure load balancing and high availability. If your instance has read-only nodes, applications that perform only read operations should connect to the instance using a ReadOnly ConnectionStringURI address. We do not recommend connecting to an instance with a single-node endpoint because a primary/secondary switchover can change the role of a node. The following table describes the differences between endpoint types.
Endpoint types
Address type | Description |
ConnectionStringURI SRV address (Recommended) | In MongoDB, SRV is a DNS record type that simplifies the connection string and supports automatic discovery of members in a replica set cluster. When you add or remove nodes from a replica set instance, you can use an SRV address to interact with the instance without any perception of the change. This simplifies application design and maintenance. Important
|
ConnectionStringURI address | A ConnectionStringURI address provides load balancing and high availability. This address contains the addresses of all nodes in the instance. You can connect to this address to perform read and write operations on the database. Important
|
ReadOnly ConnectionStringURI address | A ReadOnly ConnectionStringURI address contains only the addresses of all read-only nodes in the instance. When you connect to this address, read requests are sent to the read-only nodes, and write requests are sent to the primary node. Note
|
Primary address | The endpoint of the primary node. You can connect to this address to perform read and write operations on the database. Important Do not connect applications in a production environment to the instance using this address. When a primary/secondary switchover is triggered for the instance, the primary node address changes. You must connect to the new primary node to ensure that read and write operations are not affected. |
Secondary address | The endpoint of a secondary node. You can connect to this address to perform only read operations on the database. Important Do not connect applications in a production environment to the instance using this address. When a primary/secondary switchover is triggered for the instance, a secondary node may be promoted to the primary node. The primary node address supports write operations, which can cause data and permission security issues. |
ReadOnly address | The endpoint of a read-only node. You can connect to this address to perform only read operations on the database. Note This address is displayed only when the instance has read-only nodes. |
Connection address format
ConnectionStringURI SRV address
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?[authSource=<authenticationDatabase>][&readPreference=<readPreference_value>][&readPreferenceTags=<readonly_Tags>]<other_options>Parameter description:
Parameter | Description |
| The prefix of the connection string. It indicates that DNS SRV records are used for service discovery. |
| The name of the database account. |
| The password of the database account. |
| The hostname. |
| The name of the database to access. The default value is admin. |
| This parameter specifies the database where the database account is located. Note The TXT record of the SRV connection string for ApsaraDB for MongoDB specifies |
| This parameter implements read/write splitting and load balancing. It sends write requests to the primary node and read requests to the node specified by this parameter. Valid values:
Note When read requests are sent to secondary and read-only nodes, the load may be uneven. To further specify nodes, set the readPreferenceTags parameter. |
| This parameter prioritizes sending read requests to nodes that match the specified tags. It is typically used with the readPreference parameter and is not compatible with the
|
| Other optional parameters. Note The SSL option for an SRV connection is true by default. If you do not need to use an SSL-encrypted connection, add the |
You can use the readPreference and readPreferenceTags parameters in a MongoDB instance. By combining different tags, you can meet the requirements of various scenarios:
Read request routing policy | Target node for read requests | Parameter combination |
Read requests can fall back to the primary node | Read from the primary node first |
|
Read from secondary and read-only nodes first |
| |
Read from secondary nodes first Note If a secondary node fails, the read request is sent to the primary node. |
| |
Read from read-only nodes first Note If a read-only node fails, the read request is sent to the primary node. |
| |
Read requests do not fall back to the primary node | Read from secondary and read-only nodes only |
|
Read from secondary nodes only Note If all available secondary nodes fail, the read operation fails. |
| |
Read from read-only nodes only Note If all available read-only nodes fail, the read operation fails. |
|
Example:
The database account is test, and it belongs to the admin database.
mongodb+srv://test:****@dds-bp19215393a0****-srv.mongodb.nosql.aliyuncs.com/admin?ssl=falseConnectionStringURI address
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>][&readPreference=<readPreference_value>][&readPreferenceTags=<readonly_Tags>]Parameter description:
Parameter | Description |
| The name of the database account. The default value is root. |
| The password of the database account. |
| The domain name of the primary, secondary, and read-only nodes. |
| The port of the primary, secondary, and read-only nodes. |
| The name of the database to access. The default value is admin. |
| This parameter sends read requests to all nodes of the replica set instance. |
| This parameter specifies the database where the database account is located. |
| This parameter implements read/write splitting and load balancing. It sends write requests to the primary node and read requests to the node specified by this parameter. Valid values:
Note When read requests are sent to secondary and read-only nodes, the load may be uneven. To further specify nodes, set the readPreferenceTags parameter. |
| This parameter prioritizes sending read requests to nodes that match the specified tags. It is typically used with the readPreference parameter and is not compatible with the
|
You can use the readPreference and readPreferenceTags parameters in a MongoDB instance. By combining different tags, you can meet the requirements of various scenarios:
Read request routing policy | Target node for read requests | Parameter combination |
Read requests can fall back to the primary node | Read from the primary node first |
|
Read from secondary and read-only nodes first |
| |
Read from secondary nodes first Note If a secondary node fails, the read request is sent to the primary node. |
| |
Read from read-only nodes first Note If a read-only node fails, the read request is sent to the primary node. |
| |
Read requests do not fall back to the primary node | Read from secondary and read-only nodes only |
|
Read from secondary nodes only Note If all available secondary nodes fail, the read operation fails. |
| |
Read from read-only nodes only Note If all available read-only nodes fail, the read operation fails. |
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****ReadOnly ConnectionStringURI address
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]Description:
Parameter | Description |
| The name of the database account. The default value is root. |
| The password of the database account. |
| The Domain Name of the read-only node. |
| The Port of the read-only node. |
| The name of the database to access. The default value is admin. |
| This parameter sends read requests to read-only nodes. |
| This parameter sends read requests to all nodes of the replica set instance. |
| This parameter specifies the database where the database account is located. |
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&readPreferenceTags=role:readonly&replicaSet=mgset-6108****Primary address
Format:
mongodb://<username>:<password>@<host>:<port>/<database>?[directConnection=true]Description:
Parameter | Description |
| The name of the database account. |
| The password of the database account. |
| The Domain Name of the primary node. |
| The Port of the primary node. |
| The name of the database to access. The default value is admin. |
| Specifies that requests are sent only to the current endpoint (primary node).
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=trueSecondary address
Format:
mongodb://<username>:<password>@<host>:<port>/<database>?[directConnection=true]Description:
Parameter | Description |
| The name of the database account. |
| The password of the database account. |
| The Domain Name of the secondary node. |
| The Port of the secondary node. |
| The name of the database to access. The default value is admin. |
| Specifies that requests are sent only to the current endpoint (secondary node).
|
For example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=trueReadOnly address
Format:
mongodb://<username>:<password>@<host>:<port>/<database>?[directConnection=true]Description:
Parameter | Description |
| The name of the database account. |
| The password of the database account. |
| The Domain Name of the read-only node. |
| The Port of the read-only node. |
| The name of the database to access. The default value is admin. |
| Specifies that requests are sent only to the current endpoint (read-only node).
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=trueNetwork types for connection addresses
Network type | Description |
Private network |
|
Public network | Connecting to an instance over the Internet poses security risks. ApsaraDB for MongoDB instances do not provide public endpoints by default. To connect over the public network, you can manually apply for a public endpoint. |
Obtain a database endpoint
Go to the Replica Set Instances page. At the top of the page, select a resource group and a region, and then click the ID of the target instance.
In the navigation pane on the left of the instance details page, click Database Connections.
Log on to a MongoDB database
Retrieve the following information:
The database account. The initial account is root.
NoteDo not use the root account to connect to the database in a production environment. You can create a database account and grant permissions.
The password for the database account. If you have not set a password or have forgotten it, you can set or reset the password.
The database to access. If the database account is root, the corresponding database is admin.
Log on to the database. You can use one of the following methods: