Running MongoDB

On some platforms—such as Ubuntu—the package manager will automatically start the mongod daemon for you, and ensure it starts on boot also. On others, such as Mac OS X, you must write your own script to start it, and manually integrate with launchd so that it starts on system boot.

Note that before you can start MongoDB, its data and log directories must exist.

If you wish to have MongoDB start automatically on boot on Windows, 10gen have a document describing how to set this up at http://www.mongodb.org/display/DOCS/Windows+Service

To have MongoDB start automatically on boot under Mac OS X, first you will need a plist file. Save the following (changing db and log paths appropriately) to /Library/LaunchDaemons/org.mongodb.mongod.plist:

<?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>RunAtLoad</key>
        <true/>
        <key>Label</key>
        <string>org.mongo.mongod</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/mongodb/bin/mongod</string>
                <string>--dbpath</string>
                <string>/usr/local/mongodb/data/</string>
                <string>--logpath</string>
                <string>/usr/local/mongodb/log/mongodb.log</string>
        </array>
</dict>
</plist>

Next run the following commands to activate the startup script with launchd:

sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
sudo launchctl start org.mongodb.mongod

A quick way to test whether there is a MongoDB instance already running on your local machine is to type mongo at the command-line. This will start the MongoDB admin console, which attempts to connect to a database server running on the default port (27017).

In any case, you can always start MongoDB manually from the command-line. This is a useful thing to be familiar with in case you ever want to test features such as replica sets or sharding by running multiple mongod instances on your local machine.

Assuming the mongod binary is in your $PATH, run:

mongod --logpath <path/to/mongo.logfile> --port <port to listen on> --dbpath <path/to/data directory>

Get MongoDB and Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.