mongodb / mongo-php-library

The Official MongoDB PHP library
https://mongodb.com/docs/php-library/current/
Apache License 2.0
1.59k stars 262 forks source link

No suitable servers found,connection closed calling ismaster on #372

Closed ms100 closed 6 years ago

ms100 commented 7 years ago

err_no:13053 err_msg:No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on '10.9.10.6:27017']

php run on 10.9.10.2 mongodb/mongodb version: 1.1.2,required by composer

jmikola commented 7 years ago

This error indicates a connectivity issue between the driver and server. If the error is persistent, I would suggest starting by attempting to connect to the same MongoDB server using the mongo shell or another language driver (from the same host as your PHP application server).

ms100 commented 7 years ago

I try mongo shell with the same host user password, no problem; but driver error is persistent.

jmikola commented 7 years ago

Please try all of the following:

If you were running the script through a web server above, attempt to run it through the CLI environment.

Enable mongodb.debug for the script that generates this connection error. You can use ini_set() before constructing a MongoDB\Driver\Manager if you'd rather not declare this in php.ini and have it affect everything. If you're using a web server, I'd suggest configuring the option to point to a directory where it can create debug traces. For a CLI environment, "stderr" should suffice.

Lastly, attempt to connect to the MongoDB server using file_get_contents(). For example, file_get_contents('http://localhost:27017') should return the string "It looks like you are trying to access MongoDB over HTTP on the native driver port." Since this bypasses the driver entirely, it can test whether some external factor is preventing PHP from communicating with the database server (e.g. web server or firewall configuration).

ManuelMolina97 commented 7 years ago

Any news? I have also this problem.

PHP version: 7.0.19-1. MongoDB version: 3.2.11. Mongo-php-library version: 1.1.2.

I've traid what you said about connecting it, at first I had some problems, exactly this one, and I solved it as it shows in the answer.

But the problem still persists.

I've also tried with several different versions (dev-master, 1.2.0-alpha1) but it is still the same.

And when I try to attempt using file_get_contents(), it shows the message you said. "It looks like you are trying to access MongoDB over HTTP on the native driver port."

ManuelMolina97 commented 6 years ago

Any news on this?

I always have this problem when I have a vast amount of incoming requests, at the beginning it works but at some point it cannot connect any longer.

lbuttars commented 6 years ago

I have the same problem

After about a weak of using this driver, I get this error.

Every week I need to restart the Apache process.

Restarting apache fixes it, but that's not a valid solution to this reoccurring problem.

Could this be a memory or connection persistence problem?

This is making mongo unusable for me.

MongoDB\Driver\Exception\ConnectionTimeoutException Object ( [message:protected] => No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on '10.x.x.99:27017'] [string:Exception:private] =>

PHP 7.0.10 Mongod 3.2.17

httpd -v Server version: Apache/2.2.15 (Unix) Server built: Aug 15 2017 19:44:58

php -v PHP 7.0.10 (cli) (built: Nov 3 2016 08:05:22) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

php -i | grep mongo /etc/php.d/mongodb.ini, mongodb libmongoc bundled version => 1.5.5 libmongoc SSL => enabled libmongoc SSL library => OpenSSL libmongoc crypto => enabled libmongoc crypto library => libcrypto libmongoc crypto system profile => disabled libmongoc SASL => disabled mongodb.debug => no value => no value

MongoDB\Driver\Exception\ConnectionTimeoutException Object ( [message:protected] => No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on '10.x.x.99:27017'] [string:Exception:private] =>

jmikola commented 6 years ago

@ManuelMolina97: I've also tried with several different versions (dev-master, 1.2.0-alpha1) but it is still the same.

Note that the PHP library only provides high-level APIs around the driver (mongodb extension). The driver and libmongoc are responsible for actually connecting to the server, so their versions would be more relevant here.

@ManuelMolina97: I always have this problem when I have a vast amount of incoming requests, at the beginning it works but at some point it cannot connect any longer.

Is there some notable event on the server/cluster (e.g. replica set failover, restarting a node) after which the driver stops being able to connect? If you have many requests, I assume they may be served across a large pool of PHP workers and/or application servers. In that case, the default serverSelectionTryOnce=true behavior of the driver might see each encounter a network error after such an event. I would then expect the subsequent request served by each worker to recover.

Experimenting with serverSelectionTryOnce=false and the serverSelectionTimeoutMS client options would allow the driver to be more resilient in the event of dropped connections at the expense of additional time spent monitoring the cluster and selecting a server for database operations.

@lbuttars: Restarting apache fixes it, but that's not a valid solution to this reoccurring problem.

I'd also be curious to hear if there is some state change outside of the application server responsible for this. Is there any evidence of the mongod nodes dropping their connections through a restart or replica set failover? It also sounds as if serverSelectionTryOnce=false might help in this case.


A common element missing from both of your reports is information on what the server is doing. Having some idea of whether there is a transient network error (e.g. replica set failover) or some persistent network outage is useful to understand how and why the driver is throwing its error. Correlating driver exceptions with logs from the server (or some monitoring tools watching mongod) would help quite a bit.

Also, please read https://github.com/mongodb/mongo-php-driver/issues/660#issuecomment-335484565 for a discussion on how the driver performs server selection (which leads to the exceptions you're seeing) and the impact of the serverSelectionTryOnce and serverSelectionTimeoutMS options. I ultimately hope to port that over to a page in the driver/library documentation.

kahovn commented 6 years ago

@jmikola you don't have any solution to resolve this problem

jmikola commented 6 years ago

@kahovn: Please keep the conversation to PHPC-1025, which I moved over from the original issue you opened on the SERVER project. "No suitable servers found" is a very generic error that only implies that the driver was unable to select a server based on its view of the topology. The actual cause for that failure can be very different case to case.

jsy135135 commented 6 years ago

if you use it in linux。Maybe is selinux. you can using "setenforce 0" in shell commond. then, test your code. if working,you can disenable selinux in /etc/sysconfig/selinux. it is my solution to relolve this problem when i last time

jmikola commented 6 years ago

Closing this as there has been no response to https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-338232369 in some time and there's no indication that the driver is behaving incorrectly here. Feel free to follow up if there are new developments.

I'll also note that CDRIVER-2075 implemented retry logic for isMaster commands in libmongoc 1.7.0, which is included in ext-mongodb 1.3.0 and later. That would potentially alleviate the original error reported by the OP.

abinhho commented 6 years ago

I got same problem, 10 times connection got 8 timeout, It's mean Im connected successful 2 times.

I am using:

PHP 7.0.30
MongoDB Atlas Free Ties 512MB

Please help !

jmikola commented 6 years ago

@abinhho: I noticed you also opened https://github.com/mongodb/mongo-php-driver/issues/855. Rather than add replies to closed issues, consider linking to this thread from your new issue if you feel it adds context.

bujardeari commented 5 years ago

I have the same issue , trying different things in 3 days with no success. With my local docker env it works evereytime, but with all my other kuberentes environments it randomly works or not. Is there any hope for this ?

jmikola commented 5 years ago

@bujardeari: See my follow-up in https://github.com/mongodb/mongo-php-library/issues/546#issuecomment-482139743. In the future, please create a new issue instead of following up on closed threads.

trustit-developement commented 4 years ago

I'm having the same issue, lost an entire day trying everything... php 7.0.33 mongodb 1.5.2 running in Laravel homestead / vagrant

this is the code:

$url = 'mongodb+srv://teste:teste@applications-jujzk.mongodb.net/test?retryWrites=true&w=majority'; $client = new MongoDB\Client($url); dd($client->listDatabases());

if i try connecting to "mongodb://localhost:27017" it works fine, but i can't connect to AWS DocumentDB or Atlas.

i also tried a lot of strings like $url = 'mongodb+srv://teste:teste@applications-jujzk.mongodb.net/test?ssl=true&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=1500 0&w=majority"';

this strings work fin in Compass, but in PHP...

a No suitable servers found (serverSelectionTryOnceset) its all i accomplished...

help plz.

jmikola commented 4 years ago

@trustit-developement: Please open a new issue instead of appending to a closed thread. Additionally, an exception with "No suitable servers found (serverSelectionTryOnce set)" should include more information regarding specific connection errors that lead to failed selection (see https://github.com/mongodb/mongo-php-library/issues/697). Please share the entire exception message in your report. Additionally, it wouldn't hurt to see a backtrace, phpinfo() output, and other information that is requested in the "Bug Report" template for new issues.