MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

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

3 Likes

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

Screen Shot 2020-02-19 at 7.16.20 PM

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

1 Like

i am going through the same issues, please how did you solve yours? i tried the netstat, heck i even uninstalled and reinstalled several times, nothing on stackoverflow has helped… i really need help

:wave: Hello @Odiachi_Daniel and welcome to the MongoDB community forums.

It’s helpful if you provide more information so we can help you. While your issue is similar to the original poster you might have other things going on.

  1. What errors are you getting when trying to connect to your mongod instance?
  2. Are you trying to connect to a local or remote instance?
    a. Can you connect locally using the mongo shell?
  3. Are you hosting MongoDB or are you using something like MongoDB’s Atlas?
  4. Is your bind_ip set correctly if you’re trying to access from a remote machine?
  5. If trying to connect remotely, are there any firewalls that might be stopping the connection from being made?

Error
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
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
[main] exception: connect failed
[main] exiting with code 1

solution.

service mongod stop
#dont start mongod…instead…

systemctl start mongod
#then mongo command

mongo

Same issue, macOS Catalina 10.15.4. I have successfully installed mongodb following official tutorial. But when I reach #run-mongodb-community-edition point it gets messy. There are 2 options to run: as a macOS service and as a background process.

First one ($brew services start mongodb-community@4.2) returns Successfully started mongodb-community, second one ($mongod --config /usr/local/etc/mongod.conf --fork) returns Unrecognized option: auth=true. Let’s just use the first one as it preffered way according to the docs anyway. At this moment if I run $ps aux | grep -v grep | grep mongod I don’t get anything (which basically means that MongoDB is not running).

After that we go to the run mongo part, which is just a $mongo call. And here I get the weird output:

MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-04-30T15:41:02.184+0700 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-04-30T15:41:02.186+0700 F - [main] exception: connect failed
2020-04-30T15:41:02.186+0700 E - [main] exiting with code 1

Well, here we start to find a workaround. Instead of running $brew services start mongodb-community@4.2 I tried to run just $mongod and I got the following output with an error:

2020-04-30T15:47:43.685+0700 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none’
2020-04-30T15:47:43.687+0700 W ASIO [main] No TransportLayer configured during NetworkInterface startup
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] MongoDB starting : pid=34897 port=27017 dbpath=/data/db 64-bit host=Egors-iMac.local
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] db version v4.2.6
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] git version: 20364840b8f1af16917e4c23c1b5f5efd8b352f8
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] allocator: system
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] modules: none
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] build environment:
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] distarch: x86_64
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] target_arch: x86_64
2020-04-30T15:47:43.687+0700 I CONTROL [initandlisten] options: {}
2020-04-30T15:47:43.688+0700 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2020-04-30T15:47:43.688+0700 I NETWORK [initandlisten] shutdown: going to close listening sockets…
2020-04-30T15:47:43.688+0700 I - [initandlisten] Stopping further Flow Control ticket acquisitions.
2020-04-30T15:47:43.688+0700 I CONTROL [initandlisten] now exiting
2020-04-30T15:47:43.688+0700 I CONTROL [initandlisten] shutting down with code:100

So it looks like installation process didn’t create directory /data/db. I am not sure, where is it must be created? And this seems to be the reason why $brew services start mongodb-community@4.2 does not work. Because if I manually create /data/db folder and run $mongod --dbpath /users/{username}/data/db/ everything work as expected.

But please don’t close the issue as it’s not a solution. Because following official docs lead to different errors. How should we solve it properly?

I’ve just read that this issue is common for macOS Catalina users because

The new MacOS 10.5 does not allow you, by default to create new volumes in the / subsystem and if you do, they will be deleted on restart.

So /data/db can’t be created during installation’s process.

@Dean_1812 I am having the exact same issue as you. Were you able to get it resolved?

Buenas Noches, Les comento que hace un Rato MongoDB me Arrojo el Mismo Error y para mi Me Resulto Reiniciar la Pc, luego Ejecute los comandos para ver el Estatus e Iniciar y Listo Levanto sin Problemas…
Solo Prueben a ver si les Ayuda, no se pierde nada con hacerlo. :+1:

create a data/db in your home directory

1.cd /Users
2. cd to your home directory
3.mkdir data
4.cd data
5.mkdir db
6. mongod --dbpath ~/data/db press enter then just keep it running.