Skip to main content
MongoDB
CHAPTER 02 Beginner

Install MongoDB & MongoDB Compass | Setup Guide (Windows, Mac, Linux)

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. 1. Download: Go to the official MongoDB Download Center (mongodb.com/try/download/community) and download the Windows .msi installer.
  1. 2. Install: Run the installer and choose "Complete" setup.
  1. 3. Service: When prompted, choose "Install MongoDB as a Service". This ensures the database starts automatically when you turn on your computer.
  1. 4. Compass: The installer will usually ask if you want to install MongoDB Compass. Leave this checked.
  1. 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
12345678
# Tap the official MongoDB Homebrew repository
brew tap mongodb/brew

# Install the MongoDB Community Server
brew install mongodb-community@7.0

# Start the MongoDB service to run in the background
brew services start mongodb-community@7.0

*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
12345678910111213
# Import the public key
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

# Create the list file
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

# Install MongoDB
sudo apt-get update
sudo apt-get install -y mongodb-org

# Start the service
sudo systemctl start mongod
sudo systemctl enable mongod

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. 1. Open the MongoDB Compass application.
  1. 2. You will see a "New Connection" screen.
  1. 3. In the URI text box, it will likely pre-fill mongodb://localhost:27017.
  1. 4. Click the green Connect button.
  1. 5. You are in! On the left sidebar, you will see default system databases like admin, config, and local. 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. 1. Open your terminal (or Command Prompt).
  1. 2. Type mongosh and hit Enter.
  1. 3. You are now connected to the database!
  1. 4. Type show dbs to see a list of your databases.
  1. 5. Type exit to 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 run brew services start mongodb-community on 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. 1. Open MongoDB Compass and successfully connect to mongodb://localhost:27017.
  1. 2. Open your terminal, type mongosh, and execute the show dbs command.

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 ECONNREFUSED error when trying to connect to mongodb://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 the mongod 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.

17. Next Chapter Recommendation

Before we start inserting data, we must understand the core terminology of NoSQL. In Chapter 3: Understanding Documents and Collections, we will map your old SQL knowledge (Tables and Rows) into the modern MongoDB equivalents.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·