The mongo shell is a database management tool that comes with MongoDB. You can install the mongo shell on the client which can be an on-premise server or an Elastic Compute Service (ECS) instance. This topic describes how to connect to a standalone ApsaraDB for MongoDB instance by using the mongo shell.

Prerequisites

A standalone ApsaraDB for MongoDB instance is created.

Background information

The default database admin is provided for a standalone ApsaraDB for MongoDB instance to manage information about the default database account root. We recommend that you use a database other than admin to implement business requirements and do not perform operations on admin.

Preparations

  • Download a version of MongoDB that is the same as the database version of the ApsaraDB for MongoDB instance and install the MongoDB version on the client which can be an on-premise server or an ECS instance. For more information about the installation method, see Install MongoDB.
    If your application is deployed on an Elastic Compute Service (ECS) instance, make sure that your ApsaraDB for MongoDB instance and ECS instance meet the following requirements to ensure network connectivity: For more information about how to view ECS instance information, see View instance information.
    • Your ApsaraDB for MongoDB instance and ECS instance are deployed in the same region, and preferably belong to the same zone. This reduces network latency.
    • If the network type is VPC, you must ensure that they use the same VPC ID.
      Note If you want to use VPC, but the network type of the ECS instance is classic network, you can change the network type of the ECS instance to VPC. For more information, see Migrate ECS instances from the classic network to a VPC.
  • The IP address of the client is added to the whitelist of the ApsaraDB MongoDB instance. For more information, see Configure a whitelist for an ApsaraDB for MongoDB instance.
    Note If you want to connect to the instance over the Internet, you must apply for a public endpoint. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.

Procedure

  1. Log on to the ApsaraDB for MongoDB console.
  2. In the left-side navigation pane, click Replica Set Instances.
  3. In the upper-left corner of the page, select the resource group and region to which the instance belongs.
  4. Click the ID of an instance, or click More icon in the Actions column corresponding to the instance and select Manage.
  5. In the left-side navigation pane of the instance details page, click Database Connections.
  6. View and obtain the endpoint of the standalone ApsaraDB MongoDB instance.
    Obtain the endpoint
    Item Description
    Endpoint type
    • VPC endpoint: A virtual private cloud (VPC) is an isolated virtual network that provides higher security and higher performance than the classic network. By default, VPC endpoints are provided for instances to ensure high security and high performance.
    • Public endpoint: Public endpoints are used for communication over the Internet. If you connect to an ApsaraDB for MongoDB instance over the Internet, the instance may be exposed to security risks. By default, no public endpoints are provided for ApsaraDB for MongoDB instances. If you want to connect to the instance over the Internet, you must apply for a public endpoint. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.
    Primary
    Format:
    <host>:<port>
    Note
    • <host>: the endpoint of the instance.
    • <port>: the port number of the instance.
    Example:
    dds-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com:3717
    ConnectionStringURI
    Format:
    mongodb://<username>:<password>@<host>:<port>/<database>
    Note
    • mongodb://: the prefix which indicates a connection string URI.
    • <username>: the account used to log on to a database of the ApsaraDB for MongoDB instance. Default value: root.
    • password: the password used to log on to a database of the ApsaraDB for MongoDB instance.
    • <host>: the endpoint of the instance.
    • <port>: the port number of the instance.
    • <database>: the name of the authentication database to which the database account belongs. Default value: admin. If you want to specify a database other than the authentication database, create an account and then use the account to connect to the database. For more information about how to create an account, see db.createUser().
    Example:
    mongodb://root:****@dds-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com:3717/admin
  7. Run the following command on the local server or ECS instance where the client is installed to connect to the database:
    mongo --host <host>:<port> -u <username> -p --authenticationDatabase <database>
    Note
    • <host>: the endpoint of the instance.
    • <port>: the port number of the instance.
    • <username>: the account used to log on to a database of the ApsaraDB for MongoDB instance. Default value: root. We recommend that you do not log on to a database as the root user. 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 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-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
  8. When Enter password: is displayed, enter the password for the database account and press Enter.
    Enter password:<password>
    Note
    • password: the password used to log on to a database of the ApsaraDB for MongoDB instance. The password characters are not displayed when you enter the password.
    • If you forget the password of the account, you can reset the password. For more information, see (Optional) Reset a password.
    The following information indicates a successful connection to the instance.
    Note The actual information may vary with the version of the mongo shell.
    connecting to: mongodb://dds-bp1ea17b41abecf433330.mongodb.rds.aliyuncs.com:3717/
    MongoDB server version: 3.4.6
    Welcome to the MongoDB shell.
Note After the instance is connected, we recommend that you use a database other than admin to implement business requirements.

Common connection scenarios

FAQ