DEV Community

Cover image for Resolving MongoDB Error When Starting with Homebrew on macOS
Hamidreza Mahdavipanah
Hamidreza Mahdavipanah

Posted on • Originally published at hamidreza.tech on

Resolving MongoDB Error When Starting with Homebrew on macOS

This article is also available on my blog: https://hamidreza.tech/mongodb-error-with-homebrew-on-macos

Introduction

While installing MongoDB Community Edition on my MacBook using Homebrew (official MongoDB doc), I encountered an issue: the mongodb-community service seemed to start successfully but was listed as error when I checked its status. If you're facing the same problem, here's a quick guide to help you fix it.

The Issue

After running:

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

I received a confirmation message. However, checking the service status with:

brew services list
Enter fullscreen mode Exit fullscreen mode

showed:

Name              Status  User Plist
mongodb-community error
Enter fullscreen mode Exit fullscreen mode

The MongoDB log (/usr/local/var/log/mongodb/mongo.log) contained warnings:

  • Running as root user: "You are running this process as the root user, which is not recommended"

  • Termination signal received: "Received signal","attr":{"signal":15,"error":"Terminated: 15"}}

These indicated that MongoDB was running as the root user, which can cause permission issues and lead to the service being stopped.

The Solution

The main issue was incorrect ownership of the MongoDB data and log directories. Here's how to fix it:

Steps:

  1. Stop the MongoDB Service:

    brew services stop mongodb-community
    
  2. Terminate Any Running MongoDB Processes:

    pkill -f mongod
    
  3. Change Ownership of MongoDB Directories:

    sudo chown -R $(whoami) /usr/local/var/mongodb
    sudo chown -R $(whoami) /usr/local/var/log/mongodb
    
  4. Verify Directory Ownership:

    ls -ld /usr/local/var/mongodb
    ls -ld /usr/local/var/log/mongodb
    

    Ensure your username is listed as the owner.

  5. Start the MongoDB Service as Your User:

    brew services start mongodb-community
    
  6. Check the Service Status:

    brew services list
    

    The service should now show as started under your username.

  7. Test the MongoDB Connection:

    mongosh
    

    You should successfully connect to the MongoDB shell.

Conclusion

By correcting the directory permissions and ensuring MongoDB runs under your user account (not as root), you can resolve the error when starting the mongodb-community service with Homebrew on macOS. Remember to avoid using sudo with brew commands to prevent permission conflicts.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DreamFactory generates live REST APIs from database schemas with standardized endpoints for tables, views, and procedures in OpenAPI format. We support on-prem deployment with firewall security and include RBAC for secure, granular security controls.

See more!

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay