This topic describes how to connect to a standalone ApsaraDB for MongoDB instance by using the mongo shell, which is a database management tool built in MongoDB. You can install the mongo shell on your client or in an ECS instance.
- Mongo shell 3.0 or later is installed to ensure successful authentication. For more information about the installation process, visit Install MongoDB at the official MongoDB website.
- 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.
- 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 Replica Set Instances.
- Find the target instance and click its ID.
- In the left-side navigation pane, click Database Connection to obtain the connection addresses of the primary node.
Table 1. Database connection information Item Description Address type
- VPC connection address: A VPC is an isolated virtual network with better security and performance than a classic network. By default, an ApsaraDB for MongoDB instance provides VPC connection addresses.
- Public connection address: By default, ApsaraDB for MongoDB instances do not provide public connection addresses because connecting to instances over the Internet poses security risks. If you want to connect to an ApsaraDB for MongoDB instance from a device outside of Alibaba Cloud (such as a local device), you must apply for a public endpoint. For more information, see Apply for a public endpoint for a standalone ApsaraDB for MongoDB instance.
Role Primary: indicates the primary node of the ApsaraDB for MongoDB instance. You can connect to this node to perform read/write operations on the database. Connection string of the primary node The address of the primary node is in the format of
- <host>: the endpoint of the primary node.
- <port>: the service port of the primary node.
Connection string URI The connection string URI is in the following format:
- mongodb://: the prefix, indicating a connection string URI.
- username:password@: the username and password used to connect to the ApsaraDB for MongoDB instance. Separate them with a colon (:).
- hostX:portX: the endpoint and port number of the instance.
- /database: the name of the authentication database. It is the database where the database account is created.
- ? options: additional connection options.
- Run the following command on the local server or ECS instance where the mongo shell
is installed to connect to the database:
mongo --host <host:port> -u <username> -p --authenticationDatabase <database>Note
- <host:port>: the connection string of the primary node, including the endpoint and port number.
- <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 user in the production environment. You can create users and grant permissions based on your business needs. For more information, see Manage MongoDB users though DMS.
- <database>: the name of the authentication database. It is the database where the database account is created. If the database account is root, enter admin.
mongo --host dds-bpxxxxxxxxxx.mongodb.rds.aliyuncs.com:3717 -u root -p --authenticationDatabase admin
Enter password:is displayed, enter the password for the database user and press Enter. If you forgot the password for the root user, you can reset it. For more information, see Set a password.Note The password you enter is not displayed.
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