MongoDB Catalina: connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 MongoDB Catalina: connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 database database

MongoDB Catalina: connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017


An alternative way to fix this Catalina Mac OS root & mongodb connection issue, do the following:

Install Homebrew, if you have it, reinstall it again

run the following commands in the terminal

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"brew untap mongodb/brew && brew tap mongodb/brewbrew install mongodb-community@4.2

The issue is mongo cannot find /db/data because there is no directory, so you have to create one:

cd ~ (this should take you to your Users folder)

from the Users folder make your own db/data folder by running the follow commands:

mkdir db & cd inside db folder

inside the db folder run: mkdir data & cd inside data folder

run this command: mongod --dbpath ~/data/db(inside the Users/data/db/ folder you just created)

Now open a new tab & run: cd ~ (brings you back to Users) -> now run : cd .. & cd .. again (do this twice)(now you should be in the folder before Users)

(now find tmp folder) cd into the /tmp folder

delete the sock file (this is giving you connection issues)

delete it by running: rm -rf mongodb-27017.sock

now run command: mongo (this should work now)

inside mongo shell run command: db.verion()if you see a version, your connection works.

From now on to run mongodb database & connection you will have to have to tabs open at all times whenever you want to work on your projects

Example:in one tab run command -> mongod --dbpath ~/data/db (this starts the connection)in the other tab run command: mongo(this starts the shell)

These both have to be running..

Side Note:you no longer need to startthe connection by running brew services start mongo-community anymore.

remember mongod --dbpath ~/data/db is basically -> mongod command now.. The Mac OS Catalina Update created root permission problems, that is why mongod command alone never worked before.

Hope this helped you. Goodluck.


Faced a similar issue with macOS Big Sur. After trying all the approaches mentioned above including

  • updating homebrew
  • reinstalling MongoDB, MongoDB community and setting up a /System/Volumes/Data/data/dbfolder

This command worked for me mongod --dbpath=/System/Volumes/Data/data/db

reference: https://medium.com/codespace69/mongodb-troubleshooting-errors-for-beginners-and-macos-catalina-31befd99f6c8


This error is caused due to ownership issues.Here we are changing the ownership to 'mongodb' user.
Command 1: deletes the /tmp/mongodb-27017.sock file$sudo rm -rf /tmp/mongodb-27017.sock
Command 2: starts mongod service$sudo service mongod start
Command 3: shows the file with its ownership detailsls -lsah /tmp/mongodb-27017.sock
output0 srwx------ 1 mongodb mongodb 0 Aug 24 04:01 /tmp/mongodb-27017.sock