Search code examples
mongodbaccess-controlubuntu-20.04

IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB in ubuntu 20.04


I have just installed mongodb on my remote ubuntu server for using it with an angular and nodejs project. I created a user using the db.createUser command along with password and roles in the mongo shell.

Then when i try to start the mongodb instance with access control using the command:

mongod --auth --port 27017 --dbpath /var/lib/mongodb

its displaying the error:

IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB

Solution

  • First of all, have you checked the permissions of the folder /var/lib/mongodb (ls -l /var/lib)?

    The folder and its contents should be assigned to the mongod user. You could also check the ACL permissions (getfacl), if that's installed in your system.

    If permissions seem correct, please continue reading to know how I solved the same error in a different environment.


    This happened to me in CentOS 8 after a wrong reinstallation of MongoDB and the data folder /var/lib/mongo had to be created manually. In my case, the problem was that the security context of SELinux (https://www.redhat.com/en/topics/linux/what-is-selinux) had to be updated to give mongod access the folder.

    The command below solved my problem (note the path to the data folder in my configuration is slightly different, just update it for your case):

    chcon -Rv --type=mongod_var_lib_t /var/lib/mongo
    

    It basically tells SELinux that the context used by MongoDB to access the files should be associated with the target folder.

    You can have more information about chcon here: https://www.man7.org/linux/man-pages/man1/chcon.1.html