This topic describes how to connect to a standalone ApsaraDB for MongoDB instance by using the mongo shell, which is a database management tool built in MongoDB. You can install the mongo shell on your client or in an ECS 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 addresses of the primary node.
    Obtain the connection addresses
    Table 1. Database connection information
    Item Description
    Address type
    • Intranet Connection: A VPC is an isolated virtual network with better security and performance than a classic network. By default, an ApsaraDB for MongoDB instance provides VPC connection addresses.
    • Public IP Connection: By default, ApsaraDB for MongoDB instances do not provide public connection addresses because connecting to instances over the Internet poses security risks. If you want to connect to an ApsaraDB for MongoDB instance from a device outside of Alibaba Cloud (such as a local device), you must apply for a public endpoint. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.
    Node Primary: indicates the primary node of the ApsaraDB for MongoDB instance. You can connect to this node to perform read/write operations on the database.
    Connection string The address of the primary node is in the format of <host>:<port>.
    Note
    • <host>: the endpoint of the primary node.
    • <port>: the service port of the primary node.
    The connection string URI is in the following format:
    mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][? options]]
    • mongodb://: the prefix, indicating a connection string URI.
    • username:password@: the username and password used to connect to the ApsaraDB for MongoDB instance. Separate them with a colon (:).
    • hostX:portX: the endpoint and port number of the instance.
    • /database: the name of the authentication database. It is the database where the database account is created.
    • ? options: additional connection options.
  6. Run the following command on the local server or ECS instance where the mongo shell is installed to connect to the database:
    mongo --host <host:port> -u <username> -p --authenticationDatabase <database>
    Note
    • <host:port>: the connection string of the primary node, including the endpoint and port number.
    • <username>: the database account of the ApsaraDB for MongoDB instance. The initial account is root. 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 based on your business needs. For more information, see Manage MongoDB users though DMS.
    • <database>: the name of the authentication database. It is the database where the database account is created. If the database account is root, enter admin.

    Example:

    mongo --host dds-bpxxxxxxxxxx.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
  7. When Enter password: is displayed, enter the password for the database user and press Enter. If you forgot the password for the root user, you can reset it. For more information, see Set a password.
    Note The password you enter is not displayed.

Common connection scenarios

FAQ