Solution for permission and connection problems with MongoDB on macOS

Stepwise description of solving permission and connection issues with MongoDB on macOS. Final stabile solution (see §7) is setting a global alias with the right path in ~/.zshrc

2.    Prerequisites

3.    Problem description:

After installing MongoDB 4.4 Community Edition (https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ ) and trying to run MongoDB in terminal with the command ‘mongo’ I received the following error message:

Afbeelding met tekst

Automatisch gegenereerde beschrijving

This shows that MongoDB isn’t able to make a connection. Checking the result of the command ‘mongod’ gave that MongoDB didn’t have right permissions.

Afbeelding met tekst, groen

Automatisch gegenereerde beschrijving

The error message show that de dbpath needs to be set in the commandline or that the storagepath needs to be set in the configuration file.

4.    Solution for setting permissions on /data/db

In case you have problems with permissions on the default folder then you can follow these steps:

  1. cd into your local folder: ‘cd /usr/local/mongodb’
  2. make a new directory:  ‘sudo mkdir -p /data/db’
  3. cd into the new directory just created above: ‘cd /data/db’
  4. give mongo permisions: ‘sudo chown YourMacUserName /data/db’

5.    Solution for connecting MongoDB to the data

Assuming that you have the right permission on the folder containing the database-files the following steps can be taken:

  1. Opening ‘Terminal’
  2. Type the command: ‘mongod –dbpath /PATH/TO/DATABASE/’

This makes MongoDB running well again.

Afbeelding met tekst

Automatisch gegenereerde beschrijving

However this solution seems to be only temporary. After shutting down MongoDB we need to long command again and again to connect in the right way.

6.    Partial solution setting dbpath in mongo.conf

Setting the dbpath in mongo.conf somehow didn’t improve the working. I am still finding out what needs to be set in the mongo.conf or anywhere else in order to avoid typing everytime when starting the MongoDB server the command ‘mongod –dbpath /PATH/TO/DATABASE/’

I checked quite some resources, but couldn’t load the configuration settings automatically  that the command in Terminal mentioned above in step 2.1 wasn’t necessary. Or the configuration settings could be read but didn’t load properly. Used resources:

7.    Solution for altering command mongod to run MongoDB with the right path set

Since macOS 10.6 Catalina Apple uses the zsh shell rather than the bash shell, but if you’re using  The stabile solution to run MongoDB from Terminal with the command ‘mongod’ with the right path being set is done via a few steps:

  1. Shut down MongoDB (while mongo is running type in Terminal: ‘use admin’ presse enter and on a next line ‘db.shutdownServer()’.
  2. Close the Terminal window.
  3. Open ~/.zshrc in TextEdit or when your macOS is using bash-shell open ~/.bash_profile
  4. Add : 
  5. Save your .zshrc of .bash_profile file
  6. Open Terminal
  7. Type ‘source ~/.zshrc’ or for bash-shell users: ‘source ~/.bash_profile’
  8. Type ‘mongod’

MongoDB should now be running with the right path and connection. Terminal can be closed now. To check whether mongo is running open Terminal, type ‘top’ and check the list of applications. Another way is to type ‘mongo’, which gives you the interface in Terminal.

This makes MongoDB running well again.

Afbeelding met tekst

Automatisch gegenereerde beschrijving

Then open MongoDB Compass and use the connection string to connect to MongoDB:

Afbeelding met tekst

Automatisch gegenereerde beschrijving

There you are, up and running. You can now create databases, monitor the performance etc.

Afbeelding met tafel

Automatisch gegenereerde beschrijving

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.