Manage mongod Processes

MongoDB runs as a standard program. You can start MongoDB from acommand line by issuing the mongod command and specifyingoptions. For a list of options, see the mongodreference. MongoDB can also run as a Windows service. For details, seeStart MongoDB Community Edition as a Windows Service. To install MongoDB, seeInstall MongoDB.

The following examples assume the directory containing themongod process is in your system paths. Themongod process is the primary database process that runs onan individual server. mongos provides a coherent MongoDBinterface equivalent to a mongod from the perspective of aclient. The mongo binary provides the administrativeshell.

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

Start mongod Processes

By default, MongoDB listens for connections from clients on port27017, and stores data in the /data/db directory.

On Windows, this path is on the drive from which you start MongoDB. Forexample, if you do not specify a —dbpath, starting a MongoDBserver on the C:\ drive stores all data files in C:\data\db.

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

  1. mongod

Specify a Data Directory

If you want mongod to store data files at a path otherthan /data/db you can specify a dbPath. ThedbPath must exist before you start mongod. If itdoes not exist, create the directory and the permissions so thatmongod can read and write data to this path. For moreinformation on permissions, see the security operationsdocumentation.

To specify a dbPath for mongod to use as a datadirectory, use the —dbpath option. Thefollowing invocation will start a mongod instance and storedata in the /srv/mongodb path

  1. mongod --dbpath /srv/mongodb/

Specify a TCP Port

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

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

  1. 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 —logpathoptions. You must create the log directory; however, mongodwill create the log file if it does not exist.

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

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

Additional Configuration Options

For an overview of common configurations and deploymentsfor common use cases, seeRun-time Database Configuration.

Stop mongod Processes

In a clean shutdown a mongod completes all pendingoperations, flushes all data to data files, and closes all datafiles. Other shutdowns are unclean and can compromise the validity of thedata files.

To ensure a clean shutdown, always shutdown mongodinstances using one of the following methods:

Use shutdownServer()

Shut down the mongod from the mongo shell usingthe db.shutdownServer() method as follows:

  1. use admin
  2. db.shutdownServer()

Calling the same method from a init script accomplishes the same result.

For systems with authorization enabled, users may only issuedb.shutdownServer() when authenticated to the admindatabase or via the localhost interface on systems withoutauthentication enabled.

Use —shutdown

From the Linux command line, shut down the mongod using the—shutdown option in the following command:

  1. mongod --shutdown

Use CTRL-C

When running the mongod instance in interactive mode(i.e. without —fork), issue Control-Cto perform a clean shutdown.

Use kill

From the Linux command line, shut down a specific mongod instanceusing one of the following commands:

  1. kill <mongod process ID>
  2. kill -2 <mongod process ID>

SIGTERM and Replica Sets

Starting in MongoDB 4.0.8 (and 3.6.15), if a replica set primary receives aSIGTERM, the primary attempts to step down before shutting down.

  • If the step down succeeds, the instance does not vote in the ensuingelection of the new primary, and continues its shutdown.
  • If the step down fails, the instance continues its shutdown.

SIGKILL

Warning

Never use kill -9 (i.e. SIGKILL) to terminate a mongod instance.

Stop a Replica Set

Procedure

If the mongod is the primary in a replicaset, the shutdown process for this mongod instance hasthe following steps:

  • Check how up-to-date the secondaries are.
  • 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 timeoutSecsargument to wait for a secondary to catch up.
  • If there is a secondary within 10 seconds of the primary, the primarywill step down and wait for the secondary to catch up.
  • After 60 seconds or once the secondary has caught up, the primarywill shut down.

Force Replica Set Shutdown

If there is no up-to-date secondary and you want the primary to shutdown, issue the shutdown command with the forceargument, as in the following mongo shell operation:

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

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

The following command issues shutdown with timeoutSecsset to 5:

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

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

  1. db.shutdownServer({timeoutSecs : 5})