All Products
Search
Document Center

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

Last Updated:Nov 01, 2024

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 your client or in an ECS instance.

Prerequisites

Procedure

  1. Log on to the ApsaraDB for MongoDB console.

  2. In the left-side navigation pane, click Sharded Cluster 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.Obtain the connection information of the mongos node.

  6. Connect to the sharded cluster instance from your client or ECS instance that has the mongo shell installed.

    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 secondary node.

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

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

      Important

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

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

      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 s-bp1ea17b41abecf43****.mongodb.rds.aliyuncs.com --port 3717 -u test -p --authenticationDatabase admin
  7. When Enter password: is displayed, enter the password of the database user and press the Enter key.

    Note

    The password characters are masked when you enter the password.

Common connection scenarios

Related FAQ