This topic describes how to connect to a sharded cluster instance by using the mongo shell, which is a database management tool provided with MongoDB. You can install the mongo shell on your client or an ECS instance.
- To ensure successful authentication, the version of the mongo shell must match with that of the ApsaraDB for MongoDB instance. 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 sharded cluster instance.
For more information, see Configure a whitelist for a sharded cluster instance.
Note If you want to connect to the instance over the Internet, you must apply for a public endpoint.
- 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.
- In the left-side navigation pane, click Database Connection to obtain the connection string 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 connection string of a mongos in the sharded cluster instance.
- <username>: the username used to log on to a database of the sharded cluster instance. The initial username is root. We recommend that you do not log on to a database as the root user in a production environment. You can create accounts and grant permissions to them as needed. For more information, see Manage MongoDB users though DMS.
- <database>: the name of database corresponding to the username if authentication is enabled. If the username is root, enter admin.
mongo --host s-bp**********.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
Enter password:is displayed, enter the password of the database user and press Enter. If you forget the password of the root user, you can reset it. For more information, see Set a password for a sharded cluster instance.Note The password characters are not displayed when you enter the password.
Common connection scenarios
- Connect a local client to an ApsaraDB forMongoDB 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
- How to connect an ECS instance to an ApsaraDB for MongoDB instance when they do not belong to the same Alibaba Cloud account