How To Install MongoDB on Windows

By bhagwatchouhan
How To Install MongoDB on Windows

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source, and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and set up the MongoDB 4 Community Server on Windows 10. The steps should be the same for other versions of MongoDB including MongoDB 3 and Windows systems.

 

Download MongoDB

Open the MongoDB Download Page and download the most recent version of MongoDB for Windows as shown in Fig 1.

MongoDB Download

Fig 1

Click on the Download Button to start the download as highlighted in Fig 1.

 

Install MongoDB

In this step, we will install the MongoDB server using the MSI installer downloaded by us in the previous step. Double click the installer to start the installation. It will show the welcome screen as shown in Fig 1.

MongoDB Welcome

Fig 2

Click on the Next Button to continue with the installation. The next screen shows the License Agreement as shown in Fig 2.

MongoDB License Agreement

Fig 3

Accept the License Agreement as shown in Fig 3 and click on the Next Button to continue with the installation. The next screen asks to choose MongoDB setup type as shown in Fig 4.

MongoDB Setup Type

Fig 4

I have clicked on the Custom Button to view the available components as part of this installation process as shown in Fig 5.

MongoDB Components

Fig 5

We need at least Server and Client options to work with the server. You may omit the other components as listed in Fig 5. You can also change the installation location at this stage by clicking on the Browse... Button. Now click on the Next Button to continue the installation. The next screen provides options to configure MongoDB as a system service as shown in Fig 6.

MongoDB Service

Fig 6

I have kept the default options and clicked on the Next Button. The next screen provides options to install MongoDB Compass as shown in Fig 7. MongoDB Compass is the official graphical user interface for MongoDB to manage the database graphically similar to how we use Workbench for MySQL.

MongoDB Compass

Fig 7

I have kept the option enabled to install MongoDB Compass as shown in Fig 7. Now click on the Next Button to continue with the installation. The next screen shows the options to confirm the installation using the options selected by us in the previous steps as shown in Fig 8.

MongoDB Confirm Installation

Fig 8

Now click on the Install Button to start the installation. It will ask for system permissions to start the installation. Allow it to make the necessary changes as part of the installation process. It will show the installation progress and also ask to update the services as shown in Fig 9.

MongoDB Allow Services

Fig 9

It also needs a reboot after completing the installation as shown in Fig 9. Close all the other windows to preserve the work in progress. Now click on the OK Button to continue with the installation. It will show the installation success message after completing the installation as shown in Fig 10.

MongoDB Installed

Fig 10

Click on the Finish Button to close the installer. It will show the windows to restart the system. Now complete the steps to install the MongoDB Compass before the system restart. The installer will also launch the MongoDB Compass and it will ask to accept the License Agreement of MongoDB Compass as shown in Fig 11.

MongoDB Compass License

Fig 11

Read the License Agreement, scroll down and click on the Agree Button to accept it. It will also provide options to configure Privacy Settings as shown in Fig 12.

MongoDB Compass Privacy

Fig 12

Configure your Privacy Settings and click the Start Using Compass Button to apply the settings. It will show the default screen of MongoDB Compass as shown in Fig 13.

MongoDB Compass Welcome Screen

Fig 13

This will complete the installation and setup of MongoDB with Compass. It will also create the Desktop Icon to launch the MongoDB Compass from the desktop. Close the MongoDB Compass. Now we can restart the system as shown in Fig 14.

MongoDB System Restart

Fig 14

Now click on the Yes Button to restart the system.

 

System Path

We can add MongoDB to system PATH to access it directly from the command prompt without navigating to the bin directory. You can follow the below-mentioned steps to do so.

Right Click -> My Computer(This PC) -> Properties -> Advanced System Settings

The above steps will open the Windows settings panel. Now click on Environment Variables, select Path under the System Variables section, and click on Edit. We need to add the path of installed MongoDB to the system Path.

Remove the path of previously installed MongoDB. Now click on New Button and add the path to installing MongoDB bin which is E:\tools\db\mongodb\4.2\bin in my case. Press the OK Button 3 times to close all the windows. This sets the MongoDB 4 on system environment variables to access the same from the console.

Now again open the console and test the MongoDB version as shown in Fig - MongoDB Version.

MongoDB Version

Fig - MongoDB Version

It also confirms that MongoDB is successfully installed on the system.

 

Setup Admin User

Open the command prompt and type mongo to connect the MongoDB server running as a system service on the default port i.e. 27017. It will launch the MongoDB shell as shown in Fig 15.

# Launch MongoDB Shell
mongo
MongoDB Shell

Fig 15

It shows the warning message indicating that the RBAC is not enabled. MongoDB provides options to create the Authentication Database to store user details and privileges across different databases. The same user having appropriate permissions can act on multiple databases using the Role-Based Access Control (RBAC). The RBAC provided by MongoDB governs access to a MongoDB system.

Now list the databases as shown below.

# List Databases
show databases
# Output admin 0.000GB config 0.000GB local 0.000GB

We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.

# Switch to admin database
use admin
# Create the admin user with root role db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )
# Output Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

This is how we can add admin users to the admin database and assign a role to the user.

 

Enable Authorization in MongoDB

In this step, we will enable the authorization in MongoDB. Optionally it is required to connect MongoDB from programming interfaces. One such example is to connect MongoDB from Laravel Application. In such cases, we must enable the authorization, else an Authorization Failed exception will be thrown.

Update the MongoDB configuration file mongod.cfg located within the bin directory of MongoDB installation as shown below.

#security:
security:
  authorization: enabled

After enabling the authorization, we can connect to MongoDB via shell mongo.exe by executing it with options as shown below. We have to specify the authentication database to allow database users to log in via shell.

## Mongo Shell
mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"

 

Getting Started With MongoDB

In this step, we will connect to the MongoDB Community Server using the MongoDB Compass using the admin user created by us in the previous step. Launch MongoDB Compass using the desktop icon created in the previous steps. It will show the welcome screen as shown in Fig 16.

MongoDB Compass Welcome

Fig 16

Now configure the connection to connect with MongoDB as shown in Fig 17.

MongoDB Compass Connection

Fig 17

After configuring the connection, scroll down and click on the Connect Button. It will connect to the MongoDB server and shows the default databases as shown in Fig 18.

MongoDB Compass Connected

Fig 18

Now start creating your own database using the Create Database Button.

This is how we can install the latest MongoDB i.e. MongoDB 4 with MongoDB Compass. We have also created our first admin user assigned the pre-defined root role. In the last section, we have created a connection using MongoDB Compass to connect with the database server. Now we can access the same database from both MongoDB Shell and MongoDB Compass.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan