MongoDB can be launched using a configuration file instead of passing command-line arguments. This approach provides better maintainability and makes it easier to modify startup parameters.
Linux Configuration
First, create the necessary directories and files:
sudo mkdir -p /var/lib/mongodb
sudo mkdir -p /var/lib/mongodb/log
sudo touch /var/lib/mongodb/log/mongodb.log
sudo touch /etc/mongod.conf
These commands set up the database directory, log directory, and the configuration file.
Configuration Parameters
The following table explains common MongoDB startup options:
| Parameter | Description |
|---|---|
| --dbpath | Path to the directory storing database files |
| --logpath | Path to the log file |
| --logappend | Append new entries to the existing log file |
| --port | Network port number (default: 27017) |
| --fork | Run the process in the background as a daemon |
| --master | Designate this instance as primary in a replica set |
| --slave | Designate this instance as secondary |
| --source | IP address of the primary master instance |
| --auth | Anable authentication requiring username/password |
| --slavedelay | Delay in seconds between replication sync operatoins |
For a complete parameter reference, consult the official MongoDB documentation.
Configuration File Format
Edit the configuration file and add the following settings:
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
path: /var/lib/mongodb/log/mongodb.log
logAppend: true
net:
port: 27017
processManagement:
fork: true
Alternatively, for older MongoDB versions using the key-value format:
dbpath=/var/lib/mongodb
logpath=/var/lib/mongodb/log/mongodb.log
logappend=true
port=27017
fork=true
Launching MongoDB
Start the mongod process using the configuration file:
./mongod -f /etc/mongod.conf
Upon successful startup, the output displays:
about to fork child process, waiting until server is ready for connections.
forked process: 9392
child process started successfully, parent exiting
Windows Configuration
On Windows systems, MongoDB runs as a Windows Service, which provides automatic startup and better system integration.
Create the required directories first, then register the service with the following command:
mongod --dbpath "D:\mongodb\data\db" --logpath "D:\mongodb\data\log\mongodb.log" --logappend --install --serviceName "MongoDB"
After registration, start the service:
net start MongoDB
Common Issues and Solutions
Error: BadValue - dbPath requires an absolute file path
This error occurs when the path format is incorrect. Ensure that:
- The path uses absolute format (not relative paths)
- Drive letters include the colon and backslash (D:)
- Quotes are properly placed around paths containing spaces
Error: The service name is invalid
If the service fails to start after registration, try running Command Prompt as Administrator before executing the registration command. Right-click on CMD and select "Run as administrator", then re-run the service registration command.