MacOS – How to fix Permission Denied error when starting up MongoDB

command lineinstallmacospermission

So I'm newbie to learning react and node and thought of building something based on MERN stack – I installed mongodb using npm install -g mongodb then it says its installed – then to create data directory for Monogo to use I ran mkdir -p /data/db and the response is Permission Denied.

To solve this I tried to do Fixing /etc/sudoers file which hasn't led me anywhere either. Anyway when I run this command mongod it gives me shutting me down server message as below:

2019-03-29T13:30:02.772+1100 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-03-29T13:30:02.792+1100 I CONTROL  [initandlisten] MongoDB starting : pid=2962 port=27017 dbpath=/data/db 64-bit host=Farwas-MBP
2019-03-29T13:30:02.792+1100 I CONTROL  [initandlisten] db version v4.0.3
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] git version: 7ea530946fa7880364d88c8d8b6026bbc9ffa48c
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] allocator: system
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] modules: none
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] build environment:
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] distarch: x86_64
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] target_arch: x86_64
2019-03-29T13:30:02.792+1100 I CONTROL [initandlisten] options: {}
2019-03-29T13:30:02.792+1100 E STORAGE [initandlisten] Failed to set up listener: SocketException: Address already in use
2019-03-29T13:30:02.793+1100 I CONTROL [initandlisten] now exiting
2019-03-29T13:30:02.793+1100 I CONTROL [initandlisten] shutting down with code:48

Best Answer

Use homebrew to install mongodb so you don’t need root would be my help to offer.

Then

brew install mongodb
brew services start mongodb 

As to sudoers, boot into recovery and use terminal (utilities menu) to mount the filesystem and fix it per this answer from your linked question. Older os can use single user mode, but terminal in recovery is nicer to use.