Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

mongod.service exits with an error on Ubuntu and the cause is hidden

ErrorData directory ... not found

Short 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.

  • Applies to MongoDB 9.0 Community, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.log
  2. 2Match what you see, then jump to its fix

    1. 1Data directory ... not found, or Attempted to create lock file on a read-only directoryThe data directory is missing or the mongodb user cannot write to itFix
    2. 2Socket is already in use, or Failed to unlink socket fileAnother process already uses the MongoDB portFix

What does the error look like?

  • Data directory ... not found
  • Attempted to create lock file on a read-only directory: ...
  • Socket is already in use
  • Failed 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/mongodb

2Fix

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/mongodb

3Verify

Start the service and confirm it stays active.

sudo systemctl start mongod
systemctl status mongod

Confirmed by mongodb.com

Did this fix it?

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 27017

2Fix

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: 27018

3Verify

Restart the service and check that mongod listens on the port you chose.

sudo systemctl restart mongod
sudo ss -tulpn | grep mongod

Confirmed by mongodb.com

Did this fix it?

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

Servers

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.