ApsaraDB for MongoDB sharded cluster instances provide separate endpoints for Mongos, shard, and Configserver components. They also provide high-availability ConnectionStringURI and ConnectionStringURI SRV addresses for application connections. This topic describes how to obtain these endpoints and provides instructions on how to connect.
Obtain a database 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 ID of the target instance.
On the instance details page, in the navigation pane on the left, click Database Connections. Then, obtain the connection address of the target node.
Endpoint description
Network types of endpoints
Network type | Description |
Private network |
|
Internet | 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. To connect to an ApsaraDB for MongoDB instance over the Internet, apply for a public endpoint. |
Endpoint types
Endpoint type | Description |
ConnectionStringURI SRV address (Recommended) | In MongoDB, an SRV is a type of DNS record that simplifies the MongoDB connection string and supports automatic discovery of cluster members. When you add or remove Mongos nodes in a sharded cluster instance, using an SRV address lets you interact with the instance seamlessly. This simplifies application design and maintenance. Important
|
ConnectionStringURI address | ApsaraDB for MongoDB provides ConnectionStringURI addresses for Mongos and shard components. Compared to a single-node connection string, it is better to connect applications in a production environment to the database using a ConnectionStringURI. The client automatically sends requests to multiple Mongos nodes for load balancing. If a Mongos node fails, the client automatically performs a failover and sends 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, you only need to connect to any node of the Mongos component. |
Shard address | The endpoint of a shard component, including the endpoints of the primary, secondary, and read-only nodes. Note
|
Configserver address | The endpoint of a Configserver component, including the endpoints of the primary and secondary nodes. Note
|
The formats, parameters, and examples for each endpoint type are as follows:
ConnectionStringURI SRV address
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?[authSource=<authenticationDatabase>]<other_options>Parameters:
Parameter | Description |
| The prefix of the connection string. It indicates that a DNS SRV record is used for service discovery. |
| The name of the database account. The default value is root. |
| 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 to which the database account belongs. Note The TXT record of the SRV connection string for ApsaraDB for MongoDB specifies |
| 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 |
Example:
The database account is test and is in 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
For production environments, connect applications to the database using a ConnectionStringURI. The client automatically sends requests to multiple Mongos nodes for load balancing. If a Mongos node fails, the client automatically performs a failover and sends requests to a healthy Mongos node.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]Parameter | Description |
| The name of the database account. The default value is root. |
| The password of the database account. |
| The Domain Name of a node in the Mongos component. |
| The Port of a node in the Mongos component. |
| The name of the database to access. The default value is admin. |
| This parameter specifies the database to which the database account belongs. |
Example:
The database account test 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 is a high-availability endpoint that provides load balancing and contains the addresses of all nodes in the shard. You can connect to this endpoint to perform read and write operations on the database. For production environments, we recommend that you connect applications to the instance using this address.
For production environments, connect applications to the instance using a ConnectionStringURI address. This ensures that the client can always find the primary node and that primary/secondary failovers do not affect application read and write operations.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]Parameters:
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 specifies the database to which the database account belongs. |
| 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 the 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 preferentially sends read requests to the nodes that correspond to the specified tags. It is typically used with the readPreference parameter and is not compatible with the
|
The readPreference and readPreferenceTags parameters along with different tag combinations can meet various scenario requirements. The following table describes available parameter combinations.
Response policy of the primary node to receive read requests | Node to receive read requests | Parameter combination solution |
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://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 Name of a node in the Mongos component. |
| The Port of a node in the Mongos component. |
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717Shard address
Format:
<host>:<port>Parameters:
Parameter | Description |
| The Domain Name of a node in the shard component. |
| The Port of a node in the shard component. |
Example:
d-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717Configserver address
Format:
<host>:<port>Parameters:
Parameter | Description |
| The Domain Name of a node in the Configserver component. |
| The Port of a node in the Configserver component. |
Example:
cs-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717Log on to a MongoDB database
Obtain the following information:
The database account. The initial account is root.
ImportantDo not use the root account 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. If the database account is root, the corresponding database is admin.
Log on to the database using one of the following methods: