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
To ensure successful authentication, install the mongo shell that uses the same engine version as the ApsaraDB for MongoDB instance. For more information, see MongoDB Shell Download and The mongo Shell.
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.
NoteIf 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.
Procedure
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.
In the left-side navigation pane of the instance details page, click Database Connections.
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.ImportantWe 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.ImportantFor 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
When the
Enter password:
message is displayed, enter the password of the database user and press the Enter key.NoteThe password characters are not displayed when you enter the password.