Mongodb – start mongod by service mongod start, attempt to create a lock file on a read-only directory /mgodb

mongodb

I changed /etc/mongod.conf's dbpath to /mgodb and then I execute the follow command:

  1. mkdir /mgodb
  2. chown mongod:mongod /mgodb
  3. chmod 0755 /mgodb

Then I run service mongod start, and get errors like:

initAndListen: 20 Attempted to create a lock file on a read-only directory: /mgodb

I tried to change /usr/lib/systemd/system/mongod.service's User and Group to root, it also get the same error.

But I cat run mongod --fork --config /etc/mongod.conf through command line.

Best Answer

If you look that /etc/mongod.conf, what are values of processManagement.pidFilePath and storage.dbPath? It sounds like that system tries to create files to some subdirectory, what don't exists or is not owned by user what is used to start mongod process.

You should never start your mongod with root user. And if you have done that, f.ex. when starting mongod to maintenance mode, always remember to chown -R user: <dir> data files back to the right owner.