Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No suitable servers found #133

Closed
prolic opened this issue Nov 4, 2015 · 34 comments
Closed

No suitable servers found #133

prolic opened this issue Nov 4, 2015 · 34 comments
Assignees

Comments

@prolic
Copy link

prolic commented Nov 4, 2015

on travis, see: https://travis-ci.org/prooph/event-store-mongodb-adapter/jobs/89299198

@prolic
Copy link
Author

prolic commented Nov 4, 2015

eventually related to: #112

@bjori
Copy link
Contributor

bjori commented Nov 4, 2015

Are you sure mongod is actually running?
Can you add something like the following line after the "mongod" launch, then we can confirm mongod is running properly:

mongo localhost:27017 --eval 'printjson(db.runCommand({"isMaster": 1}))'

@prolic
Copy link
Author

prolic commented Nov 4, 2015

I am pretty sure mongod is running, as it always was with the legacy driver. Let's see.

@prolic
Copy link
Author

prolic commented Nov 4, 2015

@bjori
Copy link
Contributor

bjori commented Nov 4, 2015

Edit:
You attempt to start mongod using:

 ${PWD}/mongodb-linux-x86_64-3.0.5/bin/mongod --dbpath /tmp/data --bind_ip 127.0.0.1 --noauth &> /dev/null &

So try, right after that:

${PWD}/mongodb-linux-x86_64-3.0.5/bin/mongo localhost:27017 --eval 'printjson(db.runCommand({"isMaster": 1}))'

@prolic
Copy link
Author

prolic commented Nov 4, 2015

is running:

connecting to: localhost:27017/test

{

    "ismaster" : true,

    "maxBsonObjectSize" : 16777216,

    "maxMessageSizeBytes" : 48000000,

    "maxWriteBatchSize" : 1000,

    "localTime" : ISODate("2015-11-04T23:08:01.148Z"),

    "maxWireVersion" : 3,

    "minWireVersion" : 0,

    "ok" : 1

}

however:

MongoDB\Driver\Exception\RuntimeException: No suitable servers found (serverselectiontryonce set): [connection timeout calling ismaster on 'localhost:27017']

see: https://travis-ci.org/prooph/event-store-mongodb-adapter/jobs/89335661

@gabrielalan
Copy link

I'm getting this error too, any solution? =(

@olekhy
Copy link

olekhy commented Nov 12, 2015

same mistake, I have to wait for a speedy problem-solving?

@jmikola
Copy link
Member

jmikola commented Nov 12, 2015

Is this issue only happening on Travis, or is the error easily reproducible on a development environment, too?

@jmikola
Copy link
Member

jmikola commented Nov 12, 2015

Reading prooph/event-store-mongodb-adapter's .travis.yml:

  • You're fetching, unpacking, and starting the MongoDB binaries in before_install
  • mongod is launched with & in that block

Comparing this with the .travis.yml file from mongo-php-library:

  • I'm installing our MongoDB enterprise packages, which are free for development use in before_install
  • In before_script, I use nc to check if anything is listening on port 27017. If not, I start it with service start
  • After ensuring the service is started, I could issue an isMaster command via the shell (as @bjori suggested), but I'm just printing mongod --version.

Since standardizing on this Travis configuration, I haven't experienced this exception (it does sound familiar from earlier builds we were doing over the summer, though).

@prolic
Copy link
Author

prolic commented Nov 12, 2015

Thanks for the input, will try this evening and let you know.
Am 12.11.2015 16:31 schrieb "Jeremy Mikola" notifications@github.com:

Reading prooph/event-store-mongodb-adapter's .travis.yml
https://github.com/prooph/event-store-mongodb-adapter/blob/master/.travis.yml
:

  • You're fetching, unpacking, and starting the MongoDB binaries in
    before_install
  • mongod is launched with & in that block

Comparing this with the .travis.yml
https://github.com/mongodb/mongo-php-library/blob/master/.travis.yml
file from mongo-php-library https://github.com/mongodb/mongo-php-library
:

  • I'm installing our MongoDB enterprise packages, which are free for
    development use in before_install
  • In before_script, I use nc to check if anything is listening on port
  • If not, I start it with service start
  • After ensuring the service is started, I could issue an isMaster
    command via the shell (as @bjori https://github.com/bjori
    suggested), but I'm just printing mongod --version.

Since standardizing on this Travis configuration, I haven't experienced
this exception (it does sound familiar from earlier builds we were doing
over the summer, though).


Reply to this email directly or view it on GitHub
#133 (comment)
.

@prolic
Copy link
Author

prolic commented Nov 12, 2015

I just compared my composer.json with yours and made some updates.

See: https://github.com/prolic/event-store-mongodb-adapter/blob/new_driver/.travis.yml

Still got the error:

  1. ProophTest\EventStore\Adapter\MongoDb\MongoDBEventStoreAdapterTest::it_creates_a_stream

MongoDB\Driver\Exception\RuntimeException: No suitable servers found (serverselectiontryonce set): [connection timeout calling ismaster on 'localhost:27017']

I don't have this problem on my local machine, currently it only comes up on travis.

Best.

@prolic
Copy link
Author

prolic commented Nov 12, 2015

@pulstar
Copy link

pulstar commented Dec 2, 2015

I am having the same problem. No idea what to do. I think it is a driver problem. I hope a new DLL can solve this. Maybe another combination of library or server version resolve this too, but this is the official driver for MongoDB 3.0.7/3.2. Well it is experimental yet, but I think it will unleash the full potential of MongoDB. Or maybe another userland library becomes the best off all soon... Who knows?

MongoDB\Driver\Exception\RuntimeException [ 13053 ]: No suitable servers found (serverselectiontryonce set): [connection error calling ismaster on 'localhost:27017']
MODPATH\mongodb\mongodb\src\Operation\Find.php [ 165 ]

http://php.net/set.mongodb

https://github.com/mongodb/mongo-php-library

@LCD344
Copy link

LCD344 commented Dec 6, 2015

I am having the same issue, running php 5.6, apache 2.4 on windows 8.1 -

Caught exception No suitable servers found (serverselectiontryonce set): [connection error calling ismaster on 'localhost:27017'], on line 64

this comes from trying to run the included example write.php.

I am sure mongodb is running as I'm using it with an unrelated nodejs project.

Did anyone manage to find a solution to this?

@pulstar
Copy link

pulstar commented Dec 9, 2015

I installed the MongoDB library using: composer require mongodb/mongodb

But when I try to do any operation, my Apache server crashes and I got this error:

[mpm_winnt:notice] [pid 7660:tid 580] AH00354: Child: Starting 150 worker threads.
ext\mongodb\src\libbson\src\bson\bson.c:1825 bson_init_static(): precondition failed: data
[Wed Dec 09 00:04:14.393554 2015] [mpm_winnt:crit] [pid 7344:tid 548] AH00427: Parent: child process 7660 exited with status 3 -- Aborting.

@bjori
Copy link
Contributor

bjori commented Dec 9, 2015

@pulstar You mind trying #152 ?
If that doesn't fix it for you, then please file a new ticket with example code how to reproduce it

@pulstar
Copy link

pulstar commented Dec 11, 2015

I installed the library using:
composer require "mongodb/mongodb"

But I realised that it installed the version 0.2.0...

So, I did try again with the beta version:
composer require "mongodb/mongodb=^1.0.0@beta"

But the same error occurs...
MongoDB\Driver\Exception\RuntimeException [ 13053 ]: No suitable servers found (serverselectiontryonceset): [connection error calling ismaster on 'localhost:27017']

It throws at: mongodb\mongodb\src\Operation\Find.php [ 165 ]:

160 */
161 public function execute(Server $server)
162 {
163 $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
164
165 return $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $readPreference);
166 }

So, I opened the MongoDB\Collection::find() method and print_r() some vars there:

