Replica set instance connection
An ApsaraDB for MongoDB replica set instance provides separate endpoints for its primary, secondary, and read-only nodes, as well as high-availability ConnectionStringURI, ConnectionStringURI SRV, and ReadOnly ConnectionStringURI for application connections. This topic explains how to get these endpoints and connect to your instance.
Connection Address Description
Endpoint selection
In a production environment, connect to your instance using the ConnectionStringURI or ConnectionStringURI SRV address for load balancing and high availability. If your instance has read-only nodes, connect read-only applications using the ReadOnly ConnectionStringURI address. Avoid using a single-node connection address, as a failover can change node roles. See the following table for the differences between connection types.
Endpoint type
|
Address type |
Description |
|
ConnectionStringURI SRV address [Recommended] |
In MongoDB, SRV is a DNS record type that simplifies the MongoDB connection string and supports automatic discovery of members in a replica set. When nodes are added to or removed from a replica set instance, using an SRV address allows an application to interact with the instance seamlessly. This simplifies application design and maintenance. Important
|
|
ConnectionStringURI address |
The ConnectionStringURI address provides load balancing and high availability. It contains the addresses of all nodes in the instance, allowing you to perform read and write operations on the database. Use this address if your driver version does not support SRV addresses or automatic node discovery. Important
|
|
ReadOnly ConnectionStringURI address |
The ReadOnly ConnectionStringURI address contains only the addresses of all read-only nodes in the instance. When you connect to this address, the client sends read requests to read-only nodes and write requests to the primary node. Note
|
|
Primary address |
The connection address of the primary node. You can connect to this address to perform read and write operations on the database. Important
We do not recommend that applications in a production environment connect to the instance by using this address. After a failover, the primary node address changes. You must connect to the new primary node to avoid disrupting your read and write operations. |
|
Secondary address |
The connection address of a secondary node. You can connect to this address to perform only read operations on the database. Important
We do not recommend that applications in a production environment connect to the instance by using this address. After a failover, a secondary node may be promoted to the primary. If your application continues to use this address, it might connect to a primary node, which can lead to permission conflicts or unintended write operations. |
|
ReadOnly address |
The connection address 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 if 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>
Parameters:
|
Parameter |
Description |
|
|
The connection string prefix, indicating the use of DNS SRV records for service discovery. |
|
|
The name of the database account. |
|
|
The password for the database account. |
|
|
The hostname. |
|
|
The database to access. Default: admin. |
|
|
This parameter specifies the database to which the database account belongs. Note
For ApsaraDB for MongoDB SRV connection strings, the TXT record specifies |
|
|
This parameter enables read/write splitting and load balancing. It routes write requests to the primary node and read requests to the specified node type. Valid values:
Note
When read requests are routed to secondary and read-only nodes, the load may be uneven. To further specify the nodes, set the readPreferenceTags parameter. |
|
|
This parameter routes read requests first 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 SRV connections is true by default. To disable SSL encryption, add the |
You can combine the readPreference and readPreferenceTags parameters for different scenarios:
|
Failover behavior |
Preferred nodes |
Parameter combination |
|
If reads from preferred nodes fail, route read requests to the primary node |
Prefer the primary node |
|
|
Prefer secondary and read-only nodes |
|
|
|
Prefer secondary nodes Note
If secondary nodes are unavailable, read requests fail over to the primary node. |
|
|
|
Prefer read-only nodes Note
If read-only nodes are unavailable, read requests fail over to the primary node. |
|
|
|
If reads from preferred nodes fail, do not route read requests to the primary node |
Read only from secondary and read-only nodes |
|
|
Read only from secondary nodes Note
If a secondary node is unavailable, the read operation fails. |
|
|
|
Read only from read-only nodes Note
If a read-only node is unavailable, the read operation fails. |
|
Example:
This example uses the 'test' database account to connect to the 'admin' database.
mongodb+srv://test:****@dds-bp19215393a0****-srv.mongodb.nosql.aliyuncs.com/admin?ssl=false
ConnectionStringURI address
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. Default: root. |
|
|
The password for the database account. |
|
|
The domain names of the primary, secondary, and read-only nodes. |
|
|
The ports of the primary, secondary, and read-only nodes. |
|
|
The database to access. Default: admin. |
|
|
Routes read requests to all nodes in the replica set instance. |
|
|
Specifies the authentication database where the database account is defined. |
|
|
This parameter enables read/write splitting and load balancing. It routes write requests to the primary node and read requests to the specified node type. Valid values:
Note
When read requests are routed to secondary and read-only nodes, the load may be uneven. To further specify the nodes, set the readPreferenceTags parameter. |
|
|
This parameter routes read requests first to nodes that match the specified tags. It is typically used with the readPreference parameter and is not compatible with the
|
You can combine the readPreference and readPreferenceTags parameters for different scenarios:
|
Failover behavior |
Preferred nodes |
Parameter combination |
|
If reads from preferred nodes fail, route read requests to the primary node |
Prefer the primary node |
|
|
Prefer secondary and read-only nodes |
|
|
|
Prefer secondary nodes Note
If secondary nodes are unavailable, read requests fail over to the primary node. |
|
|
|
Prefer read-only nodes Note
If read-only nodes are unavailable, read requests fail over to the primary node. |
|
|
|
If reads from preferred nodes fail, do not route read requests to the primary node |
Read only from secondary and read-only nodes |
|
|
Read only from secondary nodes Note
If a secondary node is unavailable, the read operation fails. |
|
|
|
Read only from read-only nodes Note
If a read-only node is unavailable, 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****
Read-only ConnectionStringURI address
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. Default: root. |
|
|
The password for the database account. |
|
|
The domain name of the read-only node. |
|
|
The port of the read-only node. |
|
|
The database to access. Default: admin. |
|
|
This parameter routes read requests to read-only nodes. |
|
|
Routes read requests to all nodes in the replica set instance. |
|
|
Specifies the authentication database where the database account is defined. |
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]
Parameters:
|
Parameter |
Description |
|
|
The name of the database account. |
|
|
The password for the database account. |
|
|
The domain name of the primary node. |
|
|
The port of the primary node. |
|
|
The database to access. Default: admin. |
|
|
Sends requests only to the current endpoint (the primary node).
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=true
Secondary address
Format:
mongodb://<username>:<password>@<host>:<port>/<database>?[directConnection=true]
Parameters:
|
Parameter |
Description |
|
|
The name of the database account. |
|
|
The password for the database account. |
|
|
The domain name of the secondary node. |
|
|
The port of the secondary node. |
|
|
The database to access. Default: admin. |
|
|
Sends requests only to the current endpoint (the secondary node).
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=true
Read-only address
Format:
mongodb://<username>:<password>@<host>:<port>/<database>?[directConnection=true]
Parameters:
|
Parameter |
Description |
|
|
The name of the database account. |
|
|
The password for the database account. |
|
|
The domain name of the read-only node. |
|
|
The port of the read-only node. |
|
|
The database to access. Default: admin. |
|
|
Sends requests only to the current endpoint (the read-only node).
|
Example:
mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?directConnection=true
Endpoint network types
|
Network type |
Description |
|
private network |
|
|
public network |
Connecting to an instance over the public network poses security risks. By default, ApsaraDB for MongoDB instances do not provide public endpoints. If you need to connect over the public network, you can manually apply for a public endpoint. |
Database endpoint
-
Go to the MongoDB Replica Set Instances page, select a resource group and region, and then click the target instance.
-
In the left navigation pane, click Database Connections.
Connect to a MongoDB database
-
Get the following information:
-
The database endpoint.
-
The database account. The initial account is
root.NoteDo not use the
rootaccount to connect to the database in a production environment. You can create a database account and assign 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. When using the
rootaccount, the corresponding database isadmin.
-
-
Connect to the database by using one of the following methods:
Common connection scenarios
-
Connect to MongoDB from an ECS instance with a different network type over an internal network
-
Connect to MongoDB from an ECS instance in a different region over an internal network
-
Connect to MongoDB from an ECS instance in a different account over an internal network
-
Connect to a replica set instance to set up read/write splitting