-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41958 - Connection Targets #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4bc9856
621622b
000ac25
4ef5647
3b15c26
6c9011a
5ecaa22
e19330c
bd15ec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
.. _php-connection-targets: | ||
|
||
========================== | ||
Choose a Connection Target | ||
========================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, settings, client, stable api | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
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. | ||
|
||
.. _php-connection-atlas: | ||
|
||
Atlas | ||
----- | ||
|
||
To connect to a MongoDB deployment on Atlas, include the following elements | ||
in your connection string: | ||
|
||
- URI of your Atlas cluster | ||
- MongoDB username | ||
- MongoDB 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 {+mdb-server+}. | ||
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page | ||
<php-stable-api>`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. broken link for now |
||
|
||
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. | ||
The code also uses the ``serverApi`` option to specify a {+stable-api+} version. | ||
|
||
.. literalinclude:: /includes/connect/atlas.php | ||
:copyable: true | ||
:language: php | ||
|
||
.. tip:: | ||
|
||
Follow the :atlas:`Atlas driver connection guide </driver-connection>` | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
to retrieve your connection string. | ||
|
||
.. _php-connection-local: | ||
|
||
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 {+driver-short+} to connect to a local MongoDB | ||
deployment: | ||
|
||
.. literalinclude:: /includes/connect/client.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. broken include for now |
||
:language: php | ||
:copyable: true | ||
|
||
.. _php-connection-replica-set: | ||
|
||
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 {+driver-short+} to | ||
perform automatic discovery to find the others. To instruct the driver to perform | ||
automatic discovery, perform one of the following actions: | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- Specify the name of the replica set as the value of the ``replicaSet`` parameter. | ||
- Specify ``false`` as the value of the ``directConnection`` 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``: | ||
|
||
.. literalinclude:: /includes/connect/replica-set.php | ||
:language: php | ||
:copyable: true | ||
|
||
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: | ||
|
||
.. literalinclude:: /includes/connect/direct-connection.php | ||
:language: php | ||
:copyable: true | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about using the ``MongoDB\Client`` class, | ||
see the following API documentation: | ||
|
||
- :ref:`MongoDB\Client <php-api-mongodbclient>` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
try { | ||
// 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' => [ | ||
'version' => '1' | ||
] | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]); | ||
|
||
// 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), "\n"; | ||
echo "Pinged your deployment. You successfully connected to MongoDB!\n"; | ||
} catch (Exception $e) { | ||
echo "An exception occurred: ", $e->getMessage(), "\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default PHP will print the exception in the CLI, so this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to know. I used to Credal AI to translate code from C++ to PHP, but obviously it's not perfect! |
||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// Replace the placeholders with your actual hostname and port | ||
$uri = "mongodb://<hostname>:<port>/?directConnection=true"; | ||
|
||
// Create a MongoDB client | ||
$client = new MongoDB\Client($uri); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// Replace the placeholder with your connection string | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$uri = "mongodb://host1:27017/?replicaSet=sampleRS"; | ||
|
||
// Create a MongoDB client | ||
$client = new MongoDB\Client($uri); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _php-api-mongodbclient: | ||
|
||
===================== | ||
MongoDB\\Client Class | ||
===================== | ||
|
Uh oh!
There was an error while loading. Please reload this page.