Error: couldn't connect to server 127.0.0.1:27017

I’m following the install guide “Install MongoDB Community Edition on macOS”. I’m getting the following error:

MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-02-18T23:15:25.465-0800 E QUERY [js] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-02-18T23:15:25.468-0800 F - [main] exception: connect failed
2020-02-18T23:15:25.468-0800 E - [main] exiting with code 1

I’m out of ideas and trying several Stackoverflow solutions have taken me back to square one where I can’t connect to the server running “mongo”

You need to ensure the MongoDB server is running.

Try: brew services start mongodb-community.

Regards,
Stennie

1 Like

Make sure MongoDB server is running and listening the port 27017 using netstat command

~ netstat -an | grep 27017

tcp4       0      0  *.27017                 *.*                    LISTEN

I’m running the service already and still unable to connect. I can verify this with brew services list

mongodb-community started {user} /Users/{user}/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

Running netstat -an | grep 27017 doesn’t return anything. This is after I verified that the brew service is running.

If I run mongod --dbpath /users/{username}/data/db/ after creating those directories I do get a return

Looks like problem is solved.
By default, MongoDB doesn’t allow remote connections, except localhost (127.0.0.1)

if you want other IPs connect to this server, go and edit mongod.conf file, comment out (put # in front of the line) the line looks like this

bindIp: 127.0.0.1