PHP SSL connection to MongoDB

Hello, I’ve difficulties with connection to MongoDB using crt file by PHP. My code is as follows:

    <?php
require_once __DIR__ . "/vendor/autoload.php";
$conn = new MongoDB\Client('mongodb://[host]', array(
    'username' => '[user[',
    'password' => '[password[',
    'db'       => '[database]',
	'authSource' => 'admin',
	'replicaSet' => '[replica]',
	'ssl' => 'true',
	'sslCAFile' => '/usr/share/ca-certificates/app/ca-certificate.crt'
));

$db = $conn->[database];
$collection = $db->data;
?>

I am getting an error: ‘Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found’. I installed driver in this way:
sudo pecl install mongodb
sudo nano /etc/php/7.4/apache2/php.ini → extension=mongodb.so
cd /var/www/html
sudo composer require mongodb/mongodb

Do you know how to connect properly in this case? Thank you in advance.

I am not sure but I think it should be [user] rather than

Ditto with password.

Right, but that doesn’t solve the problem :smiley:

Try connecting with the Mongo shell. Show us a Mongo URL (with names changed, of course) that works.

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