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”

3 Likes

You need to ensure the MongoDB server is running.

Try: brew services start mongodb-community.

Regards,
Stennie

24 Likes

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

~ netstat -an | grep 27017

tcp4       0      0  *.27017                 *.*                    LISTEN
1 Like

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

2 Likes

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

2 Likes

: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?
1 Like

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

4 Likes

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?

1 Like

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.

1 Like

@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:

1 Like

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.

24 Likes

Would love an official Mongo response to this - as of right now my experience has been identical to Dean_1812’s. Could we get an update or addition to the official tutorial?

1 Like

How can I troubleshoot this error message?

“Error: couldn’t connect to server 127.0.0.1:27017” is a general error message indicating that your client/driver cannot connect to a server on the specified hostname/IP and port. In this specific example, 127.0.0.1 is the hostname or IP and 27017 is the port.

Typical reasons for this error are:

  • Your connection from client to server is blocked by firewall or network configuration
  • A MongoDB server is not listening on the requested Host/IP and port (check they are both correct)
  • You are connecting to a remote MongoDB deployment that is not listening to the public IP you are connecting to.

What should I check for my MongoDB Atlas cluster?

Please refer to Troubleshoot Connection Issues in the MongoDB Atlas documentation.

What should I check for a self-hosted deployment?

All modern versions of MongoDB bind to localhost by default so you have to explicitly adjust IP Binding to allow external connections.

Please review the MongoDB Security Checklist and consider available security measures to avoid opening your deployment to being compromised. In particular, I strongly recommend always:

Follow the general security practice of Principle of Least Privilege and consider how you can reduce unnecessary risk to your deployment. For example, instead of opening up your deployment to the world, consider using a VPN or SSH tunnel to provide secure remote access.

What should I check for MongoDB Compass?

The above suggestions should be helpful, but the Compass documentation also includes a reference for Compass Connection Errors.

What should I check for macOS Catalina (or newer)?

If you run mongod without providing a configuration file, it will use a hardcoded default of /data/db which is not created as part of the Homebrew installation and not supported on macOS Catalina (which no longer allows creating paths on the root filesystem).

The documented steps for Run MongoDB Community Edition on macOS are correct (and in recommended order of approach):

  • To run MongoDB (i.e. the mongod process) as a macOS service , issue the following:

brew services start mongodb-community@4.2

To run MongoDB (i.e. the mongod process) manually as a background process , issue the following:

mongod --config /usr/local/etc/mongod.conf --fork

If you want to run mongod without using either of these options, you can also explicitly pass the --dbpath parameter. Per SERVER-46398, this suggestion has been added to recent releases of MongoDB (3.6.19+, 4.0.19+, 4.2.7+, 4.4.0-rc4).

I have a similar problem and still need help. What should I do?

If you are having a similar issue connecting to your MongoDB deployment, please start a new discussion topic with details specific to your environment such as:

  • MongoDB server version
  • O/S version
  • How you installed MongoDB (package manager, tarball, etc) or the installation guide you followed
  • Command-line or steps used to start your MongoDB server
  • Client or driver version you are trying to connect with

It would be very useful to include information on any troubleshooting steps you have already tried.

Regards,
Stennie

10 Likes