CHAPTER 02
Beginner
Installing MongoDB and MongoDB Compass
Updated: May 16, 2026
15 min read
# CHAPTER 2
Installing MongoDB and MongoDB Compass
1. Introduction
To start building modern NoSQL applications, you need the right tools. First, you need the MongoDB Community Server—the invisible background engine that actively stores and manages your data. Second, you need MongoDB Compass—the official graphical user interface (GUI) that allows you to click, view, and query your documents visually, rather than staring at a black terminal screen. In this chapter, we will install both and configure our local environment.2. Learning Objectives
By the end of this chapter, you will be able to:- Download and install the MongoDB Community Server.
- Download and install MongoDB Compass.
- Understand the default MongoDB port and connection string.
- Connect Compass to your local localhost server.
- Open the built-in Mongo Shell (mongosh).
3. Windows Setup
MongoDB provides a unified MSI installer for Windows.-
1.
Download: Go to the official MongoDB Download Center (
mongodb.com/try/download/community) and download the Windows.msiinstaller.
- 2. Install: Run the installer and choose "Complete" setup.
- 3. Service: When prompted, choose "Install MongoDB as a Service". This ensures the database starts automatically when you turn on your computer.
- 4. Compass: The installer will usually ask if you want to install MongoDB Compass. Leave this checked.
- 5. Finish: Complete the installation.
4. macOS Setup
The cleanest way to install MongoDB on macOS is using the Homebrew package manager in the terminal.
bash
*Note: You will need to download MongoDB Compass separately from the official website.*
5. Linux Setup (Ubuntu/Debian)
Installing MongoDB on Linux requires adding the official GPG key and repository.
bash
6. The MongoDB Connection String
Just like a website has a URL (http://...), databases have connection strings (URIs).
The default connection string for a local MongoDB server is:
mongodb://localhost:27017
-
mongodb://: The protocol.
-
localhost: Your local machine.
-
27017: The default port that MongoDB actively listens on.
7. Connecting with MongoDB Compass
Now that the server is running, let's look at it visually.- 1. Open the MongoDB Compass application.
- 2. You will see a "New Connection" screen.
-
3.
In the URI text box, it will likely pre-fill
mongodb://localhost:27017.
- 4. Click the green Connect button.
-
5.
You are in! On the left sidebar, you will see default system databases like
admin,config, andlocal. Do not delete these.
8. The MongoDB Shell (mongosh)
While Compass is fantastic for visualizing data, developers often use the command line for fast prototyping and automation. The modern terminal tool for MongoDB is called mongosh (Mongo Shell).
Using the Shell:
- 1. Open your terminal (or Command Prompt).
-
2.
Type
mongoshand hit Enter.
- 3. You are now connected to the database!
-
4.
Type
show dbsto see a list of your databases.
-
5.
Type
exitto leave the shell.
9. Common Mistakes
-
Server Not Running: The most common error beginners face in Compass is
ECONNREFUSED. This means your MongoDB Server is turned off! You must ensure the Windows Service is running, or runbrew services start mongodb-communityon Mac.
- Port Conflicts: If another software is already using port 27017, MongoDB will fail to start.
10. Best Practices
- Secure Your Local DB (Later): By default, local MongoDB installations do not have a username or password. This is fine for learning on your laptop, but you MUST set up Authentication before putting a database on the public internet (We cover this in Chapter 22).
11. Exercises
-
1.
Open MongoDB Compass and successfully connect to
mongodb://localhost:27017.
-
2.
Open your terminal, type
mongosh, and execute theshow dbscommand.
12. MongoDB Challenges
Inside MongoDB Compass, click the> icon at the very bottom left of the screen. This opens an embedded mongosh terminal right inside the GUI! Type db.version() to see exactly what version of MongoDB you installed.
13. MCQ Quiz with Answers
Question 1
What is the default port number that the MongoDB server listens on?
Question 2
Which tool provides a Graphical User Interface (GUI) allowing you to visually explore databases, collections, and documents without using the command line?
14. Interview Questions
-
Q: If a junior developer reports they are getting an
ECONNREFUSEDerror when trying to connect tomongodb://localhost:27017, what is the most likely architectural cause, and how do they fix it?
-
Q: Explain the difference between the MongoDB Server daemon (
mongod) and the MongoDB Shell (mongosh).
15. FAQs
Q: Do I have to install MongoDB locally? A: No! If your computer is slow, you can use MongoDB Atlas, which is a free cloud-hosted MongoDB server. You can skip the local installation entirely and just connect MongoDB Compass directly to your cloud URI! (We cover Atlas in Chapter 20).16. Summary
Your local development environment is officially online. You have themongod engine running silently in the background on port 27017, and you have MongoDB Compass and mongosh ready to interface with it. You are equipped to start building.