Hi,
Let me repost the problem.
Hope someone can help me resolve this issue with connecting to MongoDB using PHP via http.
Thanks!
DH
The basic setup:
CentOS 7 (release 7.7.1908)
MongoDB v4.2.5, new installation, no authentication.
PHP version 7.4.5
Driver install via ‘pecl install mongodb’
Seeing this error:
No suitable servers found (serverSelectionTryOnce
set): [connection refused calling ismaster on ‘127.0.0.1:27017’]
Is there a solution to this problem that I’m not aware of? Is there some mismatch versions of dependent modules or libraries?
Please see more info below. The driver seems to be installed correctly, since the PHP standalone command works but via HTTP, it does not work. And the var_dump shows MongoDB\Client ok on either way.
Thanks for any help.
Here’s a simple PHP test script:
$ cat testmongo.php
<?php
require_once "vendor/autoload.php";
try {
$mc = new MongoDB\Client('mongodb://127.0.0.1/');
print_r($mc);
$dbs = $mc->listDatabases();
print_r($dbs);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
PROBLEM connecting to the database via HTTP (using curl):
$ curl http://localhost/testmongo.php
MongoDB\Client Object
(
[manager] => MongoDB\Driver\Manager Object
(
[uri] => mongodb://127.0.0.1/
[cluster] => Array
(
)
)
[uri] => mongodb://127.0.0.1/
[typeMap] => Array
(
[array] => MongoDB\Model\BSONArray
[document] => MongoDB\Model\BSONDocument
[root] => MongoDB\Model\BSONDocument
)
[writeConcern] => MongoDB\Driver\WriteConcern Object
(
)
)
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '127.0.0.1:27017']
But it works in PHP command mode (not sure why or what’s the difference than previous?):
$ php -f testmongo.php
MongoDB\Client Object
(
[manager] => MongoDB\Driver\Manager Object
(
[uri] => mongodb://127.0.0.1/
[cluster] => Array
(
)
)
[uri] => mongodb://127.0.0.1/
[typeMap] => Array
(
[array] => MongoDB\Model\BSONArray
[document] => MongoDB\Model\BSONDocument
[root] => MongoDB\Model\BSONDocument
)
[writeConcern] => MongoDB\Driver\WriteConcern Object
(
)
)
MongoDB\Model\DatabaseInfoLegacyIterator Object
(
[databases:MongoDB\Model\DatabaseInfoLegacyIterator:private] => Array
(
[0] => Array
(
[name] => admin
[sizeOnDisk] => 135168
[empty] =>
)
[1] => Array
(
[name] => config
[sizeOnDisk] => 49152
[empty] =>
)
[2] => Array
(
[name] => local
[sizeOnDisk] => 73728
[empty] =>
)
)
)
More info:
$ mongo localhost:27017 --eval 'printjson(db.runCommand({"isMaster": 1}))'
MongoDB shell version v4.2.5
connecting to: mongodb://localhost:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("144e5989-b8ad-47ff-a8aa-bd5ccf8ba10b") }
MongoDB server version: 4.2.5
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-04-25T04:29:36.869Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 7,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1
}
$php --ri mongodb
mongodb
MongoDB support => enabled
MongoDB extension version => 1.7.4
MongoDB extension stability => stable
libbson bundled version => 1.16.2
libmongoc bundled version => 1.16.2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc ICU => enabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongocrypt bundled version => 1.0.3
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
Directive => Local Value => Master Value
mongodb.debug => no value => no value
$ composer show mongodb/mongodb
name : mongodb/mongodb
descrip. : MongoDB driver library
keywords : database, driver, mongodb, persistence
versions : * 1.6.0
type : library
license : Apache License 2.0 (Apache-2.0) (OSI approved) https://spdx.org/licenses/Apache-2.0.html#licenseText
homepage : https://jira.mongodb.org/browse/PHPLIB
source : [git] https://github.com/mongodb/mongo-php-library.git dc43ba25fb593d6a2988e6a535b6f5386eda5b15
dist : [zip] https://api.github.com/repos/mongodb/mongo-php-library/zipball/dc43ba25fb593d6a2988e6a535b6f5386eda5b15 dc43ba25fb593d6a2988e6a535b6f5386eda5b15
path : /var/www/html/vendor/mongodb/mongodb
names : mongodb/mongodb
autoload
psr-4
MongoDB\ => src/
files
requires
ext-hash *
ext-json *
ext-mongodb ^1.7
php ^5.6 || ^7.0
requires (dev)
phpunit/phpunit ^5.7.27 || ^6.4 || ^8.3
sebastian/comparator ^1.0 || ^2.0 || ^3.0
squizlabs/php_codesniffer ^3.4
symfony/phpunit-bridge ^4.4@dev