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 the primary or secondary node, and use a connection string URI to connect to both of them. This topic provides an overview of replica set instance connections.

View connection information

  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 information.
    Connection addresses

Description of connection strings

Item Description
Address type
  • Intranet Connection - Classic Network: Cloud services on the classic network are not isolated. Unauthorized access can be blocked only by security groups or whitelists.
  • Intranet Connection - VPC: A VPC is an isolated network with higher security and performance than the classic network. By default, an ApsaraDB for MongoDB instance provides internal endpoints on a VPC.
  • Public IP Connection: Connecting to a sharded cluster instance over the Internet is risky. Therefore, ApsaraDB for MongoDB does not provide public endpoints. If you want to connect to a sharded cluster instance from a device outside Alibaba Cloud such as 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 perform only 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 of a primary or secondarynode The connection string of a primary or secondary nodeis in the following format:
<host>:<port>
  • <host>: the endpoint you use to connect to the instance.
  • <port>: the port you use to connect to the instance.
Note During routine tests, you can use a connection string to directly connect to a primary node. A failover changes the roles of connected nodes, which affects read/write operations.
ConnectionstringURIConnection string URI ConnectionstringURIA 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 string is a connection string URI.
  • username:password@: the username and password used to log on to a database of the ApsaraDB for MongoDB (Serverless) instance. You must separate the username and password with a colon (:).
  • hostX:portX: the endpoint and port numbert of a node in the replica set instance.
  • /database: the database corresponding to the username and password if authentication is enabled.
    Note If /database is not specified, the default database is test.
  • ? 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 by the failover.

Log on to a database of the replica set instance

  1. Obtain the connection addresses and the following information:
    • The username used 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 the production environment. You can create users and grant permissions to them. For more information, see Manage user permissions on MongoDB databases.
    • The password of the database user. If you forget the password of the root user, you can reset it. For more information, see Reset the password.
    • The name of the database corresponding to the username if authentication is enabled. If the username is root, enter admin.
  2. Log on to the database.

Common connection scenarios

FAQ