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 mongod executable resides. mongod is the primary database process that runs on an individual server. mongos is the sharding process. mongo is the administrative shell. This page discusses mongod.

Starting mongod

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 mongod

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
> use admin
> 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 }

If this server is the primary in a replica set, it will go through the following process (version 1.9.1+):

  1. Check how up-to-date the secondaries are.
  2. If no secondary within 10 seconds of the primary, return that we won't shut down (optionally pass the timeoutSecs option to wait for a secondary to catch up.
  3. If there is a secondary within 10 seconds of the primary, the primary will step down and wait for the secondary to catch up.
  4. After 60 seconds or once the secondary has caught up, the primary will shut down.

If there is no up-to-date secondary and you want the primary to shut down anyway, you can use force : true:

> db.adminCommand({shutdown : 1, force : true})
> // or
> db.shutdownServer({force : true})

You can also specify timeoutSecs : N, which will keep checking the secondaries for N seconds if none are immediately up-to-date. If any of the secondaries catch up within N seconds, the primary will shut down. If no secondaries catch up, it will not shut down.

> db.adminCommand({shutdown : 1, timeoutSecs : 5})
> // or
> db.shutdownServer({timeoutSecs : 5})
> // sample output:
{
        "closest" : NumberLong(1307651781),
        "difference" : NumberLong(1307651808),
        "errmsg" : "no secondaries within 10 seconds of my optime",
        "ok" : 0
}
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 if mongod is not running with the --journal option.  (In such a scenario, run repairDatabase command.)

After a hard crash, when not using --journal, MongoDB will say it was not shutdown cleanly, and ask you to do a repair of the database.

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