Running MongoDB as a Persistent Background Daemon on macOS: Best Practices and Script Examples


2 views

When developing applications with MongoDB on macOS, you'll often need the database server to run continuously in the background, similar to how MySQL operates as a system service. This ensures your development environment is always ready without manual intervention.

The simplest approach uses the nohup command to detach MongoDB from your terminal session:

nohup mongod --config /usr/local/etc/mongod.conf > /dev/null 2>&1 &

This command:

  • Uses nohup to prevent termination when the shell exits
  • Redirects output to /dev/null to suppress logs
  • Appends & to run in background

For proper system integration, create a LaunchDaemon configuration:





    Label
    org.mongodb.mongod
    ProgramArguments
    
        /usr/local/bin/mongod
        --config
        /usr/local/etc/mongod.conf
    
    RunAtLoad
    
    KeepAlive
    
    WorkingDirectory
    /usr/local/var/mongodb
    StandardErrorPath
    /usr/local/var/log/mongodb/error.log
    StandardOutPath
    /usr/local/var/log/mongodb/output.log

Save this as /Library/LaunchDaemons/org.mongodb.mongod.plist and load it with:

sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist

If you installed MongoDB via Homebrew, the simplest method is:

brew services start mongodb-community

To ensure it starts at boot:

brew services restart mongodb-community --all

Check if MongoDB is running with:

ps aux | grep mongod
# Or for system services:
brew services list
# Or:
sudo launchctl list | grep mongo

If MongoDB fails to start:

# Check logs
tail -f /usr/local/var/log/mongodb/mongo.log

# Verify data directory permissions
sudo chown -R id -un /usr/local/var/mongodb

# Check port conflicts
lsof -i :27017

For production-like setups, consider these mongod.conf settings:

storage:
  dbPath: /usr/local/var/mongodb
  journal:
    enabled: true
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
net:
  bindIp: 127.0.0.1
  port: 27017
processManagement:
  fork: true
  pidFilePath: /usr/local/var/run/mongodb/mongod.pid

Running MongoDB (mongod) persistently in the background is essential for development environments where you need constant database availability. Unlike Windows services, Unix-like systems (including macOS) require specific approaches to daemonize processes.

The simplest way is to use MongoDB's built-in forking capability:

mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork

Key parameters:

  • --dbpath: Specifies data directory location
  • --logpath: Redirects output to log file
  • --fork: Detaches process to background

For permanent background execution, create a LaunchDaemon plist file at /Library/LaunchDaemons/org.mongodb.mongod.plist:





    Label
    org.mongodb.mongod
    ProgramArguments
    
        /usr/local/bin/mongod
        --config
        /usr/local/etc/mongod.conf
    
    RunAtLoad
    
    KeepAlive
    
    StandardErrorPath
    /usr/local/var/log/mongodb/error.log
    StandardOutPath
    /usr/local/var/log/mongodb/output.log

Create /usr/local/etc/mongod.conf with these minimum settings:

systemLog:
    destination: file
    path: /usr/local/var/log/mongodb/mongo.log
    logAppend: true
storage:
    dbPath: /usr/local/var/mongodb
processManagement:
    fork: true
net:
    bindIp: 127.0.0.1
    port: 27017

After creating the plist file:

# Load the service
sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist

# Check status
launchctl list | grep mongo

# Unload when needed
sudo launchctl unload /Library/LaunchDaemons/org.mongodb.mongod.plist

If you installed MongoDB via Homebrew:

brew services start mongodb-community

This automatically creates and manages the LaunchAgent for you.

For production environments, implement log rotation by adding to your configuration:

systemLog:
    logRotate: reopen
    timeStampFormat: iso8601-utc