How to fix MongoDB 'exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb, terminating'
Problem:
When trying to start MongoDB e.g. using sudo systemctl start mongod
, the process terminates with status code 100
and you see this error message in /var/log/mongodb/mongodb.log
:
example.txt
2019-07-22T17:11:07.858+0200 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb, terminating
Solution
Fix the permissions of /var/lib/mongodb
:
example.sh
sudo chown -R mongodb: /var/lib/mongodb
then restart MongoDB e.g. using
example.sh
sudo systemctl restart mongod
In case that does not help, check your error message if you are using a data directory different to /var/lib/mongodb
. In that case run
example.sh
sudo chown -R mongodb: <insert your data directory here>
In case that is not the case or doesn’t help either, check if your filesystem is mounted in read-only mode.
Check out similar posts by category:
MongoDB
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow