When you try to start MongoDB and encounter errors such as mongod.service: Main process exited, status=1/FAILURE
, it could be due to missing or improperly configured directories for MongoDB’s data and log files. One common issue is the absence of the log directory, which prevents MongoDB from starting properly.
MongoDB writes its logs to a specific directory (usually /var/log/mongodb
) as specified in its configuration file (/etc/mongod.conf
). If this directory doesn’t exist, MongoDB will fail to start because it cannot write the log file, which is essential for tracking its operations.
Check the configuration file for syntax errors or incorrect settings.
sudo nano /etc/mongod.conf
Example of a mongod.conf
configuration file.
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
Reproducing the Issue
sudo systemctl start mongod
sudo systemctl status mongod
Output
× mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Wed 2025-01-15 01:09:37 CET; 12s ago
Duration: 24ms
Docs: https://docs.mongodb.org/manual
Process: 3818 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=1/FAILURE)
Main PID: 3818 (code=exited, status=1/FAILURE)
CPU: 20ms
Jan 15 01:09:37 developer-VirtualBox systemd[1]: Started mongod.service - MongoDB Database Server.
Jan 15 01:09:37 developer-VirtualBox mongod[3818]: {"t":{"$date":"2025-01-15T00:09:37.990Z"},"s":"I", "c":"CONTROL", "id":7484500, "ctx":">
Jan 15 01:09:37 developer-VirtualBox mongod[3818]: {"t":{"$date":"2025-01-15T00:09:37.991Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":">
Jan 15 01:09:37 developer-VirtualBox systemd[1]: mongod.service: Main process exited, code=exited, status=1/FAILURE
Jan 15 01:09:37 developer-VirtualBox systemd[1]: mongod.service: Failed with result 'exit-code'.
Solution
To resolve this issue, you can manually create the missing log directory and set the appropriate permissions.
sudo mkdir -p /var/log/mongodb
sudo chown mongodb:mongodb /var/log/mongodb
sudo systemctl start mongod
sudo systemctl status mongod
MongoDB requires a specific directory (/var/log/mongodb
) to store its log files. If this directory doesn’t exist, or if it doesn’t have the correct permissions, MongoDB cannot write its logs, which can cause it to fail to start.