Another way to connect, that works for me:
require 'vendor/autoload.php';//composer require "mongodb/mongodb=^188.8.131.52"
$m = new MongoDB\Client("mongodb://alex:email@example.com:27017/");
echo "Connection to database successfull. <br>";
To connect to the database server, use one of the following:
$connection = new MongoClient(); // connects to localhost:27017
$connection = new MongoClient( "mongodb://example.com" ); // connect to a remote host (default port: 27017)
$connection = new MongoClient( "mongodb://example.com:65432" ); // connect to a remote host at a given port
You do not have to explicitly disconnect from the database. The driver uses persistent connections and will re-use already established connections.