How to Install and Set Up MongoDB on Windows or Mac

MongoDB is a popular NoSQL database used for web applications, APIs, development projects, testing, and data-driven software. Unlike traditional relational databases that organize information into tables and rows, MongoDB stores data in flexible JSON-like documents.

Installing MongoDB on Windows or Mac is relatively straightforward, but you also need to make sure the database server is running and that you can connect to it successfully. The process is slightly different between Windows and macOS.

This guide explains how to install MongoDB Community Edition, start the MongoDB server, connect to it, and perform a basic test to confirm that everything is working.

Method 1: Download MongoDB Community Edition

The first step is to download the MongoDB Community Edition installer for your operating system.

Open the MongoDB download page and select:

  • MongoDB Community Server
  • Your operating system
  • The appropriate package format
  • The latest stable release available for your system

For Windows, the MSI installer is generally the easiest option.

For macOS, MongoDB can be installed using Homebrew or the available package installation method.

Make sure you download MongoDB from the official MongoDB website rather than an unknown third-party download site.

Before installing, check your operating system architecture and requirements so that you select the appropriate build.

Method 2: Install MongoDB on Windows

The MongoDB MSI installer makes the Windows installation process relatively simple.

After downloading the installer:

  1. Open the downloaded .msi file.
  2. Follow the installation wizard.
  3. Accept the license agreement.
  4. Choose Complete installation unless you have a reason to customize it.
  5. Keep the default installation location unless you need another drive.
  6. Continue with the installation.

During installation, MongoDB may provide options related to running MongoDB as a Windows service.

If the installer offers the option to install MongoDB as a service, keeping this enabled can make MongoDB easier to start automatically with Windows.

After the installation finishes, MongoDB’s executable files will be installed in the selected directory.

The exact installation path can vary depending on the MongoDB version and installation choices.

You can then open Command Prompt or Windows Terminal and test whether the MongoDB command-line tools are available.

Method 3: Install MongoDB on Mac Using Homebrew

Homebrew provides a convenient way to install MongoDB on macOS.

If Homebrew is already installed, open Terminal and update its package information:

brew update

Then add the MongoDB Homebrew tap:

brew tap mongodb/brew

Install the MongoDB Community Edition package:

brew install mongodb-community

Wait for Homebrew to download and install the required files.

After installation, verify the MongoDB package:

brew list | grep mongodb

You can also check the installed MongoDB version using the available MongoDB command-line tools.

If Homebrew is not installed on your Mac, install it from its official source first and then return to the MongoDB installation steps.

Method 4: Start the MongoDB Server

Installing MongoDB does not necessarily mean that the database server is currently running. You need to start the MongoDB service before applications can connect to it.

Windows

If MongoDB was installed as a Windows service, it may start automatically.

You can check Windows services by:

  1. Pressing Windows + R.
  2. Typing:
services.msc
  1. Pressing Enter.
  2. Looking for a MongoDB service.
  3. Checking its status.

If the service is stopped, you can right-click it and select Start.

You can also manage services from an elevated Command Prompt or PowerShell depending on the service configuration.

Mac

If you installed MongoDB through Homebrew, you can start it as a background service with:

brew services start mongodb-community

Check the service status with:

brew services list

Look for the MongoDB entry and verify that it is running.

To stop the service later:

brew services stop mongodb-community

Running MongoDB as a background service is convenient when you regularly develop applications that depend on the database.

Method 5: Connect to MongoDB and Test the Installation

Once MongoDB is running, connect to the local database server to verify that the installation works.

Depending on your MongoDB version and installed tools, the MongoDB shell is generally accessed using:

mongosh

On Windows, open Command Prompt or Windows Terminal and run:

mongosh

On macOS, open Terminal and run:

mongosh

If the shell connects successfully, you should see information about the MongoDB server and a MongoDB shell prompt.

You can test the connection by entering:

show dbs

This displays available databases.

You can also check the server status with:

db.runCommand({ ping: 1 })

A successful response indicates that the MongoDB server is responding.

If mongosh is not recognized, the MongoDB shell may not be installed or its executable directory may not be available through your PATH.

Method 6: Create a Database and Collection

