ApsaraDB for MongoDB sharded cluster instances provide separate endpoints for the Mongos, Shard, and ConfigServer components, along with high-availability ConnectionStringURI and ConnectionStringURI SRV addresses.
Obtain an endpoint
-
Go to the ApsaraDB for MongoDB Sharded Cluster Instances page. In the top navigation bar, select a resource group and a region. Then, click the target instance's ID.
-
In the left-side navigation pane of the instance details page, click Database Connections to obtain the endpoint of the target node.
Endpoint details
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. |
Endpoint types
|
Endpoint type |
Description |
|
ConnectionStringURI SRV address (Recommended) |
An SRV record is a DNS record type that simplifies the connection string and enables automatic cluster member discovery.
Important
|
|
ConnectionStringURI address |
ApsaraDB for MongoDB provides ConnectionStringURI addresses for Mongos and Shard components. For production environments, use a ConnectionStringURI address instead of a single-node connection string. The client automatically distributes requests across multiple Mongos nodes for load balancing, and performs failover to a healthy Mongos node if one becomes unavailable. Note
|
|
Mongos address |
The endpoint of a Mongos component, including the endpoint of the primary node. Note
For daily testing, connecting to a single Mongos node is sufficient. |
|
Shard address |
The endpoint of a Shard component, including the endpoints of the primary node, secondary node, and read-only node. Note
|
|
ConfigServer address |
The endpoint of a ConfigServer component, including the endpoints of the primary node and secondary node. Note
|
The following sections list the format, parameters, and examples for each endpoint type.
ConnectionStringURI SRV address
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?[authSource=<authenticationDatabase>]<other_options>
Parameters:
|
Parameter |
Description |
|
|
A prefix indicating that a DNS SRV record is used for service discovery. |
|
|
The username of the database account. Default value: root. |
|
|
The password of the database account. |
|
|
The hostname. |
|
|
The name of the database that you want to access. Default value: admin. |
|
|
The database to which the database account belongs. Note
For ApsaraDB for MongoDB SRV connection strings, the TXT record sets |
|
|
Other optional parameters. Note
For SRV connections, the SSL option is enabled by default. If you do not need to use SSL encryption, add the |
Example:
The database account is test, and it belongs to the admin database.
mongodb+srv://test:****@dds-2zef4c23****-srv.mongodb.pre.nosql.aliyuncs.com/admin?ssl=false
ConnectionStringURI address
ConnectionStringURI addresses are available for both the Mongos and Shard components.
Mongos
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]
|
Parameter |
Description |
|
|
The username of the database account. Default value: root. |
|
|
The password of the database account. |
|
|
The Domain Information of a Mongos node. |
|
|
The Port of a Mongos node. |
|
|
The name of the database that you want to access. Default value: admin. |
|
|
The database to which the database account belongs. |
Example:
The database account is test, and it belongs to the admin database.
mongodb://test:****@s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717,s-bp1773180e38****.mongodb.rds.aliyuncs.com:3717/admin
Shard
The ConnectionStringURI address for a Shard component includes the endpoints of all nodes in the replica set, providing high availability. Use this address in production environments.
In production environments, always connect by using the ConnectionStringURI address. This ensures that your client can always discover and connect to the current primary node, preventing read/write issues after a failover.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]
Parameters:
|
Parameter |
Description |
|
|
The username of the database account. Default value: root. |
|
|
The password of the database account. |
|
|
The Domain Information of the primary, secondary, and read-only nodes. |
|
|
The Port of the primary, secondary, and read-only nodes. |
|
|
The name of the database that you want to access. Default value: admin. |
|
|
The database to which the database account belongs. |
|
|
Controls the read preference for read/write splitting. Write requests are routed to the primary node, and read requests are routed according to the selected mode. Valid values:
Note
When read requests are sent to secondary and read-only nodes, load distribution may be uneven. To further target specific nodes, set the readPreferenceTags parameter. |
|
|
Routes read requests to nodes that match specific tags. Typically used with readPreference and incompatible 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://test:****@d-bp1586cb62bf53b****.mongodb.rds.aliyuncs.com:3717,d-bp1586cb62bf53b****.mongodb.rds.aliyuncs.com:3717/admin
Mongos address
Format:
<host>:<port>
Parameters:
|
Parameter |
Description |
|
|
The Domain Information of a Mongos node. |
|
|
The Port of a Mongos node. |
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
Shard address
Format:
<host>:<port>
Parameters:
|
Parameter |
Description |
|
|
The Domain Information of a Shard node. |
|
|
The Port of a Shard node. |
Example:
d-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
ConfigServer address
Format:
<host>:<port>
Parameters:
|
Parameter |
Description |
|
|
The Domain Information of a ConfigServer node. |
|
|
The Port of a ConfigServer node. |
Example:
cs-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
Connect to a MongoDB database
-
Gather the following information:
-
A database account. The default account is root.
ImportantFor security, avoid using the root account in production environments. 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. When the database account is root, the corresponding database is admin.
-
Connect to the database by using one of the following methods: