|
| 1 | +.. _php-connection-targets: |
| 2 | + |
| 3 | +========================== |
| 4 | +Choose a Connection Target |
| 5 | +========================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: connection string, URI, server, settings, client, stable api |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use a connection string and ``MongoDB\Client`` object |
| 24 | +to connect to different types of MongoDB deployments. |
| 25 | + |
| 26 | +.. _php-connection-atlas: |
| 27 | + |
| 28 | +Atlas |
| 29 | +----- |
| 30 | + |
| 31 | +To connect to a MongoDB deployment on Atlas, include the following elements |
| 32 | +in your connection string: |
| 33 | + |
| 34 | +- URI of your Atlas cluster |
| 35 | +- Database username |
| 36 | +- Database user's password |
| 37 | + |
| 38 | +Then, pass your connection string to the ``MongoDB\Client`` constructor. |
| 39 | + |
| 40 | +When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid |
| 41 | +breaking changes when Atlas upgrades to a new version of {+mdb-server+}. |
| 42 | +To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page |
| 43 | +<php-stable-api>`. |
| 44 | + |
| 45 | +The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. |
| 46 | +The code also uses the ``serverApi`` option to specify a {+stable-api+} version. |
| 47 | + |
| 48 | +.. literalinclude:: /includes/connect/atlas.php |
| 49 | + :copyable: true |
| 50 | + :language: php |
| 51 | + |
| 52 | +.. tip:: |
| 53 | + |
| 54 | + Follow the :ref:`php-connection-string` step of the Quick Start |
| 55 | + to retrieve your connection string. |
| 56 | + |
| 57 | +.. _php-connection-local: |
| 58 | + |
| 59 | +Local Deployments |
| 60 | +----------------- |
| 61 | + |
| 62 | +To connect to a local MongoDB deployment, use ``localhost`` as the hostname. By |
| 63 | +default, the ``mongod`` process runs on port 27017, though you can customize this for |
| 64 | +your deployment. |
| 65 | + |
| 66 | +The following code shows how to use the {+driver-short+} to connect to a local MongoDB |
| 67 | +deployment: |
| 68 | + |
| 69 | +.. literalinclude:: /includes/connect/client.php |
| 70 | + :language: php |
| 71 | + :copyable: true |
| 72 | + |
| 73 | +.. _php-connection-replica-set: |
| 74 | + |
| 75 | +Replica Sets |
| 76 | +------------ |
| 77 | + |
| 78 | +To connect to a replica set, specify the hostnames (or IP addresses) and |
| 79 | +port numbers of the replica set members in your connection string. |
| 80 | + |
| 81 | +If you aren't able to provide a full list of hosts in the replica set, you can |
| 82 | +specify one or more of the hosts in the replica set and instruct the {+driver-short+} to |
| 83 | +perform automatic discovery to find the others. To instruct the driver to perform |
| 84 | +automatic discovery, choose one of the following actions: |
| 85 | + |
| 86 | +- Specify the name of the replica set as the value of the ``replicaSet`` parameter. |
| 87 | +- Specify ``false`` as the value of the ``directConnection`` parameter. |
| 88 | +- Specify more than one host in the replica set. |
| 89 | + |
| 90 | +In the following example, the driver uses a sample connection URI to connect to the |
| 91 | +MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different |
| 92 | +hosts, including ``host1``: |
| 93 | + |
| 94 | +.. literalinclude:: /includes/connect/replica-set.php |
| 95 | + :language: php |
| 96 | + :copyable: true |
| 97 | + |
| 98 | +Initialization |
| 99 | +~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +To initialize a replica set, you must connect directly to a single member. To do so, |
| 102 | +set the ``directConnection`` connection |
| 103 | +option to ``true`` in the connection string. The following code example shows how to |
| 104 | +set this connection option: |
| 105 | + |
| 106 | +.. literalinclude:: /includes/connect/direct-connection.php |
| 107 | + :language: php |
| 108 | + :copyable: true |
| 109 | + |
| 110 | +API Documentation |
| 111 | +----------------- |
| 112 | + |
| 113 | +To learn more about using the ``MongoDB\Client`` class, |
| 114 | +see the following API documentation: |
| 115 | + |
| 116 | +- :ref:`MongoDB\Client <php-api-mongodbclient>` |
0 commit comments