Is there a way to test MongoDB connection in PHP?
Ok, I found a way to test it!
<?php
$mongo = new MongoDB\Client('mongodb://my_server_does_not_exist_here:27017');
$dbs = $mongo->listDatabases();
If the connection fails, listDatabases
throws a MongoDB\Driver\Exception\ConnectionTimeoutException
. You just need to set a try/catch around the listDatabases
.