DEV Community

FOLASAYO SAMUEL OLAYEMI
FOLASAYO SAMUEL OLAYEMI

Posted on

How to Install MongoDB Locally on a MacBook

MongoDB is a powerful, flexible NoSQL database that is widely used for modern web applications. Installing MongoDB locally on your MacBook is straightforward, especially when using the Homebrew package manager.

This article provides a comprehensive guide to installing MongoDB, starting and stopping the service, and verifying the installation.

Step 1: Install Homebrew

Homebrew is a popular package manager for macOS that simplifies the installation of software. If you don’t have Homebrew installed, you can install it by running the following command in your Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

This command downloads and executes the Homebrew installation script.

Step 2: Update Homebrew and Tap the MongoDB Formula

MongoDB is included in the official Homebrew formulae, but it’s a good practice to ensure that your Homebrew is up-to-date. Additionally, you need to tap the MongoDB repository to get the latest versions:

brew update
brew tap mongodb/brew
Enter fullscreen mode Exit fullscreen mode

These commands update Homebrew to the latest version and add the MongoDB formulae to your Homebrew package list.

Step 3: Install MongoDB

With Homebrew updated and the MongoDB formula tapped, you can now install MongoDB. Execute the following command in your Terminal:

brew install mongodb-community
Enter fullscreen mode Exit fullscreen mode

This command installs the latest version of MongoDB on your system.

Step 4: Start MongoDB

After the installation, you can start MongoDB as a background service. Running it as a service ensures that it starts automatically at login and continues running in the background:

brew services start mongodb/brew/mongodb-community
Enter fullscreen mode Exit fullscreen mode

This command starts MongoDB, and it will now be available for use.

Step 5: Verify the Installation

To ensure that MongoDB is installed correctly and running, you can connect to the MongoDB shell. Open the Terminal and type:

mongosh
Enter fullscreen mode Exit fullscreen mode

If MongoDB is running, this command will open the MongoDB shell, allowing you to interact with the database. If you see the MongoDB shell prompt, the installation was successful.

Step 6: Stop MongoDB

If you need to stop MongoDB, perhaps for maintenance or system updates, you can stop the service with the following command:

brew services stop mongodb/brew/mongodb-community
Enter fullscreen mode Exit fullscreen mode

This command stops the MongoDB service from running in the background.

Additional Information

Configuration and Data Directory

  • Configuration File: The default configuration file for MongoDB is located at /usr/local/etc/mongod.conf.
  • Data Files: MongoDB stores its data files in /usr/local/var/mongodb.
  • Log Files: MongoDB log files are stored in /usr/local/var/log/mongodb.

These paths are the default locations, but you can customize them by editing the configuration file.

Uninstalling MongoDB

If you ever need to uninstall MongoDB, you can do so easily with Homebrew:

brew services stop mongodb/brew/mongodb-community
brew uninstall mongodb/brew/mongodb-community
Enter fullscreen mode Exit fullscreen mode

These commands stop the MongoDB service and remove the MongoDB installation from your system.

Summary of Commands

Here’s a summary of the commands you’ll use to install, start, stop, and verify MongoDB on your MacBook Pro:

# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Update Homebrew and tap MongoDB
brew update
brew tap mongodb/brew

# Install MongoDB
brew install mongodb-community

# Start MongoDB as a service
brew services start mongodb/brew/mongodb-community

# Verify installation by opening MongoDB shell
mongosh

# Stop MongoDB service
brew services stop mongodb/brew/mongodb-community
Enter fullscreen mode Exit fullscreen mode

By following these steps, you will have MongoDB installed and running on your MacBook Pro, ready to support your development needs. Whether you are working on a new application or experimenting with MongoDB, having it set up locally provides a convenient and powerful database solution.

Thanks for reading...
Happy Coding!

Top comments (0)