PHP error: No suitable servers found

Hi,

I’m trying out PHP driver for the first time and run into this error. I’ve installed MongoDB extension version 1.7.4. It seems the connection to MongoDB is ok (i.e. not sure if a connection is really made from MongoDB\Client), but the call to listDatabases() failed.
Am I missing something? Please help!

Btw, I’ve been using PyMongo and it’s working fine, just couldn’t get PHP to work.

Here’s the simple code (security not enabled):

<?php
require 'vendor/autoload.php';
try {
    $mc = new MongoDB\Client('mongodb://localhost/');
    echo "<p>ClientOK</p>";
    $dbs = $mc->listDatabases();
    echo "<p>List</p>";
} catch ( **Exception** $ **e** ) {
    echo "Exception: ", $e->getMessage(), "\n";
}

Here’s the output:

Client OK
Exception: No suitable servers found (serverSelectionTryOnce set): [connection refused calling ismaster on ‘localhost:27017’]

Additional information:

Here’s a snippet from phpinfo():

$ php -i | grep -i mongo
MongoDB support => enabled
MongoDB extension version => 1.7.4
MongoDB extension stability => stable
libmongoc bundled version => 1.16.2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => enabled
libmongoc SASL => enabled
libmongoc ICU => enabled
libmongoc compression => enabled
libmongoc compression snappy => enabled
libmongoc compression zlib => enabled
libmongocrypt bundled version => 1.0.3
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
mongodb.debug => /var/log/php-mongodb.debug.log => /var/log/php-mongodb.debug.log

Any help from the MongoDB team on PHP driver?
Do you need any info from me?

Btw, I had worked on a custom filesystem using WiredTiger and Keith Bostic was very helpful a few years back. See https://github.com/deyohong/UNFS.

The error message suggests that the driver can’t connect to a MongoDB instance locally, indicating that it isn’t running or isn’t accepting any connections. Please confirm that you can connect to that address via another method (e.g. mongo shell) and retry.

If this doesn’t work, enabling the debug log will help with diagnosing where exactly this goes wrong. To do so, you can change the mongodb.debug INI setting. Please make sure to redact any sensitive information (passwords, documents) contained in the debug log before sharing it in a public or semi-public forum.

1 Like

Thanks Andreas for responding.

As mentioned, mongo shell and pymongo are working fine. As a matter of fact, running php as command line also works:

$ php -f testdb.php   # this works

But if I invoke it via the local apache server then it doesn’t connect:

$ curl http://localhost/testdb.php   # DB connection not working as shown in this thread

I’ve also set mongodb.debug pointing it to /tmp/debug.log but nothing is shown there.

Whenever I run ‘php -i’, I got this annoying error:
PHP Warning: Module ‘mongodb’ already loaded in Unknown on line 0
PHP Notice: PHP Startup: file created in the system’s temporary directory in Unknown on line 0

When running from command line, I could see a connection is made from /var/log/mongodb/mong.log, but when invoking from http connection, nothing is happening in that log.

What am I missing?

Thanks,
DH

I figured out the annoying warnings and errors coming from ‘php -i’ command. It’s because ‘extension=mongodb.so’ is specified in both /etc/php.ini and /etc/php.d/50-mongodb.ini, so it tries to load twice. I’m not sure where mongodb.debug can be set as. I thought its value would be a filepath but setting it as something like ’ mongodb.debug=/tmp/debug.log’ gives it the PHP Notice error. I changed it to ‘mongodb.debug=stderr’ and got the debug messages when running ‘php -f testdb.php’ from command line.

I still don’t get anything (from mongod or debug logs) at all when running ‘curl http://localhost/testdb.php’ Note that it’s the same testdb.php file.

I appreciate any help here.

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

Good to know you figured out the warnings yourself.

The notice about a file being created in the system’s temporary directory is due to you specifying a non-existent directory in the mongodb.debug INI setting. Please note that this setting takes a directory or stream (e.g. stdout or stderr, as you’ve discovered). If using a directory, please make sure the directory exists. The driver will create a new file in that directory (IIRC it creates one per CLI call and one for each FPM process that is spawned, but please don’t quote me on that).

