mongo-php-library: Cant connect to mongodb Atlas with mongodb php module 1.4.4

Hello, I am trying to connect to mongoDB Atlas free tier from my linux shared hosted server which is running the mongodb php module 1.4.4 but I get an error.

My code is the following:

$manager = new MongoDB\Driver\Manager("mongodb://admin:admin@mongo-shard-00-00-waf4c.mongodb.net:27017,mongo-shard-00-01-waf4c.mongodb.net:27017,mongo-shard-00-02-waf4c.mongodb.net:27017/test?ssl=true&replicaSet=mongo-shard-0&authSource=admin&retryWrites=true");

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$manager->executeBulkWrite('db.collection2', $bulk);

The error I get is the following:

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection closed calling ismaster on 'mongo-shard-00-00-waf4c.mongodb.net:27017'] [connection closed calling ismaster on 'mongo-shard-00-01-waf4c.mongodb.net:27017'] [connection closed calling ismaster on 'mongo-shard-00-02-waf4c.mongodb.net:27017'] in /test-connection.php:25 Stack trace: #0 /test-connection.php(25) MongoDB\Driver\Manager->executeBulkWrite('db.collection2', Object(MongoDB\Driver\BulkWrite)) #1 {main} thrown in /test-connection.php on line 25

I dont get this error while running the same code on my local mamp configuration (1.5.1 php module) but the majority of hosting providers all have 1.4.4.

(ive edited the credentials to not be publicly live with dummy hosts)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Could you run the following on your host (on the command line), replacing “YOUR_URL” with the URL that you just gave me? You can email the output to derick@mongodb.com for privacy.

strace -tt php -r '$m = new MongoDB\Driver\Manager( "YOUR_URL" ); var_dump($m->executeCommand( "admin", new MongoDB\Driver\Command( [ "ping" => 1] ) ) );'

You’re welcome. Glad it works now. I’ll be closing this issue.

@thenoblenewbie — thanks for that, I have copied the URL locally and deleted the comment. I don’t think we should keep a comment with your credentials.

Trying the same on the 1.5.1 version of mongodb I get bool(false)

That return value means it’s likely that file_get_contents() encountered an error. Per the documentation, an E_WARNING should have been emitted. If you didn’t see that, please make sure that error_reporting and display_errors INI options are set accordingly and nothing is being suppressed.

Also, assuming this is your “local mamp configuration”, can you confirm that file_get_contents() behaves the same for both your local CLI and web SAPI environments?

I am curious as to why file_get_contents() would have failed on your local environment, since you said the 1.5.1 driver had no trouble connecting to Atlas. Please double-check that you were testing with an https URL, since Atlas requires SSL.


On the off chance you’re using the Atlas free tier, I can confirm that attempting to use file_get_contents() to communicate with a cluster emits the following PHP error: “failed to open stream: HTTP request failed!”; however, if the remote host was truly inaccessible I believe you would see a different error (e.g. “Connection timed out”).

Running the file_get_contents() against an M10 or higher (non-free) cluster I receive the following 85-byte response string:

It looks like you are trying to access MongoDB over HTTP on the native driver port

As for why the free tier response differently to our HTTP request, I expect that has to do with it being a shared (multi-tenant) MongoDB server. The M10+ instances are dedicated mongod processes and behave no differently than running a replica set on my local machine; however, the free tier instances do have variances, especially around undocumented functionality like we’re exploring here.

On the off chance that Atlas made some configuration changes and HTTP requests no longer receive responses, we may want to test connectivity (outside of the driver) using SSL certificate verification. This would follow the steps in https://github.com/mongodb/mongo-php-library/issues/313#issuecomment-267625578, which attempts to print the certificate properties from the remote host. I can confirm that the ssl:// test from that thread works consistently for both an M0 and M10 instance (using a single host from the connection string).

Lastly, make sure you’re using a host from the mongodb:// connection string for these tests. I don’t believe the single hostname in the mongodb+srv:// URLs will work for this purpose.

Sadly I dont have CLI access to the server as its a shared hosting one.

Shifting focus to the shared hosting server, if file_get_contents() is failing with “Connection refused” instead of “HTTP request failed”, I think that implies a general connectivity issue unrelated to the PHP driver.

In your second post, you mentioned that the necessary IPs are included in the Atlas whitelist. If we assume there’s no problem there, I think the problem may be with the shared web server configuration, which would require technical support from the hosting provider. That said, the tests in this thread should come in handy to demonstrate the connectivity problem you’re experiencing.

Please answer my outstanding question in https://github.com/mongodb/mongo-php-library/issues/575#issuecomment-420012470. I asked if “Connection refused again” (from your comment in https://github.com/mongodb/mongo-php-library/issues/575#issuecomment-419599240) was referring to the file_get_contents() test that I suggested in https://github.com/mongodb/mongo-php-library/issues/575#issuecomment-419577662.

My last suggestion was that we test connectivity to the MongoDB server using HTTP (using file_get_contents()) from both a CLI environment and web SAPI. For both of those environments, it would be helpful to see the var_dump() output of whatever file_get_contents() returns. If both of those environments fail to return a response similar to what is shown in the issues I cross-referenced in https://github.com/mongodb/mongo-php-library/issues/575#issuecomment-419489567, then we can conclude that there is a general network connectivity issue between your application server and Atlas. If things are working correctly, we’d expect to see a “It looks like you are trying to access MongoDB over HTTP on the native driver port” message returned.

If the CLI environment works but the web SAPI fails, then we can conclude that the problems rests in some security settings within the web SAPI (possibly SELinux, which I alluded to above when linking to https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-367310192).

If both the CLI and web SAPI can return a response from the MongoDB server via HTTP, then we can rule out those issues and return to debugging the PHP driver itself. The next step for driver debugging would likely entail capturing trace logs with the mongodb.debug INI setting.

https://github.com/mongodb/mongo-php-library/issues/372 walks through another connectivity issue and https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-306801722 suggests using file_get_contents() to attempt to connect to one of the MongoDB nodes using HTTP (or HTTPS in your case). Other examples of using file_get_contents() for such troubleshooting can be found in https://github.com/mongodb/mongo-php-library/issues/361#issuecomment-299486305 and https://github.com/mongodb/mongo-php-library/issues/313#issuecomment-267625578, although both of those issues pertained to SSL certificate issues impeding the connection (I don’t see an indication of that in this issue).

Using file_get_contents() lets us test whether PHP is even able to make an outgoing connection from the web SAPI. Depending on your operating system and web server configuration, that could be disabled (see: https://github.com/mongodb/mongo-php-library/issues/372#issuecomment-367310192).

You can also use the mongodb.debug INI setting to capture trace information from libmongoc, which may shed more light on the connection process. https://github.com/mongodb/mongo-php-library/issues/362 is another thread worth reviewing and includes some output where trace information indicated accessibility issues (outside of the driver’s control).

https://github.com/mongodb/mongo-php-driver/issues/855 is another issue related to Atlas connectivity issues and was tied to a bug fixed in driver version 1.5.0; however, the indicator of the bug in that issue was one server connection failing with “TLS handshake failed: -9806”, which is not present in your original exception. All of your connections fail with a generic “connection closed” reason.

A few things you can try:

  • Do you have Atlas configured so that your shared host can connect to it? Atlas has an IP white list that you need to configure.
  • Could you check the “mongodb” section of phpinfo() output, and show the results here? It’s possible that SSL hasn’t been enabled in the driver build.