How to Resolve “Read-only File System” Error When Attempting mkdir /data/db on Mac?

When configuring MongoDB on macOS, users may encounter a “Read-only file system” error when attempting to create the default data directory /data/db using the mkdir command. This error is caused by the System Integrity Protection (SIP) feature in macOS, which protects the root directory / from unauthorized changes.

In this article, We will learn about How to fix the Read-only file system when attempting mkdir /data/db on Mac by understanding various approaches in detail.

Understanding the Error

  • The `Read-only file system` error is due to macOS `SIP` term, which wants to make the root file system secure by forbidding unauthorized changes.
  • On trying to prepare the /data/db folder by creating it directly under the root of the filesystem, the SIP interferes which results in an error message.

Let’s understand the below solutions:

Since creating /data/db directly is not feasible due to SIP, we can use alternative locations that are writable. Here are a few recommended solutions:

  1. Create a Data Directory in the User Home Directory
  2. Modify MongoDB Configuration to Use a Different Path.

Solution 1: Create a Data Directory in the User Home Directory

A simple solution is to create the data directory within our home directory, which is not restricted by SIP. Follow these steps:

1. Create the Directory: Open Terminal and create the MongoDB data directory inside our home directory:

mkdir -p ~/data/db

Explanation: The command creates the directory structure ~/data/db if it doesn’t already exist. The -p flag ensures that any intermediate directories are also created if needed

2. Update MongoDB to Use the New Directory: When starting MongoDB, specify the new data directory using the –dbpath option:

mongod --dbpath ~/data/db

Explanation: This command starts the MongoDB daemon (mongod) and specifies the data directory (~/data/db) where MongoDB will store its data files

3. Create a Configuration File (Optional): For convenience, you can create or modify the MongoDB configuration file to set the new data directory permanently. Edit the MongoDB configuration file (usually mongod.conf or create one if it doesn’t exist):

storage:
dbPath: /Users/your-username/data/db

Explanation: This configuration sets the dbPath parameter in the MongoDB configuration file (mongod.conf) to /Users/your-username/data/db and specifying the path where MongoDB will store its data files. Replace your-username with your actual username.

Now Start MongoDB using this configuration file:

mongod --config /path/to/mongod.conf

Explanation:This command starts MongoDB using the configuration file located at /path/to/mongod.conf.

Solution 2: Modify MongoDB Configuration to Use a Different Path

We can configure MongoDB to use a different path for the data directory by modifying the MongoDB configuration file or using the command-line option.

1. Edit the Configuration File: Open or create the MongoDB configuration file (mongod.conf):

storage:
dbPath: /path/to/your/data/db

Choose a suitable path that is writable, such as /usr/local/var/mongodb or any other directory you have write access to.

2. Create the Directory: Ensure the directory exists and has the correct permissions:

sudo mkdir -p /usr/local/var/mongodb
sudo chown -R $(whoami) /usr/local/var/mongodb

Explanation:These commands create a directory /usr/local/var/mongodb and then change its ownership recursively to the current user (whoami) using sudo

3. Start MongoDB with the Configuration File: Use the –config option to start MongoDB with the specified configuration file

mongod --config /path/to/mongod.conf

Conclusion

Overall, The “Read-only file system” error on macOS can be overcome by creating the MongoDB data directory in a writable location outside the restricted areas protected by SIP. By following the recommended solutions, such as creating the data directory in the user’s home directory or modifying the MongoDB configuration to use a different path, users can successfully configure MongoDB on their macOS systems.


Contact Us