This method is applicable to interactive command-line tools and is suitable for database management, querying, and data operations.
Step 1: Logging In and Installing MongoDB Shell
2. Download MongoDB Shell: Use the wget command to download MongoDB Shell, and then decompress the file. The following example uses MongoDB Shell (mongosh) 2.5.6.
Note:
When selecting a MongoDB Shell version, ensure it is compatible with your TencentDB for MongoDB service and the CVM operating system. For download details, see MongoDB Shell Download.
wget https://downloads.mongodb.com/compass/mongosh-2.5.6-linux-x64.tgz
tar zxvf mongosh-2.5.6-linux-x64.tgz
Step 2: Running the Connection Command
1. Go to the decompressed directory and run the following command to connect to the MongoDB instance.
./bin/mongosh -u mongouser -p ******** 10.1.0.44:27017/admin
-u: Specify the username. (For example, the default user mongouser.)
-p: Specify the password for the corresponding user.
host:port: Replace with the private network address and port of the MongoDB instance.
/admin: Specify admin as the authentication database.
2. Example for connecting to multiple nodes.
./bin/mongosh -u mongouser -p your_password 10.1.0.44:27017,10.1.0.31:27017/admin
Step 3: Verifying the Connection
After a successful connection, you will see a prompt similar to the following, which includes the versions of MongoDB and Mongosh.
Connecting to: mongodb://<credentials>@10.1.0.44:27017,10.1.0.31:27017/test?replicaSet=cmgo-2jkr****_0&authSource=admin&appName=mongosh+2.5.6
Using MongoDB: 7.0.12
Using Mongosh: 2.5.6
Step 4: Reading and Writing to Databases