The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

Shell Connection Example

Last updated: 2025-02-08 11:17:48

You can use the MongoDB shell client (please see the installation documentation) on a CVM instance to connect to TencentDB for MongoDB for data management. Be sure to use the latest version of MongoDB client suite.

Quick Start

A typical connection command is as follows:
mongo 10.66.187.127:27017/admin -u mongouser -p thepasswordA1
Note:
When using connection string access for MongoDB, if the password contains special characters, they need to be escaped to URL encoding for normal recognition. For example, @ needs to be escaped to %40.
See the figure:


Connection in different authentication methods

In Connect Instances, it is noted that TencentDB for MongoDB default provides two usernames, rwuser and mongouser, which support MONGODB-CR and SCRAM-SHA-1 authentication methods respectively. For these two authentication methods, the shell parameters are different. See below for details.

SCRAM-SHA-1 authentication (mongouser)

The default user mongouser and all new users created in the console use SCRAM-SHA-1 authentication. Their shell connection parameters are exactly the same as in the Quick Start section, with no need to add additional parameters. Example as follows:
mongo 10.66.187.127:27017/admin -u mongouser -p thepasswordA1
If you want to directly enter a specific db, such as singer, after connecting to the MongoDB service, follow the example operation:
mongo 10.66.187.127:27017/singer -u mongouser -p thepasswordA1 --authenticationDatabase admin
As shown in the figure:
Screenshot example of the connection command directly to a db



MONGODB-CR authentication (rwuser)

Please note that only the default user rwuser uses MONGODB-CR authentication. Its shell connection parameters need to specify the authentication method as MONGODB-CR, example as follows:
mongo 10.66.187.127:27017/admin -u rwuser -p thepasswordA1 --authenticationMechanism=MONGODB-CR
As shown:
Example screenshot of MONGODB-CR authentication

If you want to directly enter a specific db, such as singer, after connecting to the MongoDB service, please follow the example:
mongo 10.66.187.127:27017/singer -u rwuser -p thepasswordA1 --authenticationMechanism=MONGODB-CR --authenticationDatabase admin
As shown in the figure:
Screenshot example of the connection command directly to a db



Using shell to import and export data

Both authentication methods mentioned above can be used to perform data import and export in the shell. Please refer to Export and Import.