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
To ensure successful authentication, install the mongo shell that uses the same engine version as the ApsaraDB for MongoDB instance. For more information, visit MongoDB Shell Download and The mongo Shell.
The IP address of your client is added to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist for an ApsaraDB for MongoDB instance.
NoteIf 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 a sharded cluster instance.
Procedure
Log on to the ApsaraDB for MongoDB console.
In the left-side navigation pane, click Sharded Cluster Instances.
In the upper-left corner of the page, select the resource group and region to which the instance belongs.
Click the ID of an instance, or click in the Actions column corresponding to the instance and select Manage.
In the left-side navigation pane of the instance details page, click Database Connections.Obtain the connection information of the mongos node.
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.ImportantWe 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.ImportantFor 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
When
Enter password:
is displayed, enter the password of the database user and press the Enter key.NoteThe password characters are masked when you enter the password.