PHP 7.2.0 Release Candidate 4 Released

Making a Connection

To connect to the database server, use one of the following:

= new MongoClient(); // connects to localhost:27017
$connection = new MongoClient"mongodb://" ); // connect to a remote host (default port: 27017)
$connection = new MongoClient"mongodb://" ); // 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.

See Also

The chapter on connecting covers different types of connections.

The API documentation on the MongoClient class and MongoClient::__construct() give a comprehensive look at all possible options with a number of examples.

add a note add a note

User Contributed Notes 1 note

1 year ago
Another way to connect, that works for me:

require 'vendor/autoload.php';//composer require "mongodb/mongodb=^"

$m = new MongoDB\Client("mongodb://alex:mypassword@");
echo "Connection to database successfull. <br>";
To Top