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. This topic describes how to obtain and use these endpoints.
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
Network types of endpoints
Network type | Description |
Private network |
|
Public network | Due to security risks, ApsaraDB for MongoDB instances do not provide public endpoints by default. If you need to connect to your instance over a public network, you can manually apply for a public endpoint. |
Endpoint types
Endpoint type | Description |
ConnectionStringURI SRV address (Recommended) | In MongoDB, an SRV record is a DNS type that simplifies the connection string and supports automatic cluster member discovery. Important
|
ConnectionStringURI address | ApsaraDB for MongoDB provides ConnectionStringURI addresses for Mongos and Shard components. Compared with single-node connection strings, we recommend that you use a ConnectionStringURI address to connect your application to the database in production environments. The client automatically sends requests to multiple nodes of the Mongos component for load balancing. If a Mongos node fails, the client can automatically perform a failover and send requests to a healthy Mongos node. 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 describe the formats, 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. |
| This parameter specifies 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=falseConnectionStringURI address
The following sections describe the ConnectionStringURI addresses for 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. |
| This parameter specifies 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/adminShard
The ConnectionStringURI address for a Shard component offers high availability by including the endpoints of all nodes in the replica set. Using this address is recommended for production environments.
In production environments, 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. |
| This parameter specifies the database to which the database account belongs. |
| Controls the read preference for read/write splitting. This parameter routes write requests to the primary node and read requests 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. This parameter is typically used with readPreference and is incompatible with the
|
You can combine the readPreference and readPreferenceTags parameters to meet the requirements of different scenarios:
Read failover policy | Read target nodes | Parameter combination |
If a read operation fails, route read requests to the primary node. | Prefer the primary node. |
|
Prefer secondary and read-only nodes. |
| |
Prefer secondary nodes. Note If secondary nodes fail, read requests are routed to the primary node. |
| |
Prefer read-only nodes. Note If read-only nodes fail, read requests are routed to the primary node. |
| |
If a read operation fails, do not route read requests to the primary node. | Read only from secondary and read-only nodes. |
|
Read only from secondary nodes. Note If secondary nodes fail, the read operation fails. |
| |
Read only from read-only nodes. Note If read-only nodes fail, the read operation fails. |
|
Example:
mongodb://test:****@d-bp1586cb62bf53b****.mongodb.rds.aliyuncs.com:3717,d-bp1586cb62bf53b****.mongodb.rds.aliyuncs.com:3717/adminMongos 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:3717Shard 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:3717ConfigServer 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:3717Connect 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: