mongod.service exits with an error on Ubuntu and the cause is hidden
Data directory ... not foundShort answer
When mongod.service exits straight away, the reason is in the mongod log, not in systemctl. Read the log path from /etc/mongod.conf and look at its last lines. Two causes cover most cases: the data directory is missing or not writable by the mongodb user, or another process already holds port 27017.
Fix it fast
1Find out which cause you have
systemctl status gives little detail. Find where mongod writes its log in the config file, then read the end of that file.
grep -A3 systemLog /etc/mongod.conf # then read the file named after "path:", for example: sudo tail -n 40 /var/log/mongodb/mongod.log2Match what you see, then jump to its fix
What does the error look like?
Data directory ... not foundAttempted to create lock file on a read-only directory: ...Socket is already in useFailed to unlink socket file
Cause 1 of 2
The data directory is missing or the mongodb user cannot write to it
1Check
For an apt install, /etc/mongod.conf sets storage.dbPath, by default /var/lib/mongodb. That directory must exist and give the mongodb user and group read, write and execute access.
grep dbPath /etc/mongod.conf
ls -ld /var/lib/mongodb2Fix
Create the directory if it is missing, then give it to the mongodb user and group. If you moved the data to another path, use that path in all three commands.
sudo mkdir -p /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chmod -R u+rwx /var/lib/mongodb3Verify
Start the service and confirm it stays active.
sudo systemctl start mongod
systemctl status mongodConfirmed by mongodb.com
Cause 2 of 2
Another process already uses the MongoDB port
1Check
Usually a second mongod, for example one started by hand earlier, holds port 27017. List listening sockets and their owners.
sudo ss -tulpn | grep 270172Fix
Stop the other process, or give one of the two a different port with net.port in /etc/mongod.conf (or --port on the command line).
# /etc/mongod.conf
net:
port: 270183Verify
Restart the service and check that mongod listens on the port you chose.
sudo systemctl restart mongod
sudo ss -tulpn | grep mongodConfirmed by mongodb.com
How do I stop it coming back?
- Run mongod only through systemctl, so there is never a hand-started second instance.
- When you move storage.dbPath, create the new directory and set its owner to mongodb before restarting.
- Read the mongod log first after any failed start.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
systemctl cannot find mongod.service on Ubuntu
Unable to locate package mongodb-org
The mongod.service unit comes from MongoDB's own mongodb-org package.
2 causes, each with a fixServers
mongod dies at start with a core dump because the CPU is too old
mongod crashes at start on a CPU without the avx flag
MongoDB 5.0 and later needs the AVX instruction set on x86_64, and the ARMv8.2-A microarchitecture on arm64.
2 causes, each with a fixServers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fix