> This page location: /docs.md > /docs/management.md > /docs/manual/administration/self-managed-configuration-and-maintenance.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Configure Self-Managed `mongod` Processes

MongoDB runs as a standard program. You can start MongoDB from a command line by issuing the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) command and specifying options.  For a list of options, see the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) reference.

The following examples assume the directory containing the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) process is in your system paths. The [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) process is the primary database process that runs on an individual server. [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) provides a coherent MongoDB interface equivalent to a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) from the perspective of a client. The [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) binary provides the administrative shell.

This document discusses the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) process; however, some portions of this document may be applicable to [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances.

## Start `mongod` Processes

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

On Windows, this path is on the drive from which you start MongoDB. For example, if you do not specify a [`--dbpath`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--dbpath), starting a MongoDB server on the `C:\` drive stores all data files in `C:\data\db`.

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

```bash
mongod
```

### Specify a Data Directory

If you want [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) to store data files at a path *other
than* `/data/db` you can specify a [`dbPath`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-storage.dbPath). The [`dbPath`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-storage.dbPath) must exist before you start [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod). If it does not exist, create the directory and the permissions so that [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) can read and write data to this path. For more information on permissions, see the [security operations documentation.](https://www.mongodb.com/docs/manual/administration/security-checklist.md)

To specify a [`dbPath`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-storage.dbPath) for [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) to use as a data directory, use the [`--dbpath`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--dbpath) option. The following invocation will start a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance and store data in the `/srv/mongodb` path

```bash
mongod --dbpath /srv/mongodb/
```

**Note:**

[`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) includes a [Full Time Diagnostic Data Capture](https://www.mongodb.com/docs/manual/administration/full-time-diagnostic-data-capture.md#std-label-ftdc-stub) mechanism to assist MongoDB engineers with troubleshooting deployments. If this thread fails, it terminates the originating process. To avoid the most common failures, confirm that the user running the process has permissions to create the FTDC `diagnostic.data` directory. For `mongod` the directory is within [`storage.dbPath`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-storage.dbPath). For `mongos` it is parallel to [`systemLog.path`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-systemLog.path)

### Specify a TCP Port

Only a single process can listen for connections on a network interface at a time. If you run multiple [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.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`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod), use the [`--port`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--port) option on the command line. The following command starts [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) listening on port `12345`:

```bash
mongod --port 12345
```

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

### Start `mongod` as a Daemon

To run a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) process as a daemon (i.e. [`fork`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-processManagement.fork)), *and* write its output to a log file, use the [`--fork`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--fork) and [`--logpath`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath) options. You must create the log directory; however, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) creates the log file if it does not exist.

The following command starts [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) as a daemon and records log output to `/var/log/mongodb/mongod.log`.

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

### Additional Configuration Options

For an overview of common configurations and deployments for common use cases, see [Run-time Database Configuration for Self-Managed Deployments.](https://www.mongodb.com/docs/manual/administration/configuration.md)

## Stop `mongod` Processes

In a clean shutdown a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) completes all pending operations, flushes all data to data files, and closes all data files. Other shutdowns are *unclean* and can compromise the validity of the data files.

To ensure a clean shutdown, always shutdown [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances using one of the following methods:

### Use `shutdownServer()`

Shut down the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) from [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) using the [`db.shutdownServer()`](https://www.mongodb.com/docs/manual/reference/method/db.shutdownserver.md#mongodb-method-db.shutdownServer) method as follows:

```javascript
use admin
db.shutdownServer()
```

Calling the same method from a [init script](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-init-script) accomplishes the same result.

For systems with [`authorization`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-security.authorization) enabled, users may only issue [`db.shutdownServer()`](https://www.mongodb.com/docs/manual/reference/method/db.shutdownserver.md#mongodb-method-db.shutdownServer) when authenticated to the `admin` database or via the localhost interface on systems without authentication enabled.

### Use `--shutdown`

Supported on Linux only. From the command line, shut down the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) using the [`--shutdown`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--shutdown) option:

```bash
mongod --shutdown
```

### Use `CTRL-C`

When running the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance in interactive mode (i.e. without [`--fork`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--fork)), issue `Control-C` to perform a clean shutdown.

### Use `kill`

Supported on Linux and macOS only. From the command line, shut down a specific [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance using one of the following commands:

```none
kill <mongod process ID>
kill -2 <mongod process ID>
```

#### `SIGTERM` and Replica Sets

If a replica set primary receives a `SIGTERM`, the primary attempts to step down before shutting down.

- If the step down succeeds, the instance does not vote in the ensuing election 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.

## Troubleshoot `mongod` Processes

### Generate a Backtrace

For MongoDB instances running on Linux:

- When the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) and [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) processes receive a `SIGUSR2` signal, backtrace details are added to the logs for each process thread.

- Backtrace details show the function calls for the process, which can be used for diagnostics and provided to MongoDB Support if required.

The backtrace functionality is available for these architectures:

- `x86_64`

- `arm64` (starting in MongoDB 5.0.10, and 6.0)

**Important:**

Issue the SIGUSR2 signal only to a non-responsive mongod process. Issuing this signal to a healthy mongod process can cause the process to become deadlocked and require a restart.

To issue a `SIGUSR2` signal to a running [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) process, use the following command:

```none
kill -SIGUSR2 <mongod process ID>
```

The resulting backtrace data is written to the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) logfile as configured with [`--logpath`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

## Stop a Replica Set

### Procedure

If the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) is the [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary) in a [replica set](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-replica-set), the shutdown process for this [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance has the following steps:

1. Check how up-to-date the [secondaries](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary) are.

2. If no secondary is within 10 seconds of the primary, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) will return a message that it will not shut down. You can pass the [`shutdown`](https://www.mongodb.com/docs/manual/reference/command/shutdown.md#mongodb-dbcommand-dbcmd.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.

### Force Replica Set Shutdown

If there is no up-to-date secondary and you want the primary to shut down, issue the [`shutdown`](https://www.mongodb.com/docs/manual/reference/command/shutdown.md#mongodb-dbcommand-dbcmd.shutdown) command with the `force` argument, as in the following [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) operation:

```javascript
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`](https://www.mongodb.com/docs/manual/reference/command/shutdown.md#mongodb-dbcommand-dbcmd.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`](https://www.mongodb.com/docs/manual/reference/command/shutdown.md#mongodb-dbcommand-dbcmd.shutdown) with `timeoutSecs` set to `5`:

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

Alternately you can use the `timeoutSecs` argument with the [`db.shutdownServer()`](https://www.mongodb.com/docs/manual/reference/method/db.shutdownserver.md#mongodb-method-db.shutdownServer) method:

```javascript
db.shutdownServer({timeoutSecs : 5})
```
