MongoDB installation macOS Catalina

Viewed 2276

I recently started developing with NodeJS. In the course I attend, they gave me steps to install on Mac but it's giving me problems. After a some googling, I understood that macOS Catalina has blocked writing access to the root folder. After that, I spent a day searching on Google and Stack Overflow for solutions to this problem. There were many different solutions, but none of them worked. Some installed, but the mongod command wouldn't work. Even the installation process on MongoDB itself didn't work.

At the end, I decided to use MongoDB Atlas. So in my cluster0, I went to Connect > Connect with mongo shell > I do not have MongoDB installed. They gave some steps to install it with Homebrew. Now, when I type mongo "mongodb+srv://cluster0.hflrg.mongodb.net/<dbname>" --username <myUserName>, it connects to MongoDB Atlas. The mongod command still doesn't work.

Can you tell me a way to install MongoDB on Mac. I have macOS Catalina Version 10.15.5.

Note: I don't have much experience with the terminal, so if possible, please avoid using terms like "Add so and so the you $PATH variable", and try to explain in detail. A huge thanks in advance.

3 Answers

I usually and like to install by downloading .tgz package from MongoDB Community Server, and setting up via command lines.

  1. Download .tgz package from MongoDB Community Server.

  2. Extract the files from the downloaded archive:

tar -zxvf mongodb-macos-*.tgz
  1. Copy binaries in your PATH environment variable:
sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/
  1. Create the data directory:
sudo mkdir -p /usr/local/var/mongodb
IMPORTANT
Starting with macOS 10.15 Catalina, Apple restricts access to the MongoDB default data directory of /data/db. On macOS 10.15 Catalina, you must use a different data directory, such as /usr/local/var/mongodb.
  1. Create the log directory:
sudo mkdir -p /usr/local/var/log/mongodb
  1. Run MongoDB:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log

To run mongod in the background, use --fork at the end of command line:

mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork

Helpful links and references:

Installing MongoDB on macOS tarball
MongoDB on macOS Catalina v10.15+

Hope these may help! Have a great one!

Use Homebrew, it uses /usr/local by default.

brew tap mongodb/brew
brew install mongodb/brew/mongodb-community
brew services start mongodb/brew/mongodb-community
mongo --quiet --eval 'db.version()'
4.2.8

This worked for me, but is based on the answer by @Erni_Souza

  1. Download the .tgz package from here MongoDB Community Server.

  2. Extract the files on your machine using tar -zxvf mongodb-macos-*.tgz or just use the default Archive Utility.app on your machine.

  3. Copy the binaries into your /usr/local/bin folder with:

    sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/ just as instructed or you can resort to using the ./mongod command via Terminal.app from the folder you extracted into.

  4. Create the data directory mkdir -p ~/data/db

  5. Run mongodb

    mongod --dbpath ~/data/db or with ./mongod --dbpath ~/data/db if the binaries are still in the directory you extracted them to.

You can check out this link MongoDB on macOS Catalina v10.15+ as given in the answer by @Erni_Souza for more information.

Related