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 a Uniform Resource Identifier (URI) for each sharded cluster instance. The URI of a sharded cluster instance is used to establish a highly available connection 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 a sharded cluster instance.
View the connection strings and URIs of a sharded cluster 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 | 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. 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 replica set instance. |
Endpoint types
Endpoint type | Description |
Mongos endpoint | The endpoint of a mongos component in a sharded cluster instance, including the endpoint of the primary node. Note When you perform routine testing, you need only to connect to a mongos component. |
Shard endpoint | The endpoint of a shard component in a sharded cluster instance, including the endpoints of the primary, secondary, and read-only nodes. Note
|
ConfigServer endpoint | The endpoint of the ConfigServer component in a sharded cluster instance, including the endpoints of the primary and secondary nodes. Note
|
Connection string URI | ApsaraDB for MongoDB provides connection string URIs for mongos and shard components in a sharded cluster instance. If your application is deployed in a production environment, we recommend that you use a URI to connect to the sharded cluster instance. This way, your client can automatically distribute requests to mongos components to balance the loads within the instance. If a mongos component fails, your client can automatically redirect requests to other healthy mongos components. Note By default, only the connection string URIs of mongos components in a sharded cluster instance are displayed in the ApsaraDB for MongoDB console. To view the connection string URI of a shard component in the instance, apply for the corresponding connection string. For more information, see Apply for an endpoint for a shard node or the 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 components are added or removed. Your client can interact with the instances without service interruptions, which simplifies the design and maintenance of applications. If your application is deployed in a production environment, we recommend that you use a URI to connect to the sharded cluster instance. This way, your client can automatically distribute requests to mongos components to balance the loads within the instance. If a mongos component fails, your client can automatically redirect requests to other healthy mongos components. Important
|
The following section describes the format, parameters, and examples of each endpoint:
Mongos endpoint
Format:
<host>:<port>
The following table describes the parameters that are included in the preceding command.
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 endpoint
Format:
<host>:<port>
The following table describes the parameters that are included in the preceding command.
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 endpoint
Format:
<host>:<port>
The following table describes the parameters that are included in the preceding command.
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 URI
The following section describes the connection string URI of a mongos or shard component.
Mongos
If your application is deployed in a production environment, we recommend that you use a URI to connect to the sharded cluster instance. This way, your client can automatically distribute requests to mongos components to balance the loads within the instance. If a mongos component fails, your client can automatically redirect requests to other healthy mongos components.
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
The connection string URI of a shard component in a sharded cluster 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 includes the endpoints of all nodes in the component. If your application is deployed in a production environment, we recommend that you use a URI to connect to the sharded cluster instance.
If you connect to this URI, all requests are processed by the primary node. Read/write operations are not affected by primary/secondary switchover.
Format:
mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database>?replicaSet=<replicaSet_value>[&authSource=<authenticationDatabase>]
The following table describes the parameters that are included in the preceding command.
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 replica set instance. |
| The database to which the specified database account belongs. |
| Specifies that write requests are sent to the primary nodes and read requests are randomly sent to secondary and read-only nodes. This parameter ensures read and write splitting and load balancing. Valid values:
Note This parameter is available only for instances that use cloud disks. |
| Specifies that read requests are preferentially sent to the node corresponding to the specified tag.
Note This parameter is available only for instances that use cloud disks. |
The readPreference
and readPreferenceTags
parameters are available for instances that use cloud disks. The combinations of different tags can meet the requirements of various scenarios. The following table describes parameter combinations in different scenarios.
Method used to process failed 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 |
| |
Preferentially read data from read-only nodes |
| |
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 |
| |
Read data only from read-only nodes |
|
Example:
mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****
SRV HA endpoint
Format:
mongodb+srv://<username>:<password>@<srv-host>/<database>?<options>
The following table describes the parameters that are included in the preceding command.
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 hostname. |
| 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 SVR HA endpoint is true. To establish a successful connection to a sharded cluster instance, add the |
Example:
In the following sample command, the username is test and the database is admin.
mongodb+srv://test:****@dds-2zef4c23xxxx-srv.mongodb.pre.nosql.aliyuncs.com/admin?ssl=false
Log on to a specific database of a sharded cluster instance
Obtain the following information:
The connection strings or URIs of the sharded cluster 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 sharded cluster 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 sharded cluster 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 database For more information, see the following topics: