Setting up MongoDB on macOS Catalina — Common Errors and their Solutions

In this reading I will be helping you step-by-step on setting up MongoDB and make it running…

·

4 min read

Setting up MongoDB on macOS Catalina — Common Errors and their Solutions

Following the official documentation I personally faced a lot of errors and inconviencies in setting up MongoDB on my machine. In this reading I will guide you through all the errors and their solutions to help you setup Mongo with ease.

Step #1:

You should have Homebrew installed on your machine as a prerequisite to download mongodb. Paste this in your terminal and go

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

You have successfully installed Homebrew on your machine. 👍

Step #2:

Install MongoDb Community version by executing the following commands on your terminal.

brew tap mongodb/brew

brew install mongodb-community@4.2

This ensures mongodb is installed successfully on your machine.

The following are some useful commands to run mongo on your machine

1_JKH_IXtf5du-dykf5ZdCNA.png

  1. To run MongoDB as a macOS service
    brew services start mongodb-community@4.2
    
    (or) brew services start mongodb-community ```
  2. To stop a MongoDB running as a macOS service
    brew services stop mongodb-community@4.2
    (or)
    brew services stop mongodb-community
    
  3. To run MongoDB manually as a background process
    mongod --config /usr/local/etc/mongod.conf --fork
    
  4. To restart MongoDB running as a macOS service
    brew services restart mongodb-community@4.2
    
    (or)
    brew services restart mongodb-community
    
  5. To view if MongoDB is running as a macOS service or not
    brew services
    

This gives you the list of macOS services running on your machine and you can view the status of MongoDB community from that list. To run the CLI version of Mongo just type in

mongo

This is the ideal way of installing and setting up Mongo, but this doesn't work as easy as it looks all the time. It comes with some errors and here are their solutions

1_l9d_Ii0QCGQchb1wfVv4Ow.png

  1. Error : macOS preventing Mongo from opening This is the most common and occurs due to the fact that mongo is a third party software being downloaded and mac by default gives a break to the opening of such softwares. To solve this open

System Preferences > Security & Privacy > General

Click the button to the right of the message about mongod, labelled either Open Anyway or Allow Anyway depending on your version of macOS.

  1. Error : NonExistent Path : Data directory /data/db not found … terminating

This is the error that is more evident for Catalina users and the most common solution that is provided on Stack Overflow is to create a directory /data/db manually in the root. But this doesn’t work for Catalina.

  • Error you might encounter : mkdir: /data/db: Read-only file system Catalina has, for a surpirse, removed authority to make changes in the root directory. Hence we aren’t able to solve the issue as mentioned above. To solve this error follow the steps given below :-

Step #1: Go to your terminal and execute

cd /System/Volumes
mkdir Data
cd Data
mkdir data
cd data
mkdir db

This ensures you have created a directory for storing the database stuff in

Step #2: The configuration file for the MongoDB is created at /usr/local/etc/mongod.conf Reopen the terminal and execute the following

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

You can use any favourite editor of your choice and open the mongod.conf file. For commanlity I am using Vim

Step #3:

1_UMFnbGo_qbDZSDKA7Q6ZZg.png

press ‘i’ to go into edit mode and then move to dbPath and change

data/db ----> /System/Volumes/Data/data/db

after you have changed it enter ‘esc’ and then type :wq! to save and quit the file

1_mwV1Xwk0o37l-z-QwfuFwA.png

A more simpler way to execute Step #3 is

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

This simpler single line of code might not work for all machines, then follow the first approach to change the Database path.

  1. Error : Couldn’t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91 exception: connection failed

This error occurs due to the reason that mongod isn’t running as a service. The only valid solution is to start the service by

brew services start mongodb-community@4.2

(or)

brew services start mongodb-community

To run mongo just type

mongo

These are the most probable errors and how to solve and setup MongoDB on macOS Catalina. Please do have a look into my YouTube channel : Code Studio Sai Ankit that discusses everything related to coding from Computer Science Concepts, Competitive Coding tutorials, Codeforces Editorials, Development Projects. Please also do leave a like and Subscribe on the content you like 👍. Code Studio Sai Ankit