This topic provides an overview of replica set instance connections. ApsaraDB for MongoDB supports both connection strings and connection string URIs. You can use a connection string to connect to either the primary or secondary node, and use a connection string URI to connect to both of them. For high availability, we recommend that you use connection string URIs to connect your application to both primary and secondary nodes.

View connection addresses

  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 Connection to view connection addresses.
    Connection addresses

Introduction to connection addresses

Item Description
Address type
  • Intranet Connection - Classic Network: Cloud services on a classic network are not isolated. Unauthorized access can only be blocked by using security groups or whitelists.
  • Intranet Connection - VPC: A VPC is an isolated network with higher security and performance than a classic network. By default, ApsaraDB for MongoDB provides endpoints on a VPC.
  • Public IP Connection: Connecting to a replica set instance over the Internet is risky. Therefore, ApsaraDB for MongoDB does not provide public endpoints. If you want to connect to a replica set instance from a device outside Alibaba Cloud (for example, a local client), you must apply for a public endpoint. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.
Role
  • Primary: the primary node in the replica set instance. If you connect to this node, you can perform read/write operations on the databases of the replica set instance.
  • Secondary: the secondary node in the replica set instance. If you connect to this node, you can only perform read operations on the databases of the replica set 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.
  • ConnectionStringURI: ApsaraDB for MongoDB allows you to use a connection string URI to connect to a sharded cluster instance to achieve load balancing and high availability.
Connection string The connection string of a primary or secondary node is in the following format:
<host>:<port>
  • <host>: the endpoint you use to connect to the replica set instance
  • <port>: the port you use to connect to the replica set instance.
Note During regular tests, you can use a connection string to directly connect to a primary node. Note that a failover changes the roles of connected nodes, which affects read/write operations.
Connection string URI A connection string URI is in the following format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][? options]]
  • mongodb://: the prefix, which indicates that the connection address is a connection string URI.
  • username:password@: the username and password you use to log on to a database of the replica set instance. You must separate them with a colon (:).
  • hostX:portX: the endpoint and port of a node in the replica set instance.
  • /database: the name of the authentication database. It is the database where the database user is created.
  • ? options: additional connection options.
Note If your application is in a production environment, we recommend that you use a connection string URI to connect to the instance. This way, when a node fails, the read/write operations of your application are not affected as a result of the failover.

Log on to a database of the replica set instance

  1. Obtain the connection addresses and the following information:
    • The username you use to log on to the database. The initial username is root.
      Note We recommend that you do not log on to a database as the root user in a production environment. You can create users and grant permissions to them based on your needs. For more information, see Manage MongoDB users through DMS.
    • The password of the database user. If you forget the password of the root user, you can reset it. For more information, see Set a password for a replica set instance.
    • The name of the authentication database. It is the database where the database user is created. If the database username is root, enter admin.
  2. Log on to the database.

Common connection scenarios

FAQ