After confirming that MongoDB works, you can create a simple database for testing.

At the mongosh prompt, enter:

use testdb

MongoDB switches to the testdb database.

Create a collection and insert a document:

db.users.insertOne({
  name: "John",
  age: 30
})

MongoDB will return information about the inserted document, including its automatically generated ID.

To view the document:

db.users.find()

You should see the record you just created.

You can insert another document:

db.users.insertOne({
  name: "Sarah",
  age: 28
})

Then run:

db.users.find()

to display both documents.

This simple test confirms that MongoDB can create databases, collections, and documents successfully.

Method 7: Connect MongoDB to a Development Project

Once MongoDB is running locally, applications can connect to it using a MongoDB connection string.

A common local connection string is:

mongodb://localhost:27017

The default MongoDB port is commonly 27017.

For a specific database, a connection string can look like:

mongodb://localhost:27017/testdb

Your application can then use a MongoDB driver appropriate for its programming language.

For example, Node.js projects commonly use the official MongoDB Node.js driver.

The general connection flow is:

  1. Start MongoDB.
  2. Obtain the local connection string.
  3. Add the MongoDB driver to your project.
  4. Connect to the database.
  5. Select or create a database.
  6. Perform database operations.
  7. Close the connection when appropriate.

Keep database credentials and connection strings containing passwords out of publicly shared source code.

For development projects, environment variables are a better place to store sensitive connection information.

Method 8: Troubleshoot Common MongoDB Installation Problems

MongoDB installation problems are often related to the server not running, missing command-line tools, permissions, or incorrect paths.

mongosh is not recognized

If Windows displays an error such as:

'mongosh' is not recognized as an internal or external command

check whether MongoDB Shell is installed.

If it is installed but Windows cannot find it, verify that its executable directory is included in PATH or run it using its full path.

MongoDB cannot connect

If mongosh cannot connect to the server, first make sure MongoDB is running.

On macOS:

brew services list

On Windows, check the MongoDB service through:

services.msc

Port 27017 is unavailable

Another application may already be using the MongoDB port.

You can investigate which application is using the port and either stop the conflicting application or configure MongoDB to use another available port.

MongoDB starts and then stops

Check the MongoDB logs for the specific error. Permission problems, incorrect configuration, storage issues, or an invalid data directory can prevent the server from starting correctly.

Do not randomly delete MongoDB’s database files when troubleshooting. Those files can contain your databases and deleting them can result in permanent data loss.

Conclusion

MongoDB can be installed on both Windows and macOS using straightforward installation methods. Windows users can use the MongoDB installer, while Mac users can conveniently install MongoDB through Homebrew.

After installation, the most important step is confirming that the MongoDB server is running. Use the appropriate service controls, connect with mongosh, and run a simple database command to verify the installation.

Once the server works, you can create databases and collections and connect MongoDB to applications using a local connection string such as mongodb://localhost:27017.

If you encounter problems, first check whether the MongoDB service is running and whether the required command-line tools are available. Avoid deleting database files while troubleshooting unless you have a verified backup and understand exactly what you are removing.

FAQs

1. Is MongoDB free to install on Windows and Mac?

MongoDB Community Edition can be installed for local development and other supported uses. Make sure you select the appropriate MongoDB edition and review its current licensing terms for your intended use.

2. How do I know if MongoDB is running?

On macOS with Homebrew, run:

brew services list

On Windows, open:

services.msc

and look for the MongoDB service. You can also test the server by opening mongosh and running:

db.runCommand({ ping: 1 })

3. What is the default MongoDB port?

MongoDB commonly uses port 27017 for local connections. A typical local connection string is:

mongodb://localhost:27017

4. What should I do if mongosh is not recognized?

Check whether MongoDB Shell is installed. If it is installed, verify that its executable location is available through your system PATH or launch it using its full path. On Windows, opening a new terminal after modifying PATH may also be necessary.

Quick Summary

  • Download MongoDB Community Edition from the official source.
  • Install MongoDB using the Windows installer or Homebrew on Mac.
  • Start the MongoDB service and verify that it is running.
  • Use mongosh to test the server and create a sample database and collection.

Related Articles

Popular Categories