mongodb / mongo-php-driver

The Official MongoDB PHP driver
https://pecl.php.net/package/mongodb
Apache License 2.0
889 stars 204 forks source link

No suitable servers found #133

Closed prolic closed 8 years ago

prolic commented 9 years ago

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

prolic commented 9 years ago

eventually related to: https://github.com/mongodb/mongo-php-driver/issues/112

bjori commented 9 years ago

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 commented 9 years ago

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

prolic commented 9 years ago

ups, command not found?? https://travis-ci.org/prooph/event-store-mongodb-adapter/jobs/89329883

bjori commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

olekhy commented 9 years ago

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

jmikola commented 9 years ago

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

jmikola commented 9 years ago

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

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

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 commented 9 years ago

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 https://github.com/mongodb/mongo-php-driver/issues/133#issuecomment-156157541 .

prolic commented 9 years ago

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 commented 9 years ago

Current Travis Build here: https://travis-ci.org/prooph/event-store-mongodb-adapter/builds/90772042

pulstar commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@pulstar You mind trying https://github.com/mongodb/mongo-php-driver/pull/152 ? If that doesn't fix it for you, then please file a new ticket with example code how to reproduce it

pulstar commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

yanshiba commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

pulstar commented 8 years ago

Thank you @bjori!

syntheticminds commented 8 years ago

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

djordn commented 7 years ago

For me, this fixed:

https://github.com/mongodb/mongo-php-driver/issues/374#issuecomment-302413456

Yogesh9900 commented 4 years ago

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 commented 2 years ago

check connect options, remove 'replicaSet'