ApsaraDB for MongoDB provides a connection string for the primary node and each secondary node in a replica set instance. ApsaraDB for MongoDB also provides a Uniform Resource Identifier (URI) for each replica set instance. The URI of a replica set 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 URI of a replica set instance. This topic also describes how to log on to a specific database of a replica set instance.

View the connection strings and URI of a replica set instance

  1. Log on to the ApsaraDB for MongoDB console.
  2. In the upper-left corner of the page, select the resource group and the region of the target instance.
  3. In the left-side navigation pane, click Replica Set Instances.
  4. Find the target instance and click its ID.
  5. In the left-side navigation pane, click Database Connections. On the page that appears, view the connection strings and URI of the replica set instance.
    Connection addresses

Introduction to connection strings and URIs

Item Description
Connection address type
  • Classic network endpoint: Classic network endpoints are used for communication over the classic network. In the classic network, Alibaba Cloud services are not isolated. To block unauthorized traffic, you must configure security groups or IP address whitelists.
  • VPC endpoint: Virtual private cloud (VPC) endpoints are used for communication over VPCs. A VPC is an isolated network that provides higher security and higher performance than the classic network. By default, ApsaraDB for MongoDB provides VPC endpoints for ApsaraDB for MongoDB instances to ensure high security and high performance.
  • Public endpoint: Public endpoints are used for communication over the Internet. If you connect to an ApsaraDB for MongoDB instance over the Internet, the instance may be exposed to security risks. By default, ApsaraDB for MongoDB does not provide public endpoints for ApsaraDB for MongoDB instances. If you want to connect to an ApsaraDB for MongoDB instance from a device that resides outside Alibaba Cloud, you must apply for a public endpoint. These devices include on-premises machines. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.
Role
  • Primary node: If you connect to the primary node of a replica set instance, you can perform read and write operations on the databases of the instance.
  • Secondary node: If you connect to a specific secondary node of a replica set instance, you can perform only read operations on the databases of the instance.
    Note You can add secondary nodes to a replica set instance. For more information, see Change the number of nodes for a replica set instance.
  • URI: If you connect to a replica set instance by using the URI of the instance, you can balance loads and achieve high availability.
Connection strings of primary and secondary nodes The connection strings of the primary node and secondary nodes in a replica set instance are in the following format:
<host>:<port>
Example:
dds-bp1f6e65c95f5****.mongodb.rds.aliyuncs.com:3717
Note
  • The host variable specifies the endpoint that is used to connect to the replica set instance.
  • The port variable specifies the port number that is used to connect to the replica set instance.
  • During routine tests, you can directly connect to the primary node. A failover changes the roles of the nodes in the replica set instance. This affects read and write operations. For more information, see Trigger a primary/secondary failover for a replica set instance.
URI The URI of a replica set instance is in the following format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Example:
mongodb://root:****@dds-bp1f6e65c95f5****.mongodb.rds.aliyuncs.com:3717,dds-bp1f6e65c95f5****.mongodb.rds.aliyuncs.com:3717,dds-bp1f6e65c95f5****.mongodb.rds.aliyuncs.com:3717,dds-bp1f6e65c95f5****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-45816503
Note
  • The mongodb:// part is the prefix of the URI. This part indicates that the string is a URI.
  • The username:password@ parameter specifies the username and password of the account that is used to connect to the replica set instance. You must separate the username and password with a colon (:).
  • The hostX:portX parameter specifies the endpoint and port number that are used to connect to the replica set instance.
  • The /database parameter specifies the name of the database on which the specified account has permissions. If you do not specify this parameter, the default database named test is connected.
  • ?The options parameter specifies the additional options that are used to connect to the replica set instance.
  • We recommend that you use the URI to connect to the replica set instance in a production environment. This way, a failover in the replica set instance does not affect the read and write operations that are initiated from your application.

Log on to a specific database of a replica set instance

  1. Obtain the following information:
    • The connection strings or URI of the replica set instance. For more information, see the "View the connection strings and URI of a replica set instance" section of this topic.
    • The account that is used to connect to the replica set instance. The initial account is the root account.
      Note We recommend that you do not use the credentials of the root account to connect to the replica set instance in a production environment. You can create accounts and grant permissions to the accounts based on your business requirements. For more information, see Manage user permissions on MongoDB databases.
    • The password of the account that is used to connect to the replica set instance. If no password is set for the account or you forget the password of the account, you can set or change the password of the account. For more information, see Reset the password.
    • The name of the database on which the specified account has permissions. If the root account is specified, you can log on to the database named admin.
  2. Log on to the specified database. For more information, see the following topics:

Common connection scenarios

FAQ