This topic describes how to use the mongo shell to connect to a replica set instance.The mongo shell is a database management tool provided by ApsaraDB for MongoDB. You can install it on your client or in an ECS instance. This topic describes how to log on to an ApsaraDB for MongoDB instance.

Prerequisites

Procedure

  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 obtain the connection string of a node and the connection string URI.
    Note For more information about how to query the connection addresses, see Description of connection strings.
  6. Connect to the sharded cluster instance from your client or ECS instance that has the mongo shell installed.
    • Single-node connection

      During routine tests, you can directly connect to a primary or secondarynode. A failover changes the roles of connected nodes, which affects read/write operations.

      Command syntax:

      mongo --host <host> -u <username> -p --authenticationDatabase <database>
      Note
      • <host>: the connection string of the primary or secondary node.
        • Primary node: If you connect to this node, you can perform read/write operations on the databases of the replica set instance.
        • Secondary node: If you connect to this node, you can perform only read operations on the databases of the replica set instance.
      • <username>: the username you use to log on to a database of the ApsaraDB for MongoDB instance. The default username is root. 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 the users. For more information, see Manage user permissions on MongoDB databases.
      • <database>: the name of the authentication database. It is the database where the database user is created. If the database username is root, enter admin. If you want to specify another database, run the db.createUser() command to create an account, and then use the account to connect to this database.

      Example:

      mongo --host dds-bp**********.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
      When Enter password: is displayed, enter the password of the database user and press the Enter key. If you forget the password of the root user, you can reset the password. For more information, see Reset the password.
      Note The password characters are not displayed when you enter the password.
    • HA connection (recommended): You can use a connection string URI to connect to both the primary and secondary nodes of a replica set instance. This ensures that your application is always connected to the primary node and the read/write operations of your application are not affected even if the roles of the primary and secondary nodes are switched.
      Command syntax:
      mongo "<ConnectionStringURI>"    
      Note
      • The connection string URI must be enclosed in a pair of double quotation marks ("").
      • <ConnectionStringURI>: the connection string URI of the replica set instance.

        You must replace **** in the connection string URI with the database password. For more information about how to set a database password, see Reset the password.

Common connection scenarios

FAQ