MongoDB Server and Compass Installation Guide for Windows 11
In this article, we will explain in detail and step by step how to install MongoDB. MongoDB supports all the main operating systems, such as Windows, Ubuntu, RedHat, macOS. We will concentrate the installation only on the Windows.
The installation itself is very simple, and it has no big problems. But it may cause problems if not all steps are carried out since the process is not that of the typical installation of the following. There are other things we can do. We will also set up environment variables so that everything works perfectly. Additionally, we will explain some additional concepts that will be essential for understanding how Mongo works in our system.
For those working from a different operating system, I encourage you to take a look at the official documentation: https://docs.mongodb.com/guides/server/install/
If you love it and without further delay, let’s begin!
Types of MongoDB
Within MongoDB, if we visit its website www.mongodb.com we have a multitude of projects. We are going to talk about the 4 main ones we will focus on throughout the course:
- MongoDB Atlas: It’s the cloud service provided by this database managed in different cloud providers, such as AWS, Azure or Google Cloud (GCP). It includes automation mechanisms and makes it easier to manage scalability and data security.
-Checkout Mongo DB Atlas: https://www.mongodb.com/atlas/database
-Here is a sample MongoDB Atlas:
- MongoDB Server: It’s the solution/server provided by MongoDB for working with our databases of our own team. MongoDB Server can only work from the command line/terminal.
-Checkout Mongo DB Server: https://www.mongodb.com/try/download/community
-Here is a sample MongoDB server:
As a result, anything we use MongoDB Server will be done on the command line.
- MongoDB Compass: It’s MongoDB’s official Graphical User Interface (GUI) and is managed by MongoDB itself. MongoDB Compass helps users make intelligent decisions about data structure, queries, indexing and many other actions you can take on the database.
-Checkout MongoDB Compass: https://www.mongodb.com/products/compass
-Here is a sample MongoDB compass:
- MongoDB Shell: It’s an interactive JavaScript interface that allows you to interact with MongoDB instances through the command line.
-Checkout MongoDB Shell: https://www.mongodb.com/try/download/shell
-Here is a sample MongoDB Shell:
In summary:
- MongoDB Atlas = Cloud server for our DBs
- MongoDB Server = DB server on our computer
- MongoDB Compass = Graphical User Interface (GUI)
- MongoDB Shell = Command Line Tool (CLI)
Installing MongoDB Server/ MongoShell + MongoDB Compass
Downloading MongoServer/ MongoShell + MongoDB Compass
Click on the link below to download MongoDB Server.
https://www.mongodb.com/try/download/community
On the landing page, we select the operating system and the version (I still recommend to download the current version). Perhaps when you download the installation program, your current version will be different and does not match the one in this tutorial. But there is nothing wrong with it, you can follow the tutorial without any problems.
At last, click on Download:
The MongoDB Server installer will ask whether or not we want to install MongoDB Compass. We could use an installer.
Installation of the MongoDB Server + MongoDB compass.
Once downloaded, we perform and begin the installation:
Check the checkbox next to ‘I accept the terms of the License Agreement’ and click again on ‘Next’.
Click on ‘Complete’ to install all the functions of MongoDB. As for “Custom”, this option would be used to install only the specific components of MongoDB and also if a user wants to change the location from where the installation should be performed.
And we have come to a key point, the parameters as a service tab. Let default “Data Directory” and “Log Directory”. Leaving selected means that MongoDB will execute automatically as a service at OS startup. In addition, it is convenient to save the path C:\Program Files\MongoDB\Server\6.0 since later we will go to the installation and if we have the path it will be easier for you to find the installation directory.
If we look here, he asks if we want to set up the MongoDB Compass, we say yes. Thus, we killed two birds with one blow, and installed both simultaneously:
Click on “Install”:
And we let it be installed:
Once installed, we finalize the installer.
And we will see that the MongoDB Compass has been automatically opened:
Let’s close it now. To open it later, when required, simply search for MongoCompress from Windows:
Installation of the MongoDB Shell (mongosh)
Click on the link below to download MongoDB Server.
https://www.mongodb.com/try/download/shell
On the landing page, we select the operating system and the version (I still recommend to download the current version).
Extract the files from the downloaded archive.
Copy these files from the extracted folder to the folder where MongoDB Server is installed.
Copy all files from bin folder in the extracted folder
Then paste both files to here Where MongoDB server is installed.
C:\Program Files\MongoDB\Server\6.0\bin
This path will be different depending on the version of MongoDB server
Done! Lets go next step.
Configuring MongoDB Server + MongoDB Compass
If we run below command in windows command (both will give the same result)
mongosh --version
mongod --version
and everything is setup correctly. we will show the version of Mongo which we downloaded:
But when we execute him, we see that this is not the case. When it does occur, we have two options:
- No MongoDB installation — we just installed it and saw its files, so we discarded them. We even know the version we have installed, 6.0 since it has come out when selecting the download, during the download/install and in the path of the folder of our local directory it also appears to us.
One example that we have installed MongoDB is that, if we go into the mongoDB installation path, more precisely into the bin folder. We may see that this folder contains the executable we will need:
C:\Program Files\MongoDB\Server\6.0\bin
This path will be different depending on the version of MongoDB server
We copy the path to our bin folder located in our MongoDB system:
And from the Windows terminal (with a route CD) and we operate in the same way, we can see that the outcome is now as expected.
Configuring the environment variables
Therefore, we will touch the environment variables so that we do not have to place ourselves in the path C:\Program Files\MongoDB\Server\6.0\bin constantly to execute mongosh.exe and mongod.exe and thus make it easier to execute Mongo instructions from any terminal path.
That leads us to look at the second option:
– Do not have the environment variables configured: in this case, if it is not white it is black… So we will have to edit the environment variables. To do this, we look for PATH in Windows:
We select the “Environment Variables” option:
We can put it at the user level or at the system level (it will affect all users):
Here we paste the route of our MongoDB:
And last of all, click OK. And we can already verify whether we did it correctly. To do this, we reopen a CMD without going through the MongoDB installation path as from the bin folder we already saw that it worked for us. But not from the outside, for this, we close and reopen the terminal (for the modifications to be implemented) and test again:
Perfect, we already have the PATH to properly configured environment variables for mongosh and mongod.
Files from the bin directory
In the bin directory of our MongoDB installation in my case in the path: C:\Program Files\MongoDB\Server\6.0\bin (although as we have said yours may vary in something) we have to highlight for the moment mainly two files:
- mongosh.exe: It helps us work with the BBDD.
- mongod.exe: It allows us to launch the service that is necessary to be able to work with the MongoDB database, if it is launched it will no longer be necessary to launch it.
If we run mongod.exe or from CMD mongod (from any path). we will be able to see that we receive an error message indicating that the C:/data/db directory path does not exist and that we have to create it or specify another path.
Actually, after running to the end, we can see that mongod.exe while giving the error stopped running in our terminal.
Creation of a mongoDB directory system -> ‘C:/data/db’
Mongo has a predefined structure that requires us to create a series of folders in the C: drive. Specifically, the set of directories to be created will be ‘C:/data/db’ directory that will store the collections (we’ll see what they are, don’t worry) that we will save in MongoDB.
If we start again mongod.exe or from CMD mongod:
This can be seen from the address 127.0.0.1 (localhost) in port 27017. As a result, we already have this step perfectly configured.
WE DON’T CLOSE THIS IMPLEMENTATION OF MONGOD’S CMD AT THE MOMENT
As we can see, the directory, which we just created and in which we had no content, which was empty, was filled with files. This is a good sign!
We’re begin mongoDB for the first time.
- Start the mongosh.exe file from the bin folder of our MongoDB installation.
- From a CMD (for me the best option, since it is the most comfortable) through the mongosh or mongosh.exe instruction regardless of the route where we are since we have previously configured the PATH a little higher for it.
A perfect example of how the BBDD works is that we can show the BBDD that mongoDB provides by default:
show databasess
The advantage of making the service work means it is not necessary to run Mongod in order to work with Mongosh.
Starting/testing the Mongod service
If you have followed all the steps of this tutorial while installing, we have installed mongoDB as a service:
If you press the Windows key and search for “Service” and access Windows services
We can see that the MongoDB service is running:
Another option is to press control CTRL + SHIFT + END > Task Manager and go to the Services tab.
In the hypothetical case, whether it does not appear or seems restrained (we can stop it ourselves)
This service is equivalent to executing and maintaining console execution with mongod.exe. If we have stopped it, we must start the service or launch a CMD with mongod.exe without stopping it so that it works correctly.
If we have the service stopped and the mongod.exe not executed we will not be able to execute mongosh.exe successfully and, therefore, we will not be able to interact with the BBDD.
If we have the mongod service stopped and we try to run mongosh.exe we see that we get the Error “Connect ENCONNRFUSED 127.0.0.1:27017”:
We correct this by simply executing the command line’s mongod statement without closing this tab:
Or begin the service. This is why we have marked this before because it is very convenient to always have it initialized and avoid the passage of mongod)
The MongoDB server has already been set up. Congratulations! Now we just need a small configuration in order to also use MongoDB Compass.
Configuring MongoDB Compass
If we open MongoDB Compas, on its initial screen, it will show us that we paste our connection chain and We get this string from executing the mongosh command from the CMD:
Watch out! Just copy mongodb://127.0.0.1:27017 Not the complete URL!
And the window is going to open with our databases:
And we see that we have the same databases as from the CMD show from mongosh.exe of mongoDB Server:
Finally, now, yes, we already have MongoDB Server and MongoDB Compass functioning properly!
If you would like to ask me questions, please feel free! I’ll always have my mailbox open. Whether you have a question or would like to simply say hello. I will do my utmost to respond!