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.
- The version of the mongo shell is the same as your instance. This ensures successful authentication. For more information about the installation procedure, visit Install MongoDB. Select the correct version based on your client.
- 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 a standalone 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 upper-left corner of the page, select the resource group and the region of the target instance.
- In the left-side navigation pane, click Sharded Cluster Instances.
- Find the target instance and click its ID.
- Click Database Connections to obtain the endpoint of a mongos.
- Connect to the sharded cluster instance from your client or ECS instance that has the mongo shell installed.
mongo --host <mongos_host> -u <username> -p --authenticationDatabase <database>Note
- <mongos_host>: the endpoint of a mongos in the sharded cluster instance.
- <username>: the database account of the ApsaraDB for MongoDB instance. The initial account is root. We recommend that you do not log on to a database as the root account in a 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 database 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.
mongo --host s-bp**********.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
Enter password:is displayed, enter the password of the database account and press Enter. If you forget the password of the root account, you can reset the password. For more information, see Set a password for a standalone ApsaraDB for MongoDB instance.Note The password you enter is not displayed.
Common connection scenarios
- Connect to an ApsaraDB for MongoDB instance over the Internet
- How to 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