Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active March 2, 2022 00:57
Show Gist options
  • Save subfuzion/9630872 to your computer and use it in GitHub Desktop.
Save subfuzion/9630872 to your computer and use it in GitHub Desktop.
mongo auto start on OS X

Install with Homebrew

brew install mongodb

Set up launchctl to auto start mongod

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

/usr/local/opt/mongodb/ is a symlink to /usr/local/Cellar/mongodb/x.y.z (e.g., 2.4.9)

You can use launchctl to start and stop mongod

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

You can also more conveniently use brew to start, stop, and verify service status

$ brew services list | grep mongodb
$ brew services start mongodb
$ brew services stop mongodb

Notes

The default plist provided by homebrew stores the mongod configuration at /usr/local/etc/mongod.conf. This configuration specifies the dbpath to be /usr/local/var/mongodb instead of the default /data/db.

For more about launchctl see:

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html#//apple_ref/doc/man/1/launchctl

http://launchd.info/

@excalq
Copy link

excalq commented Sep 13, 2014

When running launchctrl load... I kept getting nothing found to load

The solution was to use -wF

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

@windmaomao
Copy link

thank you very much, btw, I searched google, and http://launchd.info (and I noticed you also mentioned) is where it explains things very well. Now i don't have to always do sudo mongod whenever my computer restarts.

@aarsilv
Copy link

aarsilv commented Mar 24, 2017

I believe you want a trailing / in ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents so that it links within the directory.

I didn't have that directory so I had to do:

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

@shiv19
Copy link

shiv19 commented Oct 27, 2017

Thanks @subfuzion this was helpful

@harrv
Copy link

harrv commented Mar 7, 2018

Thank you, @subfuzion.

It's interesting to me how many people are content to have their database run only while they are logged in. I don't know what y'all are using mongodb for, but for my purposes a LaunchDaemon is much more useful than a LaunchAgent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment