180

I am trying to create a new folder in the root directory.

I tried all kinds of examples.

sudo mkdir /data/db
sudo mkdir -p /data/db

I keep getting:

mkdir: /data: Read-only file system
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
jalapina
  • 3,962
  • 3
  • 12
  • 19
  • 3
    https://stackoverflow.com/questions/58283257/mongodb-doesnt-work-on-latest-mac-os10-15 This answers the comments below. – camille Oct 16 '19 at 18:48
  • 2
    Also see https://stackoverflow.com/questions/58283257/mongodb-doesnt-work-on-latest-mac-os10-15 – Adam Zerner Oct 23 '19 at 19:01
  • 1
    [Techno Peace](https://stackoverflow.com/users/12741466) posted an [Answer](https://stackoverflow.com/a/66233947) saying "Due to Catalina OS update, it changed. Follow this link to achieve: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/" – Scratte Feb 17 '21 at 17:20
  • 1
    This is still an issue on Big Sur – Sam King Mar 08 '21 at 03:55

16 Answers16

222

If you have a Mac and updated to Catalina or more recent version, then the root folder is no longer writable.

I just changed the directory somewhere else.

Been using this command for now

mongod --dbpath=/Users/user/data/db
Ebenezer Nikabou
  • 369
  • 4
  • 14
jalapina
  • 3,962
  • 3
  • 12
  • 19
  • 7
    Is there a way to make it permanent? I have to type it every time I start the MongoDB server! – AV Paul Nov 20 '19 at 08:54
  • 2
    @AVPaul Maybe not the best solution, but you could make an alias that refers to your path. – DjangoBlockchain Dec 10 '19 at 13:34
  • Im actually getting Data directory /Users/user/data/db not found., terminating. Even though I created the folder – Jeff Goes Jan 04 '20 at 20:24
  • 1
    @JeffGoes I am assuming you did not replace the user with the actual name. – Aman Jan 12 '20 at 23:44
  • Despite this working (and solving my problem), it seems hacky – Alex L Feb 14 '20 at 19:57
  • 3
    @AlexL It's working for me too. But do I have to type mongod --dbpath=/Users/my_USER_name/data/db everytime I want to run my dev mongo db? Because next time when I run mongod, it is saying /data/db not found – Shoeb Mirza Feb 28 '20 at 07:31
  • 7
    you can make it permanent by setting the dbPath in mongoDB config file if you're using mac it's under /usr/local/etc/mongod.conf. then add the following : "storage: dbPath: path to your db directory" – Samoox Aug 05 '20 at 11:56
  • 4
    I forgot how annoying updating OS X is no wonder I always resist it – Anthony Aug 19 '20 at 08:44
  • It might be worth noting that `/usr` is a hidden folder. To 'see' it, press cmd + shift + . (dot). That being sad, I don't seem to have a folder `etc` in `/usr/local/`. Now what? – Reinier68 Oct 13 '20 at 19:56
  • This answer is a work around but does not seem to be what the question is asking. – Brianhenry Dec 15 '22 at 10:13
107

from the official docs https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

install homebrew and run the following commands

sudo chown -R $(whoami) $(brew --prefix)/*

then

brew tap mongodb/brew

then

brew install mongodb-community@4.2

and

brew services start mongodb-community

or

mongod --config /usr/local/etc/mongod.conf

then

ps aux | grep -v grep | grep mongod

and

mongo

to verify you can run show dbs in the mongo shell

aug2uag
  • 3,379
  • 3
  • 32
  • 53
Will Abule
  • 1,414
  • 1
  • 10
  • 13
58

With the new macOS Catalina update, the folder /data/db becomes read-only, you cannot modify it. Follow this procedure to create a DB in another folder:

  1. Change mongod directory :

    sudo mongod --dbpath /System/Volumes/Data/data/db

  2. Give it an alias to use it as mongod:

    alias mongod="sudo mongod --dbpath /System/Volumes/Data/data/db"

  3. Just type mongod in your terminal, it should work.

Extra => If you need to give it current user rights, use this line of code :

sudo chown -R $(whoami) /System/Volumes/Data/data/db

(Just for info -> $(whoami) is just a variable that returns your current user)

Mohit kumar
  • 447
  • 2
  • 10
Eugène Beliaev
  • 1,041
  • 10
  • 4
16

You can't create a folder on the root directory on Mac Catalina anymore!

https://www.apple.com/macos/catalina/features/

Dedicated system volume

macOS Catalina runs in a dedicated, read-only system volume — which means it is completely separate from all other data and helps improve the reliability of macOS.

# macOS Catalina, mkdir path
$ sudo mkdir -p /System/Volumes/Data/data/db

# give permissions
$ sudo chown -R `id -un` /System/Volumes/Data/data/db

# macOS 10.14.x -
$ sudo mkdir -p /data/db

# macOS 10.15.x +
$ sudo mkdir -p /System/Volumes/Data/data/db

refs

https://discussions.apple.com/thread/250720711

https://zellwk.com/blog/install-mongodb/

https://www.youtube.com/watch?v=DX15WbKidXY

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
12

To make a permanent change of the path of mongod db folder.

Following these docs they say roughly this. If mongod is started with brew services:

$ brew services start mongodb-community@4.2

It will use config file at path /usr/local/etc/mongod.conf


To fix this, edit the config file:

$ vim /usr/local/etc/mongod.conf

And change the dbPath e.g. to your home directory like this:

systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /Users/<username>/data/db
net:
  bindIp: 127.0.0.1 

Save the file and restart mongod with brew:

$ brew services restart mongodb-community@4.2
Jirik
  • 1,435
  • 11
  • 18
7

If you are on mac and facing the issue then below command is useful, whoami variable will get the current user

mongod --dbpath=/Users/$(whoami)/data/db
Anjum....
  • 4,086
  • 1
  • 35
  • 45
7

So, with macOS Catalina Apple created a new Volume for security purposes. If you’re on macOS Catalina, you'll need to create the /data/db folder in System/Volumes/Data.

Follow these commands

  1. sudo mkdir -p /System/Volumes/Data/data/db
  2. sudo chown -R id -un /System/Volumes/Data/data/db

Start MongoDB

  1. brew services run mongodb-community

  2. Enjoy

  • `brew services run mongodb-community` gave me `Error: No available formula with the name "mongodb-community".` – JoeTidee Sep 02 '21 at 00:18
7

I am at a loss why Apple did that.. So now, we can not have any root level directory other than theirs ? This just doesn't make any sense and people creating with root privileges those folders know what they are doing

5

I did this:

mkdir -p usr/local/var/mongodb/data/db

since the new path that is "usr/local/var/mongodb".

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
franfonse
  • 189
  • 1
  • 3
  • 10
5

Default Paths for the mongodb-community Formula

In addition to installing the MongoDB server and tool binaries, the mongodb-community formula creates:

  • a configuration file: /usr/local/etc/mongod.conf
  • a log directory path: /usr/local/var/log/mongodb
  • a data directory path: /usr/local/var/mongodb

then it worked for me:

mkdir -p usr/local/var/mongodb/data/db
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
4

This is what worked for me as I was undergoing a Udemy course: 1. Install HomeBrew by typing this into your terminal

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. Move your old /data/db folder (if you want to backup your current dbs) into a non root folder and proceed with the next step

  2. Run in Terminal a) brew uninstall mongodb b) If needed run brew uninstall --force mongodb c) brew tap mongodb/brew d) brew install mongodb-community e) brew services start mongodb/brew/mongodb-community

  3. All you need now is to run mongo in the Terminal and you'll see the mongo shell symbol >.

Please let me know if this works ;) It took me almost 2 hours to figure it out, based on this article: https://apple.stackexchange.com/questions/362883/mongodb-doesnt-work-after-update-to-macos-catalina-10-15

Cheers, Radu

Radu S.
  • 41
  • 1
4

To add on to what David Buck wrote here, first you have to actually make the directory. You can do that with:

sudo mkdir /System/Volumes/Data/data/db

If you're getting this error:

"Error parsing command line: unrecognised option '--dbpath/System/Volumes/Data/data/db' try 'mongod --help' for more information"

after creating the alias, it's just because David Buck (while making a really helpful comment) missed a space in his answer.

To fix this, type

unalias mongod

and then type

alias mongod="sudo mongod --dbpath /System/Volumes/Data/data/db"

JohnnyG
  • 41
  • 1
1

You cannot create a /data/ directory. That is reserved for the APFS filesystem. Check Bombich’s blog about this issue.

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • So, could this read-only file issue be solved? I have the same problem – Zainab Hammami Oct 13 '19 at 12:29
  • 2
    No, you need to use another name for your directory. A possible solution could be to create the data directory inside another directory which is allowed as a root directory. – jvarela Oct 13 '19 at 12:47
1

With macOS Catalina, you can no longer store files or data in the read-only system volume, nor can you write to the "root" directory ( / ) from the command line, such as with Terminal.

user803422
  • 2,636
  • 2
  • 18
  • 36
1

I create a gist with simple steps to install and run mongodb on catalina

# install homebrew (https://brew.sh/) and run the following commands
sudo chown -R $(whoami) $(brew --prefix)/*
brew tap mongodb/brew
brew install mongodb-community@4.2

## --- aliases to set in your zshrc file

# open your zshrc file
open ~/.zshrc
# copy and paste shorcuts in the end of the file
alias mongod='brew services run mongodb-community'
alias mongod-start='brew services start mongodb-community' #will start MongoDB automatically when you login into your Macbook
alias mongod-status='brew services list'
alias mongod-stop='brew services stop mongodb-community'

# restart your terminal
# type mongod in your terminal for run service & mongod-stop for finish it
# test your mongodb connection with
mongo
show dbs

https://gist.github.com/sturmenta/cf19baa91b1d79d8ae2b305fb7e1f799

Nicolas Sturm
  • 619
  • 5
  • 7
0

Mac version Catalina made the root folder is no longer writable.

Brew has an updated version of mongodb to use a new path (which it creates itself), /usr/local/var/mongodb and following these instructions will fix the issue:

Guide to installing updated mongodb-community-edition

brew install mongodb-community@VERSION where the first VERSION with the fix is 4.2

RobKohr
  • 6,611
  • 7
  • 48
  • 69