11

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

Bernardo
  • 475
  • 1
  • 5
  • 19
  • kindly, make sure that `bindIp` in `/etc/mongod.conf` is correctly added. – Mustafa Magdi Jan 23 '17 at 10:42
  • I am facing this issue multiple times, have checked all ports and allowed all IP addresses. but the issue still persists as Error : No suitable servers found (`serverSelectionTryOnce` set): [socket timeout calling ismaster ... – Pawan Rama Mali Jul 29 '21 at 09:51

4 Answers4

16

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

mcmacerson
  • 885
  • 2
  • 14
  • 17
4

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.

kiko283
  • 490
  • 6
  • 15
3

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Durand Sinclair
  • 159
  • 1
  • 6
0

In my case, I am temporarily coding PHP from Windows7 against MongoDB on my VPS running Linux Debian 9. The PHP will be eventually running in the same Linux box to provide an API to the MongoDB data. BTW, it does not appear this local composer install is doing me any good, it's pure ugliness. My PHP after the fix below works without the require line require_once 'C:\Users\<Windows User Name>\vendor\autoload.php'.

My fix is different than the accepted answer which to me did not make sense.

I did not have to touch any hosts file

So edit your /etc/mongod.conf with your target machine's IP and restart with sudo systemctl restart mongod that's it

enter image description here

I don't know what to blame

  1. PHP and MongoDB sites for the terrible documentation skimpy and incomplete PHP examples, or...
  2. MongoDB installation on Linux failing to mention this bindIP.

My startup experience with MongoDB is so far very negative given all the changes that have occurred nothing matches what I expected from the videos I watched. I can't seem to find any that reflect what I am going thru like

$DB_CONNECTION_STRING="mongodb://user:password@164.152.09.84:27017"
$m = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING ) 

instead of

$m = new MongoClient()

Hope this helps someone

PS. Always say NO to semicolons, camelCAsE and anything case-sensitive... absurdity at its best.

Meryan
  • 1,285
  • 12
  • 25