$filter: Array()

$options: Array(
[readPreference] => MongoDB\Driver\ReadPreference Object (
[mode] => 1
[tags] => Array()
)
)

$operation: MongoDB\Operation\Find Object(
[databaseName:MongoDB\Operation\Find:private] => my_database
[collectionName:MongoDB\Operation\Find:private] => my_collection
[filter:MongoDB\Operation\Find:private] => Array()
[options:MongoDB\Operation\Find:private] => Array(
[readPreference] => MongoDB\Driver\ReadPreference Object(
[mode] => 1
[tags] => Array()
)
)
)

$server: MongoDB\Driver\Server Object

I have no idea what is wrong... Should this $server return anything? Maybe I am using an outdated DLL? My system config is a Windows 7 with XAMPP (PHP 5.6.12, 32-bits TS VC11).

@vchav73
Copy link

vchav73 commented Dec 11, 2015

I am seeing this problem on a freshly installed and updated CentOS 6.7 box with PHP 5.5.29, Pecl mongodb 1.0.1. I used the mongodb/mongodb composer package, trying both 1.0.0-beta1 and dev-master versions. I'm certain mongodb is running:

-bash-4.1$ mongo localhost:27017
MongoDB shell version: 3.0.5
connecting to: localhost:27017/test
> show dbs;
local  0.078GB
> 

As suggested in other comments the problem appears to be with getting the server. The code below outputs a zero element array:

require 'vendor/autoload.php';
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$servers = $manager->getServers();
var_dump($servers); 

@venatir
Copy link

venatir commented Dec 11, 2015

Same problem on OSX. Seems to be happening in this file in the library when it's calling the driver: vendor/mongodb/mongodb/src/Collection.php:517

$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

This seems critical to me. I don't see a work around for the moment and there are people there with production code depending on this.

@syntheticminds
Copy link

Same on the Laravel Homestead VM. To replicate, the after.sh script is as follows:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

sudo apt-get update
sudo apt-get install mongodb-org pkg-config libsasl2-dev -y

sudo pecl install mongodb

echo 'extension=mongodb.so' | sudo tee /etc/php5/mods-available/mongodb.ini
sudo ln -s /etc/php5/mods-available/mongodb.ini /etc/php5/fpm/conf.d/mongodb.ini
sudo ln -s /etc/php5/mods-available/mongodb.ini /etc/php5/cli/conf.d/mongodb.ini

sudo service php5-fpm restart

composer.json

"require": {
    "php": ">=5.5.9",
    "laravel/lumen-framework": "5.1.*",
    "vlucas/phpdotenv": "~1.0",
    "mongodb/mongodb": "^1.0.0@beta"
},

routes.php

$app->get('/', function () {
    $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    $servers = $manager->getServers();
    var_dump($servers);
});

The MongoDB shell works fine and show dbs lists the databases. However, the above route returns:

array (size=0)
  empty

@kornrunner
Copy link

I've had the same issue on my local setup and resolved it by changing localhost to 127.0.0.1. It might be related to mongod conf, but it used to work with localhost and old driver .

@syntheticminds
Copy link

Using $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017"); still gives the same result. I tried changing the bindIp in mongo.conf to 0.0.0.0 but that made no difference either. I may have to switch back to the old driver.

@bjori
Copy link
Contributor

bjori commented Dec 12, 2015

I managed to repro this using the Homested vm as @syntheticminds suggested.
The repro script:

<?php

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017/");
$command = new MongoDB\Driver\Command(array("buildinfo" => 1));

try {
    $cursor = $manager->executeCommand("admin", $command);
    $response = $cursor->toArray()[0];
} catch(MongoDB\Driver\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}
var_dump($response);

?>

It appears to be related to IPv6.. If there is an entry like the following in /etc/hosts

::1            localhost

I get

Fatal error: Uncaught exception 'MongoDB\Driver\Exception\RuntimeException' with message 'No suitable servers found (`serverselectiontryonce` set): [connection timeout calling ismaster on 'localhost:27017