From the error description it looks like the connection can’t even be opened when accessing the script through HTTP, which is definitely curious. A debug log will help us figure out where this fails, which hopefully points us in the right direction.

Please also allow for some time between responses, especially on weekends. Thank you for your patience.

1 Like

If set mongodb.debug=/tmp:

  • Running ‘php -f testmongo.php’ produces /tmp/PHONGO-* debug file.
  • Running ‘curl http://localhost/testmongo.php’ produces nothing in /tmp directory.

If set mongodb.debug=stderr:

  • Running ‘php -f testmongo.php’ produces many debug outputs in stderr (similar to the PHONGO file output).
  • Running ‘curl http://localhost/testmongo.php’ produces nothing on stderr, but the /var/log/httpd/error_log produces these messages:
[2020-04-28T07:37:43.805231+00:00]     PHONGO: DEBUG   > Connection string: 'mongodb://127.0.0.1/'
[2020-04-28T07:37:43.805289+00:00]     PHONGO: DEBUG   > Creating Manager, phongo-1.7.4[stable] - mongoc-1.16.2(bundled), libbson-1.16.2(bundled), php-7.4.5
[2020-04-28T07:37:43.805306+00:00]     mongoc: TRACE   > ENTRY: mongoc_topology_description_init():75
[2020-04-28T07:37:43.805329+00:00]     mongoc: TRACE   >  EXIT: mongoc_topology_description_init():94
[2020-04-28T07:37:43.805366+00:00]     mongoc: TRACE   > ENTRY: mongoc_server_description_init():115
[2020-04-28T07:37:43.805375+00:00]     mongoc: TRACE   >  EXIT: mongoc_server_description_init():139
[2020-04-28T07:37:43.805388+00:00]    cluster: TRACE   > ENTRY: mongoc_cluster_init():2147
[2020-04-28T07:37:43.805401+00:00]    cluster: TRACE   >  EXIT: mongoc_cluster_init():2174
[2020-04-28T07:37:43.805407+00:00]     PHONGO: DEBUG   > Created client hash: a:4:{s:3:"pid";i:19217;s:3:"uri";s:20:"mongodb://127.0.0.1/";s:7:"options";a:0:{}s:13:"driverOptions";a:0:{}}

