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.
- 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. Note If 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.
- 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.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 user permissions on MongoDB databases.
<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
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
- Connect to an ApsaraDB for MongoDB instance over the Internet
- Connect an ECS instance to an ApsaraDB for MongoDB instance when their network types are different
- How to connect an ECS instance to an ApsaraDB for MongoDB instance when they are in different regions
- Connect an ECS instance with an ApsaraDB for MongoDB instance in another Alibaba Cloud account