mongodb / mongo-php-library

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

No suitable servers found (`serverSelectionTryOnce` set): [connection timeout calling ismaster on '127.0.0.1:27017'] 500 Internal Server Error - MongoConnectionException #702

Closed nadiahareb closed 4 years ago

nadiahareb commented 4 years ago

Im using alcaeus adapter in my application already built in ext-mongo .

mongodb/mongodb (1.2): PHP Version 7.1.33

MongoDB extension version 1.2.8
MongoDB extension stability stable
libbson bundled version 1.5.5
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 enabled

Stack Trace (Plain Text) - [1] MongoConnectionException: No suitable servers found (serverSelectionTryOnce set): [connection timeout calling ismaster on '127.0.0.1:27017'] at n/a in C:\xampp\htdocs\SSS\vendor\alcaeus\mongo-php-adapter\lib\Alcaeus\MongoDbAdapter\ExceptionConverter.php line 85

at Alcaeus\MongoDbAdapter\ExceptionConverter::toLegacy(object(ConnectionTimeoutException), 'MongoResultException')
    in C:\xampp\htdocs\SSS\vendor\alcaeus\mongo-php-adapter\lib\Mongo\MongoCollection.php line 349

at MongoCollection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Collection.php line 1005

at Doctrine\MongoDB\Collection->doBatchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Collection.php line 141

at Doctrine\MongoDB\Collection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Traits\LoggableCollectionTrait.php line 61

at Doctrine\MongoDB\LoggableCollection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Persisters\DocumentPersister.php line 261

at Doctrine\ODM\MongoDB\Persisters\DocumentPersister->executeInserts(array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\UnitOfWork.php line 1149

at Doctrine\ODM\MongoDB\UnitOfWork->executeInserts(object(ClassMetadata), array('0000000079770bf50000000033fb7bcd' => object(Ascenseur)), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\UnitOfWork.php line 444

at Doctrine\ODM\MongoDB\UnitOfWork->commit(null, array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\DocumentManager.php line 544

at Doctrine\ODM\MongoDB\DocumentManager->flush()
    in C:\xampp\htdocs\SSS\src\AppBundle\Controller\DefaultController.php line 55

at AppBundle\Controller\DefaultController->indexAction(object(Request))
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3275

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3234

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3388

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 2594

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in C:\xampp\htdocs\SSS\web\app.php line 15

[2] MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set): [connection timeout calling ismaster on '127.0.0.1:27017'] at n/a in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Collection.php line 726

at MongoDB\Driver\Manager->selectServer(object(ReadPreference))
    in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Collection.php line 726

at MongoDB\Collection->insertMany(array(object(BSONDocument)), array('writeConcern' => object(WriteConcern)))
    in C:\xampp\htdocs\SSS\vendor\alcaeus\mongo-php-adapter\lib\Mongo\MongoCollection.php line 346

at MongoCollection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Collection.php line 1005

at Doctrine\MongoDB\Collection->doBatchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Collection.php line 141

at Doctrine\MongoDB\Collection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb\lib\Doctrine\MongoDB\Traits\LoggableCollectionTrait.php line 61

at Doctrine\MongoDB\LoggableCollection->batchInsert(array('0000000079770bf50000000033fb7bcd' => array('_id' => object(MongoId), 'localisation' => array('type' => 'Point', 'coordinates' => array('x' => '2.3530049', 'y' => '48.8934163')), 'adresse' => '102 rue des Poissonniers', 'codePostal' => '75018', 'commune' => 'Paris', 'emplacement' => 'Batiment A Ascenseur de droite', 'reference' => '123456789', 'constructeurNom' => 'Otis', 'etageMin' => '-2', 'etageMax' => '9', 'telephoneDepannage' => '0102030405', 'statut' => 'EN_PANNE', 'dateStatut' => object(MongoDate), 'dateConstruction' => object(MongoDate), 'dateRenovation' => object(MongoDate), 'syndicNom' => 'Syndic', 'bailleurNom' => 'Bailleur', 'ascensoristeNom' => 'Ascensoriste', 'updatedAt' => object(MongoDate))), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Persisters\DocumentPersister.php line 261

at Doctrine\ODM\MongoDB\Persisters\DocumentPersister->executeInserts(array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\UnitOfWork.php line 1149

at Doctrine\ODM\MongoDB\UnitOfWork->executeInserts(object(ClassMetadata), array('0000000079770bf50000000033fb7bcd' => object(Ascenseur)), array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\UnitOfWork.php line 444

at Doctrine\ODM\MongoDB\UnitOfWork->commit(null, array())
    in C:\xampp\htdocs\SSS\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\DocumentManager.php line 544

at Doctrine\ODM\MongoDB\DocumentManager->flush()
    in C:\xampp\htdocs\SSS\src\AppBundle\Controller\DefaultController.php line 55

at AppBundle\Controller\DefaultController->indexAction(object(Request))
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3275

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3234

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3388

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 2594

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in C:\xampp\htdocs\SSS\web\app.php line 15
jmikola commented 4 years ago

I most recently broke down the structure of the server selection error message in this thread, so I'd suggest reading that comment for a more detail explanation. In your case, the root error for failing to select a server is a "connection timeout" attempting to reach the server on 127.0.0.1:27017. This could either mean that no mongod process is listening on that host/port or that your PHP environment is not able to make an outgoing connection. Both of those would likely fall outside of the driver itself.

I'd suggest attempting to reproduce the error with the most minimal example, such as connecting and executing a ping command. We have an example in the library docs that demonstrates how to do so. Two things to note with that example are:

One other alternative is that you can attempt to connect to the mongod host via the HTTP protocol. If you search this repository's issues for file_get_contents you should come up with several old comments where I advised users to do this simply to verify that PHP is able to make an outgoing connection. https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-306801722 is one such example.

nadiahareb commented 4 years ago

thank you very much for your reply and for your time , in attempt to execute this : $database = (new \MongoDB\Client)->test;

$cursor = $database->command(['ping' => 1]);

var_dump($cursor->toArray()[0]);

i get this error

MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set): [connection timeout calling ismaster on '127.0.0.1:27017'] at n/a in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Database.php line 176

at MongoDB\Driver\Manager->selectServer(object(ReadPreference))
    in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Database.php line 176

at MongoDB\Database->command(array('ping' => '1'))
    in C:\xampp\htdocs\SSS\src\AppBundle\Controller\DefaultController.php line 64

at AppBundle\Controller\DefaultController->indexAction(object(Request))
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3275

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3234

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3388

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
    in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 2594

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in C:\xampp\htdocs\SSS\web\app.php line 15 
nadiahareb commented 4 years ago

I get the same error when i try to execute this $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017/"); $rp = new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY); $server = $manager->selectServer($rp); var_dump($server->getHost());

nadiahareb commented 4 years ago

thank you very much for your reply and for your time , in attempt to execute this : $database = (new \MongoDB\Client)->test;

$cursor = $database->command(['ping' => 1]);

var_dump($cursor->toArray()[0]);

i get this error

MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set): [connection timeout calling ismaster on '127.0.0.1:27017'] at n/a in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Database.php line 176

at MongoDB\Driver\Manager->selectServer(object(ReadPreference)) in C:\xampp\htdocs\SSS\vendor\mongodb\mongodb\src\Database.php line 176

at MongoDB\Database->command(array('ping' => '1')) in C:\xampp\htdocs\SSS\src\AppBundle\Controller\DefaultController.php line 64

at AppBundle\Controller\DefaultController->indexAction(object(Request)) in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3275

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1') in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3234

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true) in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 3388

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true) in C:\xampp\htdocs\SSS\app\bootstrap.php.cache line 2594

at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) in C:\xampp\htdocs\SSS\web\app.php line 15

