MongoDB 升级到 Mac OS 10.15(Catalina)后找不到数据目录

我今天升级到 MacOS 10.15(Catalina)。当我在终端运行 mongod时,它找不到 /data/db目录:

➜  /Users/william > mongod
2019-10-08T17:02:44.183+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] MongoDB starting : pid=43162 port=27017 dbpath=/data/db 64-bit host=Williams-MacBook-Pro-6.local
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] db version v4.0.3
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] git version: 7ea530946fa7880364d88c8d8b6026bbc9ffa48c
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] allocator: system
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] modules: none
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] build environment:
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten]     distarch: x86_64
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2019-10-08T17:02:44.209+0800 I CONTROL  [initandlisten] options: {}
2019-10-08T17:02:44.211+0800 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2019-10-08T17:02:44.211+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2019-10-08T17:02:44.211+0800 I NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2019-10-08T17:02:44.211+0800 I CONTROL  [initandlisten] now exiting
2019-10-08T17:02:44.211+0800 I CONTROL  [initandlisten] shutting down with code:100
➜  /Users/william >

我尝试用 brew安装 MongoDB:

brew install mongodb


➜  /Users/william > brew install mongodb
Updating Homebrew...
Error: mongodb: unknown version :mountain_lion

有人帮忙吗?

42820 次浏览

This is the main error:

exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating

Catalina has a surprise change: it won't allow changes to the root directory (this was discussed in a forum thread as well):

% sudo mkdir -p /data/db
mkdir: /data/db: Read-only file system

Unfortunately, this is not spelled out explicitly in Apple's Catalina release notes, other than a brief mention in Catalina features:

macOS Catalina runs in a dedicated, read-only system volume

Since the directory /data/db is coded as MongoDB default, a workaround is to specify a different dbpath that is not located on the root directory. For example:

mongod --dbpath ~/data/db

This will place MongoDB's data in your home directory. Just make sure that the path ~/data/db actually exists.

Alternative method

An alternative method is to follow the instructions at Install MongoDB Community Edition on macOS by leveraging brew:

brew tap mongodb/brew
brew install mongodb-community

This will create some additional files by default:

  • the configuration file (/usr/local/etc/mongod.conf)
  • the log directory path (/usr/local/var/log/mongodb)
  • the data directory path (/usr/local/var/mongodb)

To run mongod you can either:

  • Run the command manually from the command line (this can be aliased for convenience):

    mongod --config /usr/local/etc/mongod.conf
    
  • Run MongoDB as a service using brew services. Note that this will run MongoDB as a standalone node (not a replica set), so features that depends on the oplog e.g. changestreams will not work unless you modify the mongod configuration file:

    brew services start mongodb-community
    

After installing Catalina, you might have a folder on your Desktop called Relocated Items. You can find the data/db folder inside the Security folder.

All I had to do was move the Security/data/db, specifically data/db and place it inside my home folder.

You can do this with the Finder app or with the terminal by running the following command:

sudo mv /Users/Shared/Relocated\ Items/Security/data ~/

After that run: mongod --dbpath ~/data/db

It's up to you to create the alias

found a trick... delete the ~/data/db entire folder from the directory then recreate a new one but DO NOT USE mkdir -p /data/db instead, just use your trackpad to do it use system way to create folder

this worked for me

Here's what worked for me:

  1. Create a folder somewhere e.g. Development/mongodb/data/db
  2. Open mongod.conf file and update storage.dbPath value to the the newly created folder path

Kevinadi already did justice to the question, however this is how I went about solving the problem:

After installing mongodb-community

  1. sudo mkdir -p /System/Volumes/Data/data/db (Create the data/db folder)
  2. sudo chown -Rid -un/System/Volumes/Data/data/db (Give permissions)
  3. mongod --dbpath=/System/Volumes/Data/data/db (Change dbpath of mongodb)
  4. mongod (Runs well)

I found this article on installing mongodb very useful

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

Your docker image will be able to find your directories regardless of where they are if you use relative directory references ~/Desktop will map your Mac Desktop directory into your image as /Desktop.

docker run -it -v ~/Desktop:/Desktop r-base bash

Go to ~/Users/Shared folder. here, you will find a folder titled 'Relocated items' inside that you will be able to find a 'data' folder.This 'data folder contains your mongodb database in`/data/db'.

Now, copy this 'data' folder and paste it in your Users/<name-of-your-user-account-with-the-home-icon">.

And now open zsh terminal and run mongod --dbpath ~/data/db and now open a new shell in a separate tab and type mongo to open the mongo shell and you will be good to go.

Remember to use mongod --dbpath ~/data/db each time while running mongod or make an alias.

I've faced the same problem with BigSur and unfortunately Relocated Items folder didn't contain anything looking like my MongoDB dump. I decided to search Finder by database name, as dump contains folders with database names. And my dump turned out to be here

Macintosh HD/System/Volumes/Macintosh HD/data/db

As far as I understood the dump had been moved on the sly during OS update because it used to be at root and now OS does not give permission to write to root.

For me after the Catalina upgrade it moved to a different folder than mentioned above. In case anyone finds the same path, you can use the below solution.

cp /Users/Shared/Previously\ Relocated\ Items/Security/data ~
mongod --dbpath ~/data/db

This way you can see your old databases and collections when you access mongo shell using

mongo
show dbs