But removing that entry, or changing the hostname to 127.0.0.1 like so:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017/");

and the script works as expected.

I then tried to reproduce this locally by adding the IPv6 entry to /etc/hosts and I can indeed reproduce the bug now fully.

I'm not sure how to fix this at the moment though, but the workaround is to not use localhost but 127.0.0.1 instead (or the actual IP).

Can you try the workaround again @syntheticminds ?

@pulstar
Copy link

pulstar commented Dec 13, 2015

I changed "localhost" to 127.0.0.1 and it is working fine now! Thank you @kornrunner! :)

But I'm still getting this error: ErrorException [ Fatal Error ]: Class 'MongoId' not found

By the way, is the MongoDB\BSON\ObjectID class the same as MongoId?

@bjori
Copy link
Contributor

bjori commented Dec 13, 2015

@pulstar yes, MongoID and MongoDB\BSON\ObjectID are more or less the same.

@yanshiba
Copy link

the reason is mongod server not support ipv6 by defualt, but the systemOS used ipv6.
you can delete the ipv6 on system:
remove this line
::1 localhost' from /etc/hosts

or, enable the mongod ipv6 support !
eg: ./bin/mongod --filePermissions 0777 --ipv6

此问题的原因在于:
操作系统开启了IPV6支持,但mongod未开启ipv6支持.

解决办法,有2个.
一种:
删除操作系统中ipv6解析
删除/etc/hosts'中的'::1 localhost, 这一行.

或者是:
开启mongod的ipv6支持,参数如下:
./bin/mongod --filePermissions 0777 --ipv6

@bjori
Copy link
Contributor

bjori commented Dec 13, 2015

I feel like I should have known this. Unsure what I should say other then to apologize.
I'm very sorry this has been a problem for so long without any explanation.

@yanshiba is absolute right. mongod does not support IPv6 connections by default.

His work workaround is spot on. Either remove the ::1 localhost from /etc/hosts or startup mongod with --ipv6.

The old driver did not have this problem as it didn't actually support IPv6 and therefore never tried to connect over IPv6.
The new driver however does however have full IPv6 support.

We should fix this somehow, defaulting to IPv4 or whatever. For now -- @yanshiba thank you so much for the help!

@jmikola
Copy link
Member

jmikola commented Dec 13, 2015

I've opened PHPLIB-151 and mongodb/mongo-php-library#60, which should at least change the default Client constructor argument.

@pulstar
Copy link

pulstar commented Dec 13, 2015

Thank you @bjori!

@syntheticminds
Copy link

@bjori Yes it works now. Thanks everyone. Great work!

bjori added a commit to bjori/mongo-php-driver that referenced this issue Dec 14, 2015
When doing async connections we can't properly determine if we
should connect over IPv6 or IPv4. PHP will unfortunately default
to connecting over IPv6 (when compiled in support, with minor
runtime check), while mongod does not support IPv6 by default.

When then creating the IPv6 connection asyncronously, PHP hits
EINPROGRESS and will happily return the socket as valid.

There currently is no way to configure these functions to prefer IPv4.
The best we can do for now is to not connect asyncronously as then
we'll properly discover that the IPv6 socket failed, and then continue
through the list of addrinfo and next pickup the IPv4 address which
works.
@bjori bjori closed this as completed in f912475 Dec 14, 2015
@djordn
Copy link

djordn commented Aug 18, 2017

For me, this fixed:

#374 (comment)

@Yogesh9900
Copy link

Yogesh9900 commented Nov 22, 2019

I had this issue:
Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set)

I searched a lot but did not get any solution..
using macOs catalina , php 7.2

after I opened terminal and run command

mongod --dbpath Users/username/data/db

--dbpath is your /data/db path

and my code is working properly..

@sasahou
Copy link

sasahou commented Sep 29, 2022

check connect options, remove 'replicaSet'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests