MongoDB no suitable servers found
Asked Answered
E

5

12

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

Erikaerikson answered 13/1, 2017 at 22:23 Comment(2)
kindly, make sure that bindIp in /etc/mongod.conf is correctly added.Scripture
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 ...Jetblack
S
15

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

Season answered 30/1, 2017 at 17:0 Comment(2)
I've used both localhost vs. 127.0.0.1 also but its not working with windows 10 system. After windows update its not working properly.Jeavons
@shankar msr I've added some additional notes that may help you.Season
A
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.

Ansell answered 1/7, 2019 at 21:7 Comment(0)
M
4

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.

Mitzi answered 10/3, 2021 at 3:39 Comment(0)
M
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:[email protected]: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.

Malloy answered 12/6, 2020 at 5:20 Comment(0)
W
0

How To make Network Access go to mongodb atlas -> go to database -> Network Access ->IP Access List-> create ip It will suggest that you create an IP for your device only, but if there is more than one person, you can add your own IP. The device will suggest that you initially make the IP public and anyone can enter. If you work correctly, you can specify your IP or make it public.

Wynny answered 21/2 at 16:59 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Wilkins

© 2022 - 2024 — McMap. All rights reserved.