I have to install MongoDB on a Linode server with Ubuntu 10.04. So I decided to install it on my MBP first. It’s a cool little database, and John Nunemaker’s MongoMapper gem is a treat.
Here’s how I got the server installed and running as a daemon in Snow Leopard for local development.
Download, unpack, and install the pre-compiled 32-bit binaries:
curl -O http://downloads.mongodb.org/osx/mongodb-osx-i386-1.4.0.tgz tar xzf mongodb-osx-i386-1.4.0.tgz sudo mv mongodb-osx-i386-1.4.0 /usr/local/mongodb sudo mkdir /usr/local/mongodb_data /var/log/mongodb sudo chown -R root /usr/local/mongodb
(If you’re on a 64-bit machine, substitute in x86_64 for each i386 above.)
Next, you’ll have to make a config file so you can change the server’s options without fiddling with command-line arguments.
Save this content as: /usr/local/mongodb/mongod.conf
# Store data alongside MongoDB instead of the default, /data/db/ dbpath = /usr/local/mongodb_data # Only accept local connections bind_ip = 127.0.0.1
Now, we’ll make a launchd job to register the server as an OS X daemon. launchd will start the server at startup, stop it before shutdown, make sure it stays up, and redirect its output to a nice log file.
Save this content as: /Library/LaunchDaemons/org.mongodb.mongod.plist
Label org.mongodb.mongod ProgramArguments /usr/local/mongodb/bin/mongod run --config /usr/local/mongodb/mongod.conf RunAtLoad KeepAlive WorkingDirectory /usr/local/mongodb StandardErrorPath /var/log/mongodb/output.log StandardOutPath /var/log/mongodb/output.log
Now we just need to load the launchd job:
sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
And that should do it! Try visiting http://localhost:28017 to see the status console for your database.
This is mine:
One last thing: you should probably add /usr/local/mongodb/bin to your $PATH. That way you can use the other binaries that ship with MongoDB, like the mongo console, mongoexport, and so on.
You can adjust your path the regular way by editing your shell’s profile, or you can use this nice paths.d mechanism that OS X provides:
sudo sh -c 'echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb'