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.
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.
Click on the Next Button to continue with the installation. The next screen shows the License Agreement as shown in Fig 2.
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.
I have clicked on the Custom Button to view the available components as part of this installation process as shown in 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.
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.
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.
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.
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
Click on the Finish
Read the License Agreement, scroll down and click on the Agree
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.
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.
Now click on the Yes
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.
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
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.
Now configure the connection to connect with MongoDB as shown in 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.
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.