Set up Mongodb as a daemon / service on a Mac –

Launch MongoDB on Startup or User Login

A quick post on setting up Mongodb to start automatically on a Mac so I can do my other work. To start, I found a link to Apple’s info about Mac daemons and services.

About Daemons and Services

Which lead me to the section that specifically talks about Launching Custom Daemons Using launchd.

Launching Custom Daemons Using launchd

Which talks about system daemons.

It uses the plist files in the following folders /System/Library/LaunchDaemons/ and /Library/LaunchDaemons/ to register daemons. It attempts to launch the daemons in a somewhat random order, if a daemon has a dependency, its launch is deferred momentarily while other daemons are launched.

MONGODB as a daemon / service for mac

Create a link to the MONGODB plist file

(s = symbolic, f = replace existing, n = link is seen as a normal file)

To start using the current users LaunchAgents

ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

use launchctl to direct launchd to load mongodb .

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

To start as a system wide daemon use the following

ln -sfv /usr/local/opt/mongodb/*.plist /Library/LaunchDaemons/

use launchctl to direct launchd to load mongodb .

launchctl load /Library/LaunchDaemons/homebrew.mxcl.mongodb.plist

homebrew.mxcl.mongodb.plist

This file tells launchd how to launch the application, where the config files and log files should be and should look something like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.mongodb</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/mongodb/bin/mongod</string>
<string>--config</string>
<string>/usr/local/etc/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>1024</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>1024</integer>
</dict>
</dict>
</plist>

The config location is referenced in above plist file (--config). Currently, the default points to /usr/local/etc/mongod.conf. Set the dbPath to the path to the db for your implementation.

mongod.conf
systemLog:
destination: filexvc
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /dbData/db
net:
bindIp: 127.0.0.1

Leave a Reply

Your email address will not be published. Required fields are marked *