Complete Guide to Installing and Running MongoDB on macOS

Nov 27, 2025 · Programming · 10 views · 7.8

Keywords: MongoDB | macOS | Database Installation | Homebrew | Troubleshooting

Abstract: This article provides a comprehensive guide for installing, configuring, and running MongoDB database on macOS systems. It addresses common connection failure issues with solutions based on Homebrew and manual installation methods, covering service startup, data directory configuration, connection testing, and other critical steps. Through specific command-line examples and configuration instructions, it helps developers quickly set up a local MongoDB development environment.

Introduction

MongoDB, as a popular NoSQL database, is widely used in web development. However, developers often encounter various issues when installing and running MongoDB on macOS systems, particularly connection failures after system reboots. Based on practical development experience, this article provides a complete installation and configuration guide.

Installation Method Selection

There are two main approaches to install MongoDB on macOS: using the Homebrew package manager or manually downloading installation packages. The Homebrew method is more convenient, automatically handling dependencies and service management.

Installation via Homebrew

First, add the official MongoDB tap repository:

brew tap mongodb/brew

Then install the MongoDB Community Edition:

brew install mongodb-community

Data Directory Configuration

MongoDB requires a data storage directory. By default, MongoDB uses the /usr/local/var/mongodb directory. If this directory doesn't exist, create it manually:

sudo mkdir -p /usr/local/var/mongodb
sudo chown $(whoami) /usr/local/var/mongodb

Starting MongoDB Service

Use Homebrew service management to start MongoDB:

brew services start mongodb-community

This sets up MongoDB as a background service that automatically runs on system startup.

Verifying Installation

After starting the service, verify the connection using MongoDB shell:

mongo

If the connection is successful, you'll see the MongoDB shell prompt, indicating the database is running properly.

Troubleshooting Common Issues

When encountering connection errors, first check if MongoDB service is running:

brew services list

If the service status shows as stopped, restart it:

brew services restart mongodb-community

Manual Startup Method

Besides service management, you can also start MongoDB process manually:

mongod --config /usr/local/etc/mongod.conf

This approach is suitable for temporary testing or development environments.

Connection Testing

In another terminal window, start MongoDB shell to connect to a specific database:

mongo test

This connects to the "test" database, allowing basic CRUD operation testing.

Service Management Commands

Complete service management commands include:

Configuration Inspection

View MongoDB installation information and configuration options using:

brew info mongodb-community

This displays detailed installation paths, configuration file locations, and startup options.

Advanced Troubleshooting

If connection issues persist, check these common problems:

  1. Confirm MongoDB process is running
  2. Check if port 27017 is occupied
  3. Verify data directory permissions
  4. Examine log files for detailed error information

Best Practices

Recommended practices for development environments:

Conclusion

Through proper installation and configuration procedures, MongoDB database can run stably on macOS systems. Using Homebrew for installation and management simplifies operational workflows and avoids common connection issues. By following the steps and recommendations in this article, developers can quickly establish a reliable local development environment.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.