This topic describes how to connect to a standalone ApsaraDB for MongoDB instance by using the mongo shell. The mongo shell is a database management tool that comes with 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 endpoint of the primary node.
    Obtain the endpoint
    Table 1. Database connection information
    Item Description
    Connection type
    • Intranet Connection: A virtual private cloud (VPC) is an isolated virtual network that provides higher security and higher performance than the classic network. By default, ApsaraDB for MongoDB provides endpoints on a VPC.
    • Public IP Connection: By default, ApsaraDB for MongoDB instances do not provide public endpoints because connecting to instances over the Internet poses security risks. If you want to connect to a sharded cluster 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.
    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.
    Address The connection string of the primary node is in the format of <host>:<port>.
    Note
    • <host>: the endpoint of the primary node.
    • <port>: the service port number 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, which indicates a connection string URI.
    • username:password@: the account and password used to log on to a database of the ApsaraDB for MongoDB instance. Separate the username and password with a colon (:).
    • hostX:portX: the endpoint and port number of the instance.
    • /database: the name of the authentication database to which the database account belongs.
    • ? 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 endpoint of the primary node, which includes the domain name 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 a production environment. You can create accounts and grant permissions to the accounts. For more information, see Manage user permissions on MongoDB databases.
    • <database>: the name of the authentication database to which the database account belongs. If the database account is root, enter admin. If you want to specify a database other than the authentication database, run the db.createUser() command to create an account and then use the account to connect to the database.

    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 account and press Enter. If you forget the password of the root user, you can reset the password. For more information, see Set a password for a standalone ApsaraDB for MongoDB instance.
    Note The password you enter is not displayed.

Common connection scenarios

Related FAQ