mongodb / mongo-php-library

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

Can't connect to Atlas #698

Closed trustit-developement closed 4 years ago

trustit-developement commented 4 years ago

PHPINFO I'm having the same issue, lost an entire day trying everything... php 7.0.33 mongodb 1.5.2 running in Laravel homestead / vagrant

this is the code:

$url = 'mongodb+srv://teste:teste@applications-jujzk.mongodb.net/test?retryWrites=true&w=majority'; $client = new MongoDB\Client($url); dd($client->listDatabases());

if i try connecting to "mongodb://localhost:27017" it works fine, but i can't connect to AWS DocumentDB or Atlas.

i also tried a lot of strings like $url = 'mongodb+srv://teste:teste@applications-jujzk.mongodb.net/test?ssl=true&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=1500 0&w=majority"';

this strings work fine in Compass, but not in PHP...

follows the error log

LARAVEL LOG

jmikola commented 4 years ago

In the future, please share the exception message/backtrace as plain text rather than attaching an HTML file. I was able to extract the following from it:

No suitable servers found (serverSelectionTryOnce set): [Failed to receive length header from server. calling ismaster on 'applications-shard-00-02-jujzk.mongodb.net:27017'] [Failed to receive length header from server. calling ismaster on 'applications-shard-00-00-jujzk.mongodb.net:27017'] [Failed to receive length header from server. calling ismaster on 'applications-shard-00-01-jujzk.mongodb.net:27017']

This suggests a socket error attempting to read a response from the server. You mentioned that you can connect to these servers just fine with Compass. Is Compass also running within the same Vagrant machine as the PHP driver? If not, I'd suggest trying to run the PHP driver from the same environment as the Compass application (presumably outside of Vagrant).

I don't see any evidence that this is a TLS issue, but it may be helpful to follow the steps I outlined in https://github.com/mongodb/mongo-php-library/issues/697#issuecomment-559157290. Therein, I linked to an example script that attempts to connect to the MongoDB server using HTTPS to obtain an error message from the server. That may be used to verify that PHP can at least communicate in both directions with the server (taking the driver itself out of the equation).

jmikola commented 4 years ago

Closing due to inactivity. Feel free to follow-up with answers to my previous questions if this is still outstanding and we can re-open.