MongoDB no suitable servers found

I had the same error in a docker based setup:

  • container1: nginx listening on port 80

  • container2: php-fpm listening on port 9000

  • container3: mongodb listening on port 27017

nginx forwarding php to php-fpm

Trying to access mongodb from php gave this error.

In the mongodb Dockerfile, the culprit was:

CMD ["mongod", "--bind_ip", "127.0.0.1"]

Needed to change it to:

CMD ["mongod", "--bind_ip", "0.0.0.0"]

And the error went away. Hope this helps somebody.


The IP address of your home network may have changed, which would lead to MongoDB locking you out.

I solved this problem for myself by going to MongoDB Atlas and changing which IP address is allowed to connect to my data. Originally, I'd set it up to only allow connections from my home network. But my home network IP address changed, and I started getting the same error message as you.

To check if this is the same issue with you, go to MongoDB Atlas, go into your project, and click "Network Access" on the left hand side of the screen. That's where you're able to update your IP address. It shows you what IP address(es) it's allowing in. To find out what your current IP address is, go to whatismyipaddress.com and update MongoDB if it's different.


Switching any localhost references to 127.0.0.1 helped me. There is a difference between localhost and 127.0.0.1

See: localhost vs. 127.0.0.1

MongoDB can be set to run on a UNIX socket or TCP/IP

If all else fails, what I've found that works most consistently across all situations is the following:

In your hosts file, make sure you have a name assigned to the IP address you want to use (other than 127.0.0.1).

192.168.0.101 coolname

or

192.168.0.101 coolname.somedomain.com

In mongodb.conf:

bind_ip = 192.168.0.101

Restart Mongo

NOTE1: When accessing mongo from the command line, you now have to specify the host.

mongo --host=coolname

NOTE2: You'll also have to change any references to either localhost or 127.0.0.1 to your new name.

$client = new MongoDB\Client("mongodb://coolname:27017");

Tags:

Php Mongodb