Starting and Stopping Mongo

MongoDB is run as a standard program from the command line. Please see Command Line Parameters for more information on those options.

The following examples assume that you are in the directory where the Mongo executable is, and the Mongo executable is called mongod.

Starting Mongo

Default Data Directory, Default Port

To start Mongo in default mode, where data will be stored in the /data/db directory (or c:\data\db on Windows), and listening on port 27017, just type

$ ./mongod
Alternate Data Directory, Default Port

To specify a directory for Mongo to store files, use the --dbpath option:

$ ./mongod --dbpath /var/lib/mongodb/

Note that you must create the directory and set its permissions appropriately ahead of time -- Mongo will not create the directory if it doesn't exist. 

Alternate Port

You can specify a different port for Mongo to listen on for connections from clients using the --port option

$ ./mongod --port 12345

This is useful if you want to run more than one instance of Mongo on a machine (e.g., for running a master-slave pair).

Running as a Daemon

Note: these options are only available in MongoDB version 1.1 and later. 

This will fork the Mongo server and redirect its output to a logfile.  As with --dbpath, you must create the log path yourself, Mongo will not create parent directories for you.

 $ ./mongod --fork --logpath /var/log/mongodb.log --logappend

Stopping Mongo

Control-C

If you have Mongo running in the foreground in a terminal, you can simply "Ctrl-C" the process. This will cause Mongo to do a clean exit, flushing and closing it's data files. Note that it will wait until all ongoing operations are complete.

Sending shutdownServer() message from the mongo shell

The shell can request that the server terminate.

$ ./mongo
> db.shutdownServer()

This command only works from localhost, or, if one is authenticated. 

From a driver (where the helper function may not exist), one can run the command

{ "shutdown" : 1 }
Sending a Unix INT or TERM signal

You can cleanly stop mongod using a SIGINT or SIGTERM signal on Unix-like systems. Either ^C, "kill -2 PID," or kill -15 PID will work.

Sending a KILL signal kill -9 will probably cause damage as mongod will not be able to cleanly exit.  (In such a scenario, run the repairDatabase command.)

After a unclean shutdown, MongoDB will say it was not shutdown cleanly, and ask you to do a repair. This is absolutely not the same as corruption, this is MongoDB saying it can't 100% verify what's going on, and to be paranoid, run a repair.

Memory Usage

Mongo uses memory mapped files to access data, which results in large numbers being displayed in tools like top for the mongod process. This is not a concern, and is normal when using memory-mapped files. Basically, the size of mapped data is shown in the virtual size parameter, and resident bytes shows how much data is being cached in RAM.

You can get a feel for the "inherent" memory footprint of Mongo by starting it fresh, with no connections, with an empty /data/db directory and looking at the resident bytes.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus