MongoDB no suitable servers found

Bernardo picture Bernardo · Jan 13, 2017 · Viewed 33.5k times · Source

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

Answer

mcmacerson picture mcmacerson · Jan 30, 2017

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