No suitable servers found: PHP error

Hi,

I’m using the PHP driver for the first time and get this error message. I try to get a connection from a php based host to a mongoDB container.
I’ve installed MongoDB extension version > 1.8. The connection to MongoDB through the MongoDB Client seems to be ok (maybe it’s not), but the insertOne function doesn’t work.
Don’t know if something is missing. There are some infos here according this error message, but none of them worked in my case. Would be very grateful for help.

Here’s the code snippet, I use for testing the connection:

       try {
            $DB_CONNECTION_STRING="mongodb://localhost:27017";
            $con = new Client($DB_CONNECTION_STRING);
            $db = $con->test;
            $collection = $db->tester;
            $collection->insertOne(['name'=>'Tom', 'email'=>'tom@tester.com']);
        }
        catch (\Exception $e) {
            print_r($e->getMessage());
        } 

The output is here:

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

And here is some information from the phpinfo:

MongoDB extension version 1.9.1
MongoDB extension stability stable
libbson bundled version 1.17.4
libmongoc bundled version 1.17.4
libmongoc SSL enabled
libmongoc SSL library OpenSSL
libmongoc crypto enabled
libmongoc crypto library libcrypto
libmongoc crypto system profile disabled
libmongoc SASL disabled
libmongoc ICU enabled
libmongoc compression enabled
libmongoc compression snappy disabled
libmongoc compression zlib enabled
libmongoc compression zstd disabled
libmongocrypt bundled version 1.0.4
libmongocrypt crypto enabled
libmongocrypt crypto library libcrypto

When the client is created, no connection to the server is established, which is why you see the failure only when you run insertOne. The exception you’re seeing is the result of the driver trying to find a server to send this command to, which it can’t because nobody is listening on localhost:27017. You mentioned you want to connect “to a mongoDB container”, are you sure the connection string is correct?

To confirm that you are trying to connect to the right server by calling the mongo shell: mongo mongodb://localhost:27017. I would expect this to fail as well, so you’ll have to find where MongoDB is listening on your PHP host and use the correct connection string. I can’t help you without more information on your setup, so please do elaborate on how you run your setup if you need more help.

1 Like

Thank you very much, Andreas. That was the hint I needed (had already suspected that the error was caused by this). After reconfiguring the container/network settings (also changing the connection string) i can connect.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.