Unable to restart MongoDB after enabling access control

I recently tried enabling access control in MongoDB by following this this tutorial. However, when I reached the fourth step, MongoDB failed to start, showing this error:

{"error":{"code":38,"codeName":"FileNotOpen","errmsg":"Failed to read metadata from /var/lib/mongo/storage.bson"}}

After troubleshooting, I found two issues and their solutions:

  1. Use sudo: The mongod command must be run with sudo to avoid permission issues: sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb
  2. Directory name difference: On Fedora 41 (MongoDB 8.0.4 + Mongosh 2.3.9), the default directory is mongo, not mongodb

Some suggested modifying data and log directory persmissions, but simply using sudo resolves the issue.

I hope this helps others facing the same issue. Let me know if you have any questions or other solutions!

This is not recommend, mongod is now running as root. Ideally (and when installled via package manager) mongod will run under a dedicated non-privileged user.

The tutorial should really be updated to include starting with system utilites (systemctl).

1 Like

Thanks for your feedback! I tried restarting mongod using systemctl, but it didn’t work. Without sudo, the only way I could restart it was by rebooting my system. However, after a reboot, the server started without access control enabled.

For reference, I used this tutorial to check how to manage MongoDB with systemctl.

Maybe the proper solution is to adjust the privileges of the mongo(db) directory instead. What do you think?

@Matheus_Alves Yes, after having it run under root some repair of permissions will need to be done.

Depending on the system the user will be mongodb or mongod.

Stop the running mongod.
Change ownership of the data directory for RedHat: chown -R mongod: /var/lib/mongod
Start mongod with systemctl.

1 Like

I will try it later. Thanks!