[2020-04-28T07:37:43.805731+00:00] topology_scanner: TRACE   > ENTRY: mongoc_topology_scanner_node_setup_tcp():661
[2020-04-28T07:37:43.805774+00:00] topology_scanner: TRACE   >  EXIT: mongoc_topology_scanner_node_setup_tcp():709
[2020-04-28T07:37:43.805783+00:00]     socket: TRACE   > ENTRY: mongoc_socket_new():997
[2020-04-28T07:37:43.805812+00:00]     socket: TRACE   > ENTRY: _mongoc_socket_setnodelay():570
[2020-04-28T07:37:43.805821+00:00]     socket: TRACE   >  EXIT: _mongoc_socket_setnodelay():582
[2020-04-28T07:37:43.805827+00:00]     socket: TRACE   > ENTRY: _mongoc_socket_setkeepalive():535
[2020-04-28T07:37:43.805834+00:00]     socket: TRACE   > TRACE: _mongoc_socket_setkeepalive():539 Setting SO_KEEPALIVE
[2020-04-28T07:37:43.805842+00:00]     socket: TRACE   > TRACE: _mongoc_socket_set_sockopt_if_less():482 'TCP_KEEPIDLE' is 7200, target value is 300
[2020-04-28T07:37:43.805850+00:00]     socket: TRACE   > TRACE: _mongoc_socket_set_sockopt_if_less():493 'TCP_KEEPIDLE' value changed to 300
[2020-04-28T07:37:43.805857+00:00]     socket: TRACE   > TRACE: _mongoc_socket_set_sockopt_if_less():482 'TCP_KEEPINTVL' is 75, target value is 10
[2020-04-28T07:37:43.805864+00:00]     socket: TRACE   > TRACE: _mongoc_socket_set_sockopt_if_less():493 'TCP_KEEPINTVL' value changed to 10
[2020-04-28T07:37:43.805871+00:00]     socket: TRACE   > TRACE: _mongoc_socket_set_sockopt_if_less():482 'TCP_KEEPCNT' is 9, target value is 9
[2020-04-28T07:37:43.805876+00:00]     socket: TRACE   >  EXIT: _mongoc_socket_setkeepalive():551
[2020-04-28T07:37:43.805880+00:00]     socket: TRACE   >  EXIT: mongoc_socket_new():1029
[2020-04-28T07:37:43.805885+00:00]     socket: TRACE   > ENTRY: mongoc_socket_connect():860
[2020-04-28T07:37:43.805985+00:00]     socket: TRACE   > TRACE: _mongoc_socket_capture_errno():68 setting errno: 13 Permission denied
[2020-04-28T07:37:43.805997+00:00]     socket: TRACE   > TRACE: _mongoc_socket_errno_is_again():631 errno is: 13
[2020-04-28T07:37:43.806014+00:00]     socket: TRACE   >  EXIT: mongoc_socket_connect():892
[2020-04-28T07:37:43.806035+00:00]     stream: TRACE   > ENTRY: _mongoc_stream_socket_poll():227
[2020-04-28T07:37:43.806040+00:00]     socket: TRACE   > ENTRY: mongoc_socket_poll():297
[2020-04-28T07:37:43.806051+00:00]     stream: TRACE   >  EXIT: _mongoc_stream_socket_poll():253
[2020-04-28T07:37:43.806060+00:00]     stream: TRACE   > ENTRY: mongoc_stream_failed():80
[2020-04-28T07:37:43.806065+00:00]     stream: TRACE   > ENTRY: _mongoc_stream_socket_failed():93
[2020-04-28T07:37:43.806070+00:00]     stream: TRACE   > ENTRY: _mongoc_stream_socket_destroy():72
[2020-04-28T07:37:43.806075+00:00]     socket: TRACE   > ENTRY: mongoc_socket_close():790
[2020-04-28T07:37:43.806089+00:00]     socket: TRACE   >  EXIT: mongoc_socket_close():825
[2020-04-28T07:37:43.806095+00:00]     stream: TRACE   >  EXIT: _mongoc_stream_socket_destroy():86
[2020-04-28T07:37:43.806099+00:00]     stream: TRACE   >  EXIT: _mongoc_stream_socket_failed():97
[2020-04-28T07:37:43.806104+00:00]     stream: TRACE   >  EXIT: mongoc_stream_failed():90
[2020-04-28T07:37:43.806110+00:00]     mongoc: TRACE   > ENTRY: mongoc_server_description_handle_ismaster():520
[2020-04-28T07:37:43.806119+00:00]     mongoc: TRACE   >  EXIT: mongoc_server_description_handle_ismaster():527
[2020-04-28T07:37:43.806128+00:00]     mongoc: TRACE   > TRACE: mongoc_topology_description_handle_ismaster():1960 No transition entry to Single for Unknown
[2020-04-28T07:37:43.806139+00:00]     mongoc: TRACE   > ENTRY: mongoc_topology_description_select():798
[2020-04-28T07:37:43.806148+00:00]     mongoc: TRACE   > TRACE: mongoc_topology_description_select():807 Topology type single, [127.0.0.1:27017] is down
[2020-04-28T07:37:43.806202+00:00]     mongoc: TRACE   >  EXIT: mongoc_topology_description_select():808
[2020-04-28T07:37:43.806246+00:00]     mongoc: DEBUG   > Not destroying persistent client for Manager
[2020-04-28T07:37:43.805985+00:00]     socket: TRACE   > TRACE: _mongoc_socket_capture_errno():68 setting errno: 13 Permission denied
[2020-04-28T07:37:43.805997+00:00]     socket: TRACE   > TRACE: _mongoc_socket_errno_is_again():631 errno is: 13

This suggest a permission error with your HTTP server. This may be related to SElinux restrictions, which was covered in an older GitHub issue, mongodb/mongo-php-driver#484. It also seems related to the following Stack Overflow thread for MySQL, which shows that this isn’t particularly unique to MongoDB: https://stackoverflow.com/a/7243034/162228

Thanks Jeremy for the tip. I will look into how SELinux affects this. MySQL is working fine through HTTP though.