Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB no suitable servers found

Tags:

php-mongodb

I'm having trouble connecting to a replica set.

[MongoDB\Driver\Exception\ConnectionTimeoutException]                                                                                                              
No suitable servers found (`serverSelectionTryOnce` set): 
[Server closed connection. calling ismaster on 'a.mongodb.net:27017'] 
[Server closed connection. calling ismaster on 'b.mongodb.net:27017']
[Server closed connection. calling ismaster on 'c.mongodb.net:27017']

I however, can connect using MongoChef

like image 566
Bernardo Avatar asked Jan 13 '17 22:01

Bernardo


2 Answers

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.

like image 33
kiko283 Avatar answered Nov 08 '22 22:11

kiko283


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");

like image 73
mcmacerson Avatar answered Nov 08 '22 20:11

mcmacerson