MongoDB exception in initAndListen 20 Attempted to create a lock file on a read-only directory /data/db, terminating
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The error "exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating" is a common problem encountered when setting up or running a MongoDB instance. This error indicates that MongoDB is unable to write to the data directory as it doesn’t have the necessary permission, which is crucial since MongoDB must be able to write various files to this directory to function correctly.
Understanding the Error
MongoDB requires a specific directory, often /data/db, to store its database files. During initialization, MongoDB attempts to create a lock file—identified by mongod.lock—in this directory to prevent multiple instances from mistakenly using the same data files concurrently. If the directory is read-only, MongoDB cannot proceed, resulting in the "attempted to create a lock file on a read-only directory" error.
Causes of the Error
To grasp how this error arises, consider the following scenarios:
- Permissions Issue: The most common cause is that the user running the MongoDB process does not have the write permissions for the
/data/dbdirectory. - Incorrect Directory Configuration: MongoDB may be configured to use a different data directory, but this configuration might inadvertently point to a location with improper permissions.
- Filesystem Mount Type: The filesystem containing
/data/dbmay be mounted in read-only mode. - Non-existent Directory: The specified directory may not even exist, causing MongoDB to attempt (and fail) to create new directories.
Solutions to the Error
Resolving this error requires addressing the underlying filesystem or permissions issues. Here are some detailed steps to tackle the problem:
- Check Directory Existence:
- Ensure that the directory
/data/dbexists. - If it does not exist, create it using:
- Adjust Permissions:
- Verify and adjust the permissions of the directory using:
- Replace
id -uwithyour-usernameif running in a multi-user environment without elevated permissions.
- Check Filesystem Mount Options:
- Use
mountordfcommands to examine if the filesystem is mounted as read-only. - If necessary, remount the filesystem with write permissions. For example:
- Review MongoDB Configuration:
- Double-check the MongoDB configuration file (e.g.,
mongod.conf) for any incorrect path settings. - Ensure the
dbPathparameter points to a valid, writable directory.
- Starting MongoDB with Correct Options:
- If not using a configuration file, specify the
dbpathdirectly when starting MongoDB:
Example Resolution
Here's a step-by-step example of identifying and fixing this error:
Key Points Summary
| Key Point | Description |
| Common Error Message | Exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating |
| Primary Cause | Permissions issue; MongoDB cannot write to /data/db. |
| Solution Steps | 1. Check permissions 2. Ensure directory existence 3. Adjust file system settings 4. Fix MongoDB configuration |
| Check Permissions | Use ls and chown to ensure write permissions are set. |
| Correct File System Configuration | Ensure directory is on a writable file system. |
| Rerun MongoDB with Correct Settings | Ensure mongod starts with correct dbpath settings. |
Additional Considerations
- Docker Environment: When running MongoDB within containers, you might face similar issues if the volume mounts are not correctly specified. Ensure that the host directory bound to the container has appropriate read/write permissions.
- SELinux Restrictions: In environments with Security-Enhanced Linux (SELinux) enabled, you may encounter additional permission restrictions. Use the
chconcommand with appropriate context settings to address these.
Understanding and resolving this error revolves around adjusting file permissions and ensuring the correct setup of MongoDB directories. By following the outlined solutions, you mitigate runtime interruptions and ensure optimal database functionality.

