LinuxCommandLibrary

mongod

Run the MongoDB database server

TLDR

Specify the storage directory (default: /data/db on Linux and macOS, C:\data\db on Windows)

$ mongod --dbpath [path/to/directory]
copy

Specify a configuration file
$ mongod --config [path/to/file]
copy

Specify the port to listen on (default: 27017)
$ mongod --port [port]
copy

Specify the database profiling level. 0 is off, 1 is only slow operations, 2 is all (default: 0)
$ mongod --profile [0|1|2]
copy

SYNOPSIS

mongod [options]

PARAMETERS

--dbpath <path>
    Specifies the directory where mongod stores its data files. This is a critical option for defining where your database resides.

--port <num>
    Specifies the port number on which mongod listens for client connections. The default MongoDB port is 27017.

--bind_ip <address>
    Specifies the IP address(es) that mongod binds to and listens for connections. Use 0.0.0.0 to bind to all available network interfaces. By default, it binds to localhost (127.0.0.1).

--logpath <file>
    Specifies the absolute path to a file for logging all diagnostic output. Logging is essential for monitoring and troubleshooting.

--fork
    Forks the mongod process to run in the background as a daemon. This is commonly used when starting mongod directly from the command line.

--config <file>
    Specifies a configuration file for mongod options. This is the recommended way to configure mongod in production environments, typically using YAML format.

--auth
    Enables authentication. Clients must authenticate to access the database. Highly recommended for production deployments.

--journal
    Enables journaling to ensure data consistency and durability. Journaling is enabled by default on 64-bit systems and is crucial for recovering from unclean shutdowns.

--nojournal
    Disables journaling. Not recommended for production deployments as it increases the risk of data loss upon unexpected shutdown.

--replSet <name>
    Specifies the name of the replica set to which this mongod instance belongs. This option is necessary for setting up high availability.

--shardsvr
    Configures this mongod instance to function as a shard server in a sharded cluster. This enables horizontal scaling.

--configsvr
    Configures this mongod instance to function as a config server in a sharded cluster. Config servers store the cluster's metadata.

--storageEngine <name>
    Specifies the storage engine for mongod to use, e.g., wiredTiger (default) or mmapv1.

--version
    Displays the mongod version number and exits.

--help
    Displays a help message with all available options and exits.

DESCRIPTION

The mongod command is the primary daemon process for the MongoDB database system. It handles data requests, manages data access, and performs essential background management operations. When you run mongod, it initializes and hosts the MongoDB data directory, making your data available for client applications. Key functionalities include supporting replica sets for high availability, sharding for horizontal scalability, and ensuring data durability through journaling. It manages client connections, authenticates users, and executes various database operations like queries, updates, and aggregations. Users typically run mongod as a background service, often configured via a YAML file, to ensure continuous database operation and provide a robust data storage solution.

CAVEATS

Permissions: Ensure the user running mongod has appropriate read and write permissions to the --dbpath and --logpath directories.

Journaling: Disabling journaling with --nojournal can lead to data loss in the event of an unexpected shutdown. It is strongly discouraged for production systems, where data durability is paramount.

Security: By default, mongod may bind only to localhost (127.0.0.1). For network access, you must explicitly use --bind_ip. Always combine this with --auth and robust network security measures (e.g., firewalls) to prevent unauthorized access.

DEFAULT DATA DIRECTORY

If the --dbpath option is not specified, mongod defaults to storing data in /data/db on Linux and macOS systems. Ensure this directory exists and has the correct permissions for the user running mongod.

CONFIGURATION FILES

MongoDB strongly recommends using a configuration file (typically in YAML format) for running mongod in production environments. This method allows for cleaner management of numerous options and their values, promoting better organization and readability.

RUNNING AS A SERVICE

For production deployments, mongod is almost always run as a background service managed by an init system like systemd (on modern Linux distributions) or SysVinit. This ensures that the database starts automatically on boot and can be easily managed (start, stop, restart) using standard system commands.

HISTORY

The MongoDB project, and consequently the mongod daemon, was initiated in 2007 by 10gen (now MongoDB Inc.) as a component of a planned platform-as-a-service product. When the company pivoted away from the PaaS model, it decided to release MongoDB as an open-source, document-oriented database. The first public release was in 2009. Over the years, mongod has evolved significantly, incorporating key features like the WiredTiger storage engine (introduced in version 3.0), advanced security options, and enhanced scalability capabilities, solidifying its position as a leading NoSQL database.

SEE ALSO

Copied to clipboard