All Products
Search
Document Center

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

Last Updated:Feb 11, 2025

This topic describes how to connect to a sharded cluster 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 client or an Elastic Compute Service (ECS) instance.

Prerequisites

Procedure

  1. Go to the Sharded Cluster Instances page. In the top navigation bar, select the region in which the instance resides. Then, find the instance and click the ID of the instance.

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

  3. Connect to the sharded cluster instance from your on-premises client or ECS instance that has the mongo shell installed.

    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.

      Important

      We recommend that you do not connect to a specific database in the production environment by using the root account. You can create accounts and grant the required permissions to the accounts. For more information, see Manage the permissions of MongoDB database users.

    • <authenticationDatabase>: the name of the authentication database to which the database account belongs. If the username 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 s-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com --port 3717 -u test -p --authenticationDatabase admin
  4. When the Enter password: message is displayed, enter the password of the database user and press the Enter key.

    Note

    The password characters are not displayed when you enter the password.

Common connection scenarios

FAQ