All Products
Search
Document Center

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

Last Updated:Feb 11, 2025

This topic describes how to connect to a standalone 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 a client that can be an on-premises server or an Elastic Compute Service (ECS) instance.

Prerequisites

A standalone instance is created.

Usage notes

The default database named admin is provided for a 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

  • The mongo shell that uses the same engine version as the replica set instance is downloaded and installed on your on-premises server or ECS instance. For more information, see MongoDB Shell Download and The mongo Shell.

    If your application is deployed on an ECS instance, make sure that your ApsaraDB for MongoDB instance and the ECS instance meet the following requirements to ensure network connectivity. For more information about how to view the information of ECS instances, 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 on-premises server or ECS instance is added to a whitelist of the instance. For more information, see Configure a whitelist for an instance.

    Note

    If you want to connect to an ApsaraDB for MongoDB instance over the Internet, you must apply for a public endpoint. For more information, see (Optional) Apply for a public endpoint for an instance.

Procedure

  1. Go to the Replica Set Instances page. In the top navigation bar, select the region in which the instance resides. Then, find the standalone instance that you want to manage and click the ID of the instance.

  2. In the left-side navigation pane of the instance details page, click Database Connections.

  3. View and obtain the endpoint of the instance.

    获取连接地址

    Item

    Description

    Connection address type

    • VPC endpoint: Virtual Private Cloud (VPC) endpoints are used for communication over VPCs. A 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 (Optional) Apply for a public endpoint for an instance.

    Primary

    Format:

    <host>:<port>

    Parameters:

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

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

    Example:

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

    ConnectionStringURI

    Format:

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

    Parameters:

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

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

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

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

    • <database>: the name of the database that you want to access. Default value: admin.

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

      Important

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

    Example:

    In the following sample command, the username is test and the database is admin.

    mongodb://test:****@dds-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com:3717/admin
  4. Run the following command on the on-premises server or ECS instance where the mongo shell is installed to connect to the instance:

    mongo --host <host> --port <port> -u <username> -p --authenticationDatabase <authenticationDatabase>

    Parameters:

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

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

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

    • <authenticationDatabase>: 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.

      Important

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

    Example:

    In the following sample command, the username is test and the database is admin.

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

    Enter password:<password>

    Parameter: <password> specifies the password that is 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