Installing MongoDB on OS X is an easy task. However, if you want the service to start each time your computer is restarted, some additional effort is required.
The easiest way to get MongoDB installers is to use Homebrew.
[korey@localhost ~]$ brew install mongodb
At this point MongoDB is installed. To start it manually, first create the location where the DB will be stored (default is /data/db):
[korey@localhost ~]$ mkdir /data/db
[korey@localhost ~]$ mongd
Note that the user running mongod needs to have write access to the DB folder. The downside here is that the DB needs to be started manually each time and it will run as your userid.
In order to automatically start the service, it is necessary to create a LaunchDaemon which will allow the service to start as soon as the computer starts.
-
1. The first step is to create a service account so the service does not run as root.
2. Next, is to create a proper location for the log and database location:
[korey@localhost ~]$ sudo mkdir -p /var/lib/mongodb
[korey@localhost ~]$ sudo mkdir -p /var/log/mongo
[korey@localhost ~]$ sudo chown -R _mongo:_mongo /var/lib/mongodb
[korey@localhost ~]$ sudo chown -R _mongo:_mongo /var/log/mongo
3. Now that we have an account and location, it is time to create the daemon plist file:
Label
org.mongo.mongod
ProgramArguments
/usr/local/bin/mongod
--dbpath
/var/lib/mongodb/
--logpath
/var/log/mongo/mongodb.log
KeepAlive
UserName
_mongo
GroupName
_mongo
Store this file at: /Library/LaunchDaemons and name it: org.mongo.mongod.plist.
Now you can start and stop the service without having to restart your computer by using the following commands:
[korey@localhost ~]$ sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist
[korey@localhost ~]$ sudo launchctl unload /Library/LaunchDaemons/org.mongo.mongod.plist