ApsaraDB for MongoDB provides a connection string for each mongos component, each shard component, and the ConfigServer component in a sharded cluster instance. ApsaraDB for MongoDB also provides connection string Uniform Resource Identifiers (URIs) for mongos and shard nodes in the instance. The URIs of a sharded cluster instance are used to establish high-availability (HA) connections between your application and the instance. This topic describes how to view the connection strings and URIs of a sharded cluster instance and how to log on to a specific database of the instance.
View the connection strings and URIs of the instance
Go to the Sharded Cluster Instances page. In the top navigation bar, select the region in which the instance resides. Then, find the instance and click the ID of the instance.
In the left-side navigation pane of the instance details page, click Database Connections. In the page that appears, view the connection information of a node in the instance.
Connection strings and URIs
Network types of endpoints
Network type | Note |
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. If you want to connect to an ApsaraDB for MongoDB instance over the Internet, you must apply for a public endpoint. For more information, see (Optional) Apply for a public endpoint for an ApsaraDB for MongoDB instance. |
Endpoint types
Endpoint type | Description |
Mongos endpoint | The endpoint of the primary node in a mongos component in the instance. The endpoint allows you to connect to the primary node in the component. Note When you perform routine testing, you need only to connect to a mongos node. |
Shard endpoint | The endpoint of a shard node in a shard component in the instance. The endpoint allows you to connect to the primary node, a secondary node, or a read-only node in the component. Note
|
ConfigServer endpoint | The endpoint of a ConfigServer node in the ConfigServer component in the instance. The endpoint allows you to connect to the primary node or a secondary node in the component. Note
|
Connection string URI | ApsaraDB for MongoDB provides a connection string URI for mongos or shard nodes in the instance. If your application is deployed in a production environment, we recommend that you use a connection string URI to connect to the instance. This way, your client can automatically distribute requests to mongos nodes to balance the loads within the instance. If a mongos node fails, your client can automatically redirect requests to other healthy mongos nodes. Note By default, only the connection string URI of mongos nodes in the instance are displayed in the ApsaraDB for MongoDB console. To view the connection string URI of shard nodes in the instance, apply for an endpoint for a shard node. For more information, see Apply for an endpoint for a shard or ConfigServer node in a sharded cluster instance. |
SRV HA endpoint | SRV HA endpoints can simplify the maintenance and management of sharded cluster instances. You do not need to modify the endpoints when mongos nodes are added or removed. Your client can interact with the instances without service interruptions, which simplifies the design and maintenance of your application. If your application is deployed in a production environment, we recommend that you use a connection string URI to connect to the instance. This way, your client can automatically distribute requests to mongos nodes to balance the loads within the instance. If a mongos node fails, your client can automatically redirect requests to other healthy mongos nodes. Important
|
The following section describes the format, parameters, and examples of each endpoint:
Mongos endpoints
Format:
<host>:<port>
Parameters:
Parameter | Description |
| The domain name of the node in the mongos component. |
| The port number of the node in the mongos component. |
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
Shard endpoints
Format:
<host>:<port>
Parameters:
Parameter | Description |
| The domain name of the node in the shard component. |
| The port number of the node in the shard component. |
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
ConfigServer endpoints
Format:
<host>:<port>
Parameters:
Parameter | Description |
| The domain name of the node in the ConfigServer component. |
| The port number of the node in the ConfigServer component. |
Example:
s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717
Connection string URIs
The following section describes the connection string URI of mongos or shard shards.
Mongos nodes
If your application is deployed in a production environment, we recommend that you use a connection string URI to connect to the instance. This way, your client can automatically distribute requests to mongos nodes to balance the loads within the instance. If a mongos node fails, your client can automatically redirect requests to other healthy mongos nodes.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>[?&authSource=<authenticationDatabase>]
Parameter | Description |
| The name of the database account used to log on to the database. Default value: root. |
| The password of the database account. |
| The domain name of the node in the mongos component. |
| The port number of the node in the mongos component. |
| The name of the database to which you want to connect. Default value: admin. |
| The database to which the specified database account belongs. |
Example:
In the following sample command, the username is test and the database is admin.
mongodb://test:****@s-bp1c010266f6****.mongodb.rds.aliyuncs.com:3717,s-bp1773180e38****.mongodb.rds.aliyuncs.com:3717/admin
Shard nodes
The connection string URI of shard nodes in the instance is an HA connection string URI that can implement load balancing and ensure high availability. You can use the connection string URI to connect to the instance to perform read/write operations on databases in the instance. The endpoint allows you to connect to all shard nodes in the instance. If your application is deployed in a production environment, we recommend that you use a connection string URI to connect to the instance.
If you use this URI to connect to the instance, all requests are processed by primary nodes in the instance. Read/write operations are not affected by a primary/secondary switchover.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]
Parameters:
Parameter | Description |
| The name of the database account used to log on to the database. Default value: root. |
| The password of the database account. |
| The domain name used to connect to the primary, secondary, or read-only node. |
| The port used to connect to the primary, secondary, or read-only node. |
| The name of the database to which you want to connect. Default value: admin. |
| Specifies that read requests are sent to all nodes of the instance. |
| The database to which the specified database account belongs. |
| Specifies that write requests are sent to the primary node and read requests are sent to nodes specified by the parameter. This parameter ensures read/write splitting and load balancing. Valid values:
Note If read requests are sent to secondary and read-only nodes, uneven loads may occur. To further specify nodes that receive the read requests, configure the readPreferenceTags parameter. |
| Specifies that read requests are preferentially sent to the node corresponding to the specified tag. In most cases, the parameter is specified together with the readPreference parameter and is incompatible with the
|
The readPreference
and readPreferenceTags
parameters are available for instances. The combinations of different tags can meet the requirements of various scenarios. The following table describes parameter combinations in different scenarios.
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:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****
SRV HA endpoints
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?<options>
Parameters:
Parameter | Description |
| The prefix of the connection string, which indicates that DNS SRV records are used to identify services. |
| The name of the database account used to log on to the database. Default value: root. |
| The password of the database account. |
| The hostnames. |
| The name of the database to which you want to connect. Default value: admin. |
| Other optional parameters. Note The default value of the ssl parameter in an SRV HA endpoint is true. If you no longer use the SSL encryption feature, add the ssl parameter that is set to |
Example:
In the following sample command, the username is test and the database is admin.
mongodb+srv://test:****@dds-2zef4c23****-srv.mongodb.pre.nosql.aliyuncs.com/admin?ssl=false
Log on to a specific database of the instance
Obtain the following information:
The connection strings or URIs of the instance. For more information, see View the connection strings and URIs of a sharded cluster instance.
The database account that is used to connect to the instance. The initial account is the root account.
ImportantWe recommend that you do not connect to a database in the production environment by using the root account. You can create database accounts and grant permissions to the accounts based on your business requirements. For more information, see Manage the permissions of MongoDB database users.
The password of the database account that is used to connect to the instance. If you do not specify a password for the database account or you forget the password of the account, you can set or change the password of the account. For more information, see (Optional) Reset a password.
The name of the database to which you want to connect. If the username is root, enter admin.
Log on to a specific database. For more information, see the following topics: