Choose a Connection Target
Overview
In this guide, you can learn how to use a connection string and MongoDB\Client
object
to connect to different types of MongoDB deployments.
Atlas
To connect to a MongoDB deployment on Atlas, include the following elements in your connection string:
URI of your Atlas cluster
Database username
Database user's password
Then, pass your connection string to the MongoDB\Client
constructor.
When you connect to Atlas, we recommend using the Stable API client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. To learn more about the Stable API feature, see the Stable API page.
The following code shows how to use the PHP library to connect to an Atlas cluster.
The code also uses the serverApi
option to specify a Stable API version.
// Replace the placeholder with your Atlas connection string $uri = '<connection string>'; // Create a MongoDB client with server API options $client = new MongoDB\Client($uri, [], [ 'serverApi' => new MongoDB\Driver\ServerApi('1') ]); // Ping the server to verify that the connection works $admin = $client->admin; $command = new MongoDB\Driver\Command(['ping' => 1]); $result = $admin->command($command)->toArray(); echo json_encode($result), PHP_EOL; echo 'Pinged your deployment. You successfully connected to MongoDB!\n';
Tip
Follow the Create a Connection String step of the Quick Start to retrieve your connection string.
Local Deployments
To connect to a local MongoDB deployment, use localhost
as the hostname. By
default, the mongod
process runs on port 27017, though you can customize this for
your deployment.
The following code shows how to use the PHP library to connect to a local MongoDB deployment:
$client = new MongoDB\Client("mongodb://localhost:27017");
Replica Sets
To connect to a replica set, specify the hostnames (or IP addresses) and port numbers of the replica set members in your connection string.
If you aren't able to provide a full list of hosts in the replica set, you can specify one or more of the hosts in the replica set and instruct the PHP library to perform automatic discovery to find the others. To instruct the driver to perform automatic discovery, choose one of the following actions:
Specify the name of the replica set as the value of the
replicaSet
parameter.Specify
false
as the value of thedirectConnection
parameter.Specify more than one host in the replica set.
In the following example, the driver uses a sample connection URI to connect to the
MongoDB replica set sampleRS
, which is running on port 27017
of three different
hosts, including host1
:
$uri = 'mongodb://host1:27017/?replicaSet=sampleRS'; // Create a MongoDB client $client = new MongoDB\Client($uri);
Initialization
To initialize a replica set, you must connect directly to a single member. To do so,
set the directConnection
connection
option to true
in the connection string. The following code example shows how to
set this connection option:
// Replace the placeholders with your actual hostname and port $uri = 'mongodb://<hostname>:<port>/?directConnection=true'; // Create a MongoDB client $client = new MongoDB\Client($uri);
API Documentation
To learn more about using the MongoDB\Client
class,
see the following API documentation: