PHP SSL connection to MongoDB

Hi, I use DigitalOcean MongoDB cluster. Here is my connection string that works:

mongodb+srv://doadmin:[password]@[app]-ad319710.mongo.ondigitalocean.com/aircraft?authSource=admin&replicaSet=[app]&tls=true&tlsCAFile=/usr/share/ca-certificates/app/ca-certificate.crt

try this:

$client = new MongoDB\Client(
‘mongodb+srv://doadmin:[password]@[app]-ad319710.mongo.ondigitalocean.com/aircraft?authSource=admin&replicaSet=[app]&tls=true&tlsCAFile=/usr/share/ca-certificates/app/ca-certificate.crt’
);

$db = $client->[database];

Now I’m getting: Parse error : syntax error, unexpected ‘:’, expecting ‘)’ in line with your code

try this:

$client = new MongoDB\Client('mongodb+srv://doadmin:[password]@[app]-ad319710.mongo.ondigitalocean.com/aircraft?authSource=admin&replicaSet=[app]&tls=true&tlsCAFile=/usr/share/ca-certificates/app/ca-certificate.crt');

PHP code is valid but again: Fatal error : Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found

what are you getting if you run this?

ping [app]-ad319710.mongo.ondigitalocean.com

ping: [app]-ad319710.mongo.ondigitalocean.com: No address associated with hostname

When I use the certificate in the Mongo client, I connect normally and can work with the database

are you able to connect when ssl is disabled?

have you tried reading this PHP error: No suitable servers found?

I don’t use SELinux as they mentioned in PHP error: No suitable servers found

Without TLS:

{“t”:{“$date”:“2021-07-04T09:41:18.687Z”},“s”:“E”, “c”:“NETWORK”, “id”:23256, “ctx”:“ReplicaSetMonitor-TaskExecutor”,“msg”:“SSL peer certificate validation failed”,“attr”:{“error”:“SSL peer certificate validation failed: self signed certificate in certificate chain”}}

are you running this from the same machine you use for your app?

No, MongoDB is one of Managed Databases. Droplet is separate

which version of these are you using?
image

is you mongodb version one of these?

In the sense of a MongoDB client? If so, 4.4.6

in the sense of your mongodb server

Version of server is 4.4.3

so, it seems like your client version is not compatible with you server version
see this: https://docs.mongodb.com/drivers/php/#compatibility
image

Should I install newer PHP driver version?

yes, seems like you should
If you cant, you can try using lower version of mongodb server