All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to an ApsaraDB for MongoDB standalone instance by using the mongo shell

Last Updated:Nov 01, 2023

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 an ApsaraDB for MongoDB standalone instance by using the mongo shell.

Prerequisites

An ApsaraDB for MongoDB standalone instance is created.

Background information

The default database admin is provided for an ApsaraDB for MongoDB standalone 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 the mongo shell that uses the same engine version as the ApsaraDB for MongoDB instance and install it on the client which can be an on-premise server or an ECS instance. For more information, visit MongoDB Shell Download and The mongo Shell.

    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 an ApsaraDB for MongoDB standalone 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 ApsaraDB MongoDB standalone instance.

    Obtain an endpoint

    Parameter

    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 an ApsaraDB for MongoDB standalone instance.

    Primary

    Format:

    <host>:<port>

    The parameters used in the connection string URI:

    • <host>: the domain name information used to connect to the node.

    • <port>: the port number used to connect to the node.

    Example:

    dds-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com:3717

    ConnectionStringURI

    Format:

    mongodb://<username>:<password>@<host>:<port>/<database>[?&authSource=<authenticationDatabase>]

    The parameters used in the connection string URI:

    • <username>: the name of the database account. Default value: root.

    • <password>: the password of the database account.

    • <host>: the domain name information used to connect to the node.

    • <port>: the port number used to connect to the node.

    • <database>: the name of the database to which you want to connect. Default value: admin.

    • authSource=<authenticationDatabase>: specifies the database to which the specified database account belongs. This parameter is optional. <authenticationDatabase>: the name of the database used for authentication. If you do not specify the authentication database, the database specified by <database> is used as the authentication database.

      Important

      For the authentication to succeed, the specified database account must belong to the specified authentication database.

    Example:

    The database account is test and the database is admin.

    mongodb://test:****@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 <port> -u <username> -p --authenticationDatabase <authenticationDatabase>

    The parameters used in the connection string URI:

    • <host>: the domain name information used to connect to the node.

    • <port>: the port number used to connect to the node.

    • <username>: the name of the database account. Default value: root.

    • <authenticationDatabase>: the name of the database used for authentication. The database account is created in this database. 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.

      Important

      For the authentication to succeed, the specified database account must belong to the specified authentication database.

    Example:

    The database account is test and the database is admin.

    mongo --host dds-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com --port 3717 -u test -p --authenticationDatabase admin
  8. When Enter password: is displayed, enter the password for the database account and press Enter.

    Enter password:<password>

    Parameters: <password> is the password used to connect to the instance. The password characters are not displayed when you enter the 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-bp1ea17b41abecf43****.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