2019-12-25 18:25 UTC+01:00, Jeremy Mikola notifications@github.com:

I most recently broke down the structure of the server selection error message in this thread, so I'd suggest reading that comment for a more detail explanation. In your case, the root error for failing to select a server is a "connection timeout" attempting to reach the server on 127.0.0.1:27017. This could either mean that no mongod process is listening on that host/port or that your PHP environment is not able to make an outgoing connection. Both of those would likely fall outside of the driver itself.

I'd suggest attempting to reproduce the error with the most minimal example, such as connecting and executing a ping command. We have an example in the library docs that demonstrates how to do so. Two things to note with that example are:

  • You'll need to ensure the library classes are available by including Composer's autoloader. We omit that from the doc examples to keep them concise.
  • You may want to construct the MongoDB\Client object explicitly (see: MongoDB\Client::__construct()) instead of doing so with no arguments as we have in our examples (again, for conciseness).

One other alternative is that you can attempt to connect to the mongod host via the HTTP protocol. If you search this repository's issues for file_get_contents you should come up with several old comments where I advised users to do this simply to verify that PHP is able to make an outgoing connection. https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-306801722 is one such example.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/mongodb/mongo-php-library/issues/702#issuecomment-568917694

-- Cordialement, -- Nadia HAREB Ingénieur chercheur Ecole Nationale des Sciences de l'Informatique Laboratoire CRISTAL - Pôle RAMSIS

nadiahareb commented 4 years ago

when i try this file_get_contents('http://127.0.0.1:27017') ; Iget this error

Warning: file_get_contents(http://127.0.0.1:27017): failed to open stream: Aucune connexion n�a pu �tre �tablie car l�ordinateur cible l�a express�ment refus�e. 500 Internal Server Error - ContextErrorException

Please helpp

jmikola commented 4 years ago

Given that the file_get_contents() approach was also unsuccessful, I think one of two things is happening:

Both of these are beyond the driver/library, so you'll likely need to take your question to a separate forum.

To confirm that the mongod process is running properly, you can attempt connecting to it with the mongo shell. If that is unsuccessful, you can review the MongoDB Windows installation docs and/or take this issue to one of the MongoDB community forums. Otherwise, if this is likely a PHP environment issue, I think you should inquire in some PHP community resource, such as Stack Overflow or the XAMPP forums (if you are indeed using XAMPP as your pasted errors suggest).

nadiahareb commented 4 years ago

thank you very much for your reply , actually im running it in windowws 10, but when i deplay the application on OVH (i use exactly the same environnement php 7.1 and mongodb 1.2.8) i get exactly the same error, please how can i confirm that the mongod process is running on my local envirement windows 10

jmikola commented 4 years ago

please how can i confirm that the mongod process is running on my local envirement windows 10

In my last comment, I suggested you use the mongo shell, which ships with the MongoDB server, to verify that you can connect to the server. I also linked the Windows installation docs so you can confirm that the server is properly installed.

This issue does not appear to be directly related to the PHP driver or library, so I am going to close it and request that you consult one of the support channels I referenced in my previous reply for more general support with MongoDB or your PHP environment.

marcnewton commented 4 years ago

SELinux (CentOS7/8) can cause this error, applying the following rule may resolve it:

setsebool -P httpd_can_network_connect_db 1