Navigation
This version of the documentation is archived and no longer supported.

Manage mongod Processes

MongoDB runs as a standard program. You can start MongoDB from a command line by issuing the mongod command and specifying options. For a list of options, see mongod. MongoDB can also run as a Windows service. For details, see MongoDB as a Windows Service. To install MongoDB, see Installing MongoDB.

The following examples assume the directory containing the mongod process is in your system paths. The mongod process is the primary database process that runs on an individual server. mongos provides a coherent MongoDB interface equivalent to a mongod from the perspective of a client. The mongo binary provides the administrative shell.

This document page discusses the mongod process; however, some portions of this document may be applicable to mongos instances.

Start mongod

By default, MongoDB stores data in the /data/db directory. On Windows, MongoDB stores data in C:\data\db. On all platforms, MongoDB listens for connections from clients on port 27017.

To start MongoDB using all defaults, issue the following command at the system shell:

mongod

Specify a Data Directory

If you want mongod to store data files at a path other than /data/db you can specify a dbpath. The dbpath must exist before you start mongod. If it does not exist, create the directory and the permissions so that mongod can read and write data to this path. For more information on permissions, see the security operations documentation.

To specify a dbpath for mongod to use as a data directory, use the --dbpath option. The following invocation will start a mongod instance and store data in the /srv/mongodb path

mongod --dbpath /srv/mongodb/

Specify a TCP Port

Only a single process can listen for connections on a network interface at a time. If you run multiple mongod processes on a single machine, or have other processes that must use this port, you must assign each a different port to listen on for client connections.

To specify a port to mongod, use the --port option on the command line. The following command starts mongod listening on port 12345:

mongod --port 12345

Use the default port number when possible, to avoid confusion.

Start mongod as a Daemon

To run a mongod process as a daemon (i.e. fork,) and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.

The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.

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

Additional Configuration Options

For an overview of common configurations and common configuration deployments. configurations for common use cases, see Run-time Database Configuration.

Stop mongod

To stop a mongod instance not running as a daemon, press Control+C. MongoDB stops when all ongoing operations are complete and does a clean exit, flushing and closing all data files.

To stop a mongod instance running in the background or foreground, issue the shutdownServer() helper in the mongo shell. Use the following sequence:

  1. To open the mongo shell for a mongod instance running on the default port of 27017, issue the following command:

    mongo
    
  2. To switch to the admin database and shutdown the mongod instance, issue the following commands:

    use admin
    db.shutdownServer()
    

You may only use db.shutdownServer() when connected to the mongod when authenticated to the admin database or on systems without authentication connected via the localhost interface.

Alternately, you can shut down the mongod instance:

mongod Shutdown and Replica Sets

If the mongod is the primary in a replica set, the shutdown process for these mongod instances has the following steps:

  1. Check how up-to-date the secondaries are.
  2. If no secondary is within 10 seconds of the primary, mongod will return a message that it will not shut down. You can pass the shutdown command a timeoutSecs argument 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, issue the shutdown command with the force argument, as in the following mongo shell operation:

db.adminCommand({shutdown : 1, force : true})

To keep checking the secondaries for a specified number of seconds if none are immediately up-to-date, issue shutdown with the timeoutSecs argument. MongoDB will keep checking the secondaries for the specified number of seconds if none are immediately up-to-date. If any of the secondaries catch up within the allotted time, the primary will shut down. If no secondaries catch up, it will not shut down.

The following command issues shutdown with timeoutSecs set to 5:

db.adminCommand({shutdown : 1, timeoutSecs : 5})

Alternately you can use the timeoutSecs argument with the shutdownServer() method:

db.shutdownServer({timeoutSecs : 5})

Sending a UNIX INT or TERM Signal

You can cleanly stop mongod using a SIGINT or SIGTERM signal on UNIX-like systems. Either ^C for a non-daemon mongod instance, kill -2 <pid>, or kill -15 <pid> will cleanly terminate the mongod instance.

Terminating a mongod instance that is not running with journaling with kill -9 <pid> (i.e. SIGKILL) will probably cause data corruption.

To recover data in situations where mongod instances have not terminated cleanly without journaling see Recover MongoDB Data following Unexpected Shutdown.