How to Install MongoDB Locally on a MacBook
Tags: webdev, javascript, mongodb, programming
Published: 2024-05-18 11:03:04

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:

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

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:

```bash
brew update
brew tap mongodb/brew
```

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:

```bash
brew install mongodb-community
```

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:

```bash
brew services start mongodb/brew/mongodb-community
```

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:

```bash
mongosh
```

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:

```bash
brew services stop mongodb/brew/mongodb-community
```

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:

```bash
brew services stop mongodb/brew/mongodb-community
brew uninstall mongodb/brew/mongodb-community
```

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:

```bash
# 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
```

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!**