Connect MongoDB Atlas Cluster Using Shell

Connect MongoDB Atlas Cluster Using Shell

In this post, we will learn to connect the MongoDB Atlas cluster using a shell command from our local machine.

To connect a cluster we must have:

  1. A cluster is created in Atlas. If you haven’t created it already you can visit my previous post Create Cluster In MongoDB Atlas and
  2. We must have a MongoDB shell installed on our computer. If you haven’t installed a MongoDB shell on your computer then you can visit The Mongo Shell and follow the steps given there.

Verify MongoDB Shell Version

Open your Terminal or Command Prompt if you are using a Windows machine and type:

mongo --version

If MongoDB shell is already installed on your computer you may see output like:

MongoDB shell version v5.0.4
Build Info: {
    "version": "5.0.4",
    "gitVersion": "62a84ede3cc9a334e8bc82160714df71e7d3a29e",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "windows",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

It is confirmed that the MongoDB Shell is installed on our machine.

Hence, let us start connecting to Atlas Cluster from our computer using MongoDB Shell.

Connect cluster using MongoDB Shell

  1. Go to your Database Deployments and select your cluster. In our case Sandbox is the cluster where we are trying to connect.
Connect MongoDB Atlas Cluster Using Shell Image 26
  1. Click Connect button on the right side. (You can directly click connect from step 1 as well. Both options are the same.)
Connect MongoDB Atlas Cluster Using Shell Image 27
  1. In the new window, we can see many options to connect. In this tutorial, we are trying to connect from MongoDB Shell. Hence, click on this.
Connect MongoDB Atlas Cluster Using Shell Image 28
  1. After clicking Connect with the MongoDB Shell it asks us to download the Mongosh. Since we have already installed MongoDB Shell which is the Prerequisite we don’t have to download and install it. If you haven’t already then you may download it from here. The only difference will be the command. You will see this in the next step. In the following screen, copy the connection string by clicking on the copy icon as shown. See the image below to understand it more clearly.
Connect MongoDB Atlas Cluster Using Shell Image 29
  1. I assume that you’ve copied the connection string but if you have installed MongoDB Shell the connection string would be:
mongo "mongodb+srv://sandbox.7dtrvh3.mongodb.net/myFirstDatabase" --apiVersion 1 --username codersathi

And if you’ve installed the Mongosh then the connection would be:

mongosh "mongodb+srv://sandbox.7dtrvh3.mongodb.net/myFirstDatabase" --apiVersion 1 --username codersathi

If you have noticed on these two connection strings only the difference is mongo and mongosh.

  1. You can see the following screen when you paste the connection string into your command line or terminal

On my computer I’ve installed MongoDB Shell hence I will be using the following command:

mongo "mongodb+srv://sandbox.7dtrvh3.mongodb.net/myFirstDatabase" --apiVersion 1 --username codersathi
Connect MongoDB Atlas Cluster Using Shell Image 30

After pasting the connection string in the command line or terminal it asks for the password. Enter the correct password.

Once the password is matched you may see the following in your terminal.

Connect MongoDB Atlas Cluster Using Shell Image 31

It means that the connection to our Atlas Cluster is successful.

List databases

In this section, we will be using the following command to see a list of available databases in our MongoDB database cluster.

show databases

By using, the above command we can see the list of databases.

Since we’ve already connected to our MongoDB Atlas cluster using MongoDB Shell let’s see the databases available there.

Connect MongoDB Atlas Cluster Using Shell Image 32

It shows all the available databases there.

Error: queryTxt ETIMEOUT

While connection to Atlas sometimes, we may see connection timeout error. And this error is very common.

If you encounter this error then our another post DNSHostNotFound: Failed to look up service “”: DNS name does not exist issue while connecting to MongoDb Atlas may help.

Summary

In this post, we learned to connect MongoDB Atlas Cluster using the MongoDB shell and we also learned to see the list of databases available there.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments