141

I've installed mongodb and have been able to run it, work with it, do simple DB read / write type stuff. Now I'm trying to set up my Mac to run mongod as a service.

I get "Command not found" in response to:

 init mongod start

In response to:

~: service mongod start
service: This command still works, but it is deprecated. Please use launchctl(8) instead.
service: failed to start the 'mongod' service

And if I try:

~: launchctl start mongod
launchctl start error: No such process

So obviously I'm blundering around a bit. Next step seems to be typing in random characters until something works. The command which does work is: mongod --quiet & I'm not sure, maybe that is the way you're supposed to do it? Maybe I should just take off 'quiet mode' and add > /logs/mongo.log to the end of the command line?

I'm building a development environment on a Mac with the intention of doing the same thing on a linux server. I'm just not sure of the Bash commands. All the other searches I do with trying to pull up the answer give me advice for windows machines.

Perhaps someone knows the linux version of the commands?

Thanks very much

Alex C
  • 16,624
  • 18
  • 66
  • 98
  • I have done on Windows with this link (http://stackoverflow.com/questions/2438055/how-to-run-mongodb-as-windows-service), not sure if same on Mac OS. – Scott Apr 08 '11 at 14:27
  • Thanks Scott, no I'd also seen and tried the stuff from your thread `error command line: unknown option install` – Alex C Apr 08 '11 at 14:32

11 Answers11

152

Edit: you should now use brew services start mongodb, as in Gergo's answer...

When you install/upgrade mongodb, brew will tell you what to do:

To have launchd start mongodb at login:

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

Then to load mongodb now:

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

Or, if you don't want/need launchctl, you can just run:

    mongod

It works perfectly.

Mario Alemi
  • 1,797
  • 1
  • 13
  • 18
  • I hadn't seen that instruction! This works perfectly for me on Mac OSX 10.8.4, I think I installed it with Brew. Jacob's comment in the accepted answer pointed to an unknown file when I tried it. – Matt Fletcher Nov 19 '13 at 23:40
  • 5
    I prefer this solution over the accepted answer. Copying the plist in the answer above make things a little harder when updating mongo; you'll have to remember to update the paths as needed. Using a symlink as advised by homebrew however takes care of this for you. I used this approach, and the aliases from the answer above (replacing org.mongodb.mongod with homebrew.mxcl.mongodb), and things work great – verboze Mar 30 '14 at 23:18
  • 2
    I tried this solution and the database I was using "disappeared"! I believe this is the reason: 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. Just wanted to note this in the event it happens to someone else. To get my database to appear again, I had to unload and remove the symbolic link. – ttemple Sep 21 '15 at 17:07
  • 1
    Note: There's some weirdness with `launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist` inside `tmux` – the Oct 16 '15 at 21:30
  • @ttemple any idea how to undo all these? – Timeless Oct 23 '15 at 12:39
129

With recent builds of mongodb community edition, this is straightforward.

When you install via brew, it tells you what exactly to do. There is no need to create a new launch control file.

$ brew install mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.0.6.yosemite.bottle.tar.gz ### 100.0%
==> Pouring mongodb-3.0.6.yosemite.bottle.tar.gz
==> Caveats
To have launchd start mongodb at login:
  ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
  /usr/local/Cellar/mongodb/3.0.6: 17 files, 159M
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
Brendan W. McAdams
  • 9,379
  • 3
  • 41
  • 31
  • Does `mongod` need to display information through the window server; does it need to be a _launch agent_ or is it enough to make it a _launch daemon_? – Marius Butuc Feb 10 '13 at 19:55
  • 23
    Heads up to OSX users: I installed mongo via homebrew and it included `/usr/local/Cellar/mongodb/2.4.5-x86_64/homebrew.mxcl.mongodb.plist` (and was properly configured for my installation). Just copied `homebrew.mxcl.mongodb.plist` into LaunchAgents and followed the rest of these instructions (substituting `homebrew.mxcl.mongodb` for `org.mongodb.mongod`) and it works great. – Jakob Jingleheimer Jul 12 '13 at 17:37
  • 5
    You'd better scroll down for Mario Alemi's answer ;) – octavian Jan 28 '15 at 21:09
  • /Library/LaunchAgents/homebrew.mxcl.mongodb.plist: No such file or directory – user269867 May 23 '19 at 22:15
  • First of all: MongoDB has it's own official Homebrew Tap you should use to install the community edition. `$ brew tap mongodb/brew` then install using `$ brew install mongodb-community`. As for the services, if you run `$ brew services ` Homebrew now has that Tap included, so no need for doing anything. Also, about the startup services, if you just need MongoDB after you log in (as a dev server not a production server), use the `~/Library/LaunchAgents/` directory and not the system `/Library/LaunchDeamons` the latter requires root privileges . – C. Sederqvist Jun 10 '20 at 04:31
75

Homebrew's services tap integrates formulas with the launchctl manager. Adding it is easy:

brew tap homebrew/services

You can then launch MongoDB with this command (this will also start mongodb on boot):

brew services start mongodb

You can also use stop or restart:

brew services stop mongodb
brew services restart mongodb
swateek
  • 6,735
  • 8
  • 34
  • 48
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • 4
    Liked the answer but found that brew will remove services in the future. brew services start mongodb Warning: brew services is unsupported and will be removed soon. – smile2day Aug 12 '14 at 10:13
  • 1
    That's sad news. For now it's just deprecated, which means it will be removed sometime, but it still works. Hopefully someone will volunteer to maintain it as a tap. I will update my answer when it gets removed, or when a tap becomes available. – Gergo Erdosi Aug 17 '14 at 11:26
  • 1
    https://robots.thoughtbot.com/starting-and-stopping-background-services-with-homebrew – Igor Shubovych Apr 17 '15 at 20:41
  • It has been removed already, since I see `Error: Unknown command: services` when I tried this way. – iplus26 Sep 17 '15 at 06:59
  • Thanks, updated my answer with the new instructions. – Gergo Erdosi Sep 28 '15 at 18:02
  • 1
    Thanks for this answer. @iplus26 it still works for me – Phil May 30 '17 at 16:50
  • You don't have to use `brew tap homebrew/services` any longer and haven't for quite some time. Think you'll get a deprecation message if you do that now. This tap is called automatically when you list `brew services` the first time. – C. Sederqvist Jun 10 '20 at 04:20
14

If you feel like having a simple gui to fix this (as I do), then I can recommend the mongodb pref-pane. Description: https://www.mongodb.com/blog/post/macosx-preferences-pane-for-mongodb

On github: https://github.com/remysaissy/mongodb-macosx-prefspane

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
koffster
  • 418
  • 4
  • 7
3

Just installed MongoDB via Homebrew. At the end of the installation console, you can see an output as follows:

To start mongodb:

brew services start mongodb

Or, if you don't want/need a background service you can just run:

mongod --config /usr/local/etc/mongod.conf

So, brew services start mongodb, managed to run MongoDB as a service for me.

Ardi Bello
  • 61
  • 5
2

I did a bit of looking around on the Mac side. You may want to use the installer here as it looks like it does all the setup for you to automatically launch on Mac OS. The only downside is it looks like it's using a pretty old mongo version.

This link here also explains the setup to get mongo automatically launching as a background service on the Mac.

Scott
  • 16,711
  • 14
  • 75
  • 120
0

First Step

install mongodb in your linux machine with

apt install mongodb-client && apt install mongodb-server

second step is

change the database path instead of your system default path if you want.
so do the following steps and change it for yourself.

mongod --directoryperdb --dbpath /var/lib/mongodb/data/db --logpath /var/lib/mongodb/log/mongodb.log --logappend --rest

and in your windows machine do it just like that just put an --install flag. you have to get a successful message.

Best Regards...

Community
  • 1
  • 1
MehdiPRG
  • 56
  • 2
0

On macOS 10.13.6 with MongoDB 4.0

I was unable to connect to localhost from the mongo shell

I started MongoDB with:

mongod --config /usr/local/etc/mongod.conf

I found that the 'mongod.conf' had:

bindIp: 127.0.0.1

Change my JavaScript connection from localhost to 127.0.0.1 and it worked fine.

The same was occurring with MongoDB Compass too.

NOTiFY
  • 1,255
  • 1
  • 20
  • 36
0

mongod wasn't working to start the daemon for me but after I ran the following, it started working:

'mongod --fork --logpath /var/log/mongodb.log'

(from here: https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/)

Ali
  • 2,702
  • 3
  • 32
  • 54
briami
  • 1
  • 1
-1
mongod --dbpath [path_to_data_directory]
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Rejeev Divakaran
  • 4,384
  • 7
  • 36
  • 36
-1

After installing mongodb through brew, run this to get it up and running:

mongod --dbpath /usr/local/var/mongodb
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
user129916
  • 46
  • 3
  • 2
    This is the same answer @Rejeev-Divakaran provided over five years ago. Please be sure to check existing answers before submitting a new one. If one exists that suggests your same approach, the appropriate action is to upvote it—a privilege you’ll earn after receiving four more reputation points, I believe. – Jeremy Caney Jun 07 '20 at 01:20