|
| 1 | +.. _php-connection-options: |
| 2 | + |
| 3 | +========================== |
| 4 | +Specify Connection Options |
| 5 | +========================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: connection string, URI, server, Atlas, settings, configure |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +This page describes the MongoDB connection and authentication options |
| 24 | +available in the {+driver-short+}. |
| 25 | + |
| 26 | +Set Connection Options |
| 27 | +---------------------- |
| 28 | + |
| 29 | +You can configure your connection by specifying options in the connection URI or by |
| 30 | +passing them to the ``MongoDB\Client`` constructor. |
| 31 | + |
| 32 | +.. _php-connection-uri: |
| 33 | + |
| 34 | +Using the Connection URI |
| 35 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 36 | + |
| 37 | +If you pass a connection URI to the ``MongoDB\Client`` constructor, you can include |
| 38 | +connection options in the URI as ``<name>=<value>`` pairs. In the following example, |
| 39 | +the connection URI sets the ``tls`` option to ``true`` and the |
| 40 | +``tlsCertificateKeyFile`` option to ``/path/to/file.pem``: |
| 41 | + |
| 42 | +.. literalinclude:: /includes/connect/connection-options.php |
| 43 | + :language: php |
| 44 | + :copyable: true |
| 45 | + :start-after: // start-connection-uri |
| 46 | + :end-before: // end-connection-uri |
| 47 | + :emphasize-lines: 2,5 |
| 48 | + |
| 49 | +.. _php-client-object: |
| 50 | + |
| 51 | +Using a MongoDB\\Client Object |
| 52 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 53 | + |
| 54 | +You can pass connection options to the ``MongoDB\Client`` constructor |
| 55 | +instead of including them in your connection URI. |
| 56 | + |
| 57 | +The following example shows how to use the ``$uriOptions`` parameter of the |
| 58 | +``MongoDB\Client`` constructor to set connection options: |
| 59 | + |
| 60 | +.. literalinclude:: /includes/connect/connection-options.php |
| 61 | + :language: php |
| 62 | + :copyable: true |
| 63 | + :start-after: // start-client-options |
| 64 | + :end-before: // end-client-options |
| 65 | + :emphasize-lines: 5-8, 11 |
| 66 | + |
| 67 | +.. note:: |
| 68 | + |
| 69 | + If you specify an option in both the ``$uriOptions`` parameter and in the connection |
| 70 | + URI, the value in ``$uriOptions`` takes precedence. |
| 71 | + |
| 72 | +Connection URI Options |
| 73 | +---------------------- |
| 74 | + |
| 75 | +The following sections describe the options that you can set for your connection to |
| 76 | +MongoDB. Each connection option links to its corresponding |
| 77 | +entry in the {+mdb-server+} manual. |
| 78 | + |
| 79 | +.. important:: Percent-Encoding |
| 80 | + |
| 81 | + If the value of a connection option contains special characters, you must |
| 82 | + :wikipedia:`percent-encode <Percent-encoding>` the value before including it |
| 83 | + in the connection URI. You can use the ``rawurlencode()`` method to encode |
| 84 | + these values according to the URI syntax specified in RFC 3986. |
| 85 | + |
| 86 | + Don't percent-encode connection options when including them in the |
| 87 | + ``$uriOptions`` parameter. |
| 88 | + |
| 89 | + To learn more, see the following resources: |
| 90 | + |
| 91 | + - `RFC 3986 <http://www.faqs.org/rfcs/rfc3986.html>`__ |
| 92 | + - `rawurlencode <{+php-manual+}/rawurlencode>`__ in the PHP manual |
| 93 | + |
| 94 | +Replica Set Options |
| 95 | +~~~~~~~~~~~~~~~~~~~ |
| 96 | + |
| 97 | +.. list-table:: |
| 98 | + :header-rows: 1 |
| 99 | + :stub-columns: 1 |
| 100 | + |
| 101 | + * - Connection Option |
| 102 | + - Description |
| 103 | + |
| 104 | + * - :manual:`directConnection </reference/connection-string-options/#mongodb-urioption-urioption.directConnection>` |
| 105 | + - | **Data Type**: {+bool-data-type+} |
| 106 | + | **MongoDB\\Client Example**: ``$uriOptions = ['directConnection' => true];`` |
| 107 | + | **Connection URI Example**: ``directConnection=true`` |
| 108 | + |
| 109 | + * - :manual:`replicaSet </reference/connection-string-options/#mongodb-urioption-urioption.replicaSet>` |
| 110 | + - | **Data Type**: {+string-data-type+} |
| 111 | + | **MongoDB\\Client Example**: ``$uriOptions = ['replicaSet' => 'replicaSetName'];`` |
| 112 | + | **Connection URI Example**: ``replicaSet=replicaSetName`` |
| 113 | + |
| 114 | +Connection Options |
| 115 | +~~~~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +TLS Options |
| 118 | +``````````` |
| 119 | +To learn about the TLS options available in the {+driver-short+}, see the |
| 120 | +:ref:`TLS <php-tls>` page. |
| 121 | + |
| 122 | +Timeout Options |
| 123 | +``````````````` |
| 124 | + |
| 125 | +.. list-table:: |
| 126 | + :header-rows: 1 |
| 127 | + :stub-columns: 1 |
| 128 | + |
| 129 | + * - Connection Option |
| 130 | + - Description |
| 131 | + |
| 132 | + * - :manual:`connectTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.connecttimeoutms>` |
| 133 | + - | **Data Type**: {+int-data-type+} |
| 134 | + | **MongoDB\\Client Example**: ``$uriOptions = ['connectTimeoutMS' => 2000];`` |
| 135 | + | **Connection URI Example**: ``connectTimeoutMS=2000`` |
| 136 | + |
| 137 | + * - :manual:`socketTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.sockettimeoutms>` |
| 138 | + - | **Data Type**: {+int-data-type+} |
| 139 | + | **MongoDB\\Client Example**: ``$uriOptions = ['socketTimeoutMS' => 20000];`` |
| 140 | + | **Connection URI Example**: ``socketTimeoutMS=20000`` |
| 141 | + |
| 142 | +.. _php-compression-options: |
| 143 | + |
| 144 | +Compression Options |
| 145 | +``````````````````` |
| 146 | + |
| 147 | +.. list-table:: |
| 148 | + :header-rows: 1 |
| 149 | + :stub-columns: 1 |
| 150 | + |
| 151 | + * - Connection Option |
| 152 | + - Description |
| 153 | + |
| 154 | + * - :manual:`compressors </reference/connection-string-options/#mongodb-urioption-urioption.compressors>` |
| 155 | + - | **Data Type**: {+string-data-type+} |
| 156 | + | **MongoDB\\Client Example**: ``$uriOptions = ['compressors' => 'snappy,zstd,zlib'];`` |
| 157 | + | **Connection URI Example**: ``compressors=snappy,zstd,zlib`` |
| 158 | + |
| 159 | + * - :manual:`zlibCompressionLevel </reference/connection-string-options/#mongodb-urioption-urioption.zlibcompressionlevel>` |
| 160 | + - | **Data Type**: {+int-data-type+} |
| 161 | + | **MongoDB\\Client Example**: ``$uriOptions = ['zlibCompressionLevel' => 3];`` |
| 162 | + | **Connection URI Example**: ``zlibCompressionLevel=3`` |
| 163 | + |
| 164 | +Write Concern Options |
| 165 | +~~~~~~~~~~~~~~~~~~~~~ |
| 166 | + |
| 167 | +.. list-table:: |
| 168 | + :header-rows: 1 |
| 169 | + :stub-columns: 1 |
| 170 | + |
| 171 | + * - Connection Option |
| 172 | + - Description |
| 173 | + |
| 174 | + * - :manual:`w </reference/connection-string-options/#mongodb-urioption-urioption.w>` |
| 175 | + - | **Data Type**: {+string-data-type+} |
| 176 | + | **MongoDB\\Client Example**: ``$uriOptions = ['w' => 'majority'];`` |
| 177 | + | **Connection URI Example**: ``w=majority`` |
| 178 | + |
| 179 | + * - :manual:`wTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.wtimeoutms>` |
| 180 | + - | **Data Type**: {+int-data-type+} |
| 181 | + | **MongoDB\\Client Example**: ``$uriOptions = ['wTimeoutMS' => 10000];`` |
| 182 | + | **Connection URI Example**: ``wTimeoutMS=10000`` |
| 183 | + |
| 184 | + * - :manual:`journal </reference/connection-string-options/#mongodb-urioption-urioption.journal>` |
| 185 | + - | **Data Type**: {+bool-data-type+} |
| 186 | + | **MongoDB\\Client Example**: ``$uriOptions = ['journal' => true];`` |
| 187 | + | **Connection URI Example**: ``journal=true`` |
| 188 | + |
| 189 | +Read Concern Options |
| 190 | +~~~~~~~~~~~~~~~~~~~~ |
| 191 | + |
| 192 | +.. list-table:: |
| 193 | + :header-rows: 1 |
| 194 | + :stub-columns: 1 |
| 195 | + |
| 196 | + * - Connection Option |
| 197 | + - Description |
| 198 | + |
| 199 | + * - :manual:`readConcernLevel </reference/connection-string-options/#mongodb-urioption-urioption.readconcernlevel>` |
| 200 | + - | **Data Type**: {+string-data-type+} |
| 201 | + | **MongoDB\\Client Example**: ``$uriOptions = ['readConcernLevel' => 'majority'];`` |
| 202 | + | **Connection URI Example**: ``readConcernLevel=majority`` |
| 203 | + |
| 204 | +Read Preference Options |
| 205 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 206 | + |
| 207 | +.. list-table:: |
| 208 | + :header-rows: 1 |
| 209 | + :stub-columns: 1 |
| 210 | + :widths: 22 78 |
| 211 | + |
| 212 | + * - Connection Option |
| 213 | + - Description |
| 214 | + |
| 215 | + * - :manual:`readPreference </reference/connection-string-options/#mongodb-urioption-urioption.readpreference>` |
| 216 | + - | **Data Type**: `MongoDB\\Driver\\ReadPreference <https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__ |
| 217 | + | **MongoDB\\Client Example**: ``$uriOptions = ['readPreference' => 'secondaryPreferred'];`` |
| 218 | + | **Connection URI Example**: ``readPreference=secondaryPreferred`` |
| 219 | + |
| 220 | + * - :manual:`maxStalenessSeconds </reference/connection-string-options/#mongodb-urioption-urioption.maxstalenessseconds>` |
| 221 | + - | **Data Type**: {+int-data-type+} |
| 222 | + | **MongoDB\\Client Example**: ``$uriOptions = ['maxStalenessSeconds' => 30];`` |
| 223 | + | **Connection URI Example**: ``maxStalenessSeconds=30`` |
| 224 | + |
| 225 | + * - :manual:`readPreferenceTags </reference/connection-string-options/#mongodb-urioption-urioption.readpreferencetags>` |
| 226 | + - | **Data Type**: ``array`` |
| 227 | + | **MongoDB\\Client Example**: |
| 228 | + |
| 229 | + .. code-block:: php |
| 230 | + |
| 231 | + $uriOptions = [ |
| 232 | + 'readPreferenceTags' => [ |
| 233 | + ['dc' => 'ny', 'rack' => 'r1'], |
| 234 | + [], |
| 235 | + ], |
| 236 | + ]; |
| 237 | + |
| 238 | + **Connection URI Example**: ``readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=`` |
| 239 | + |
| 240 | +Authentication Options |
| 241 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 242 | + |
| 243 | +To learn about the authentication options available in the {+driver-short+}, see |
| 244 | +:ref:`Authentication Mechanisms. <php-auth>` |
| 245 | + |
| 246 | +Server Selection and Discovery Options |
| 247 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 248 | + |
| 249 | +.. list-table:: |
| 250 | + :header-rows: 1 |
| 251 | + :stub-columns: 1 |
| 252 | + :widths: 30 70 |
| 253 | + |
| 254 | + * - Connection Option |
| 255 | + - Description |
| 256 | + |
| 257 | + * - :manual:`localThresholdMS </reference/connection-string-options/#mongodb-urioption-urioption.localthresholdms>` |
| 258 | + - | **Data Type**: {+int-data-type+} |
| 259 | + | **MongoDB\\Client Example**: ``$uriOptions = ['localThresholdMS' => 20];`` |
| 260 | + | **Connection URI Example**: ``localThresholdMS=20`` |
| 261 | + |
| 262 | + * - :manual:`serverSelectionTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.serverselectiontimeoutms>` |
| 263 | + - | **Data Type**: {+int-data-type+} |
| 264 | + | **MongoDB\\Client Example**: ``$uriOptions = ['serverSelectionTimeoutMS' => 5000];`` |
| 265 | + | **Connection URI Example**: ``serverSelectionTimeoutMS=5000`` |
| 266 | + |
| 267 | + * - :manual:`serverSelectionTryOnce </reference/connection-string-options/#mongodb-urioption-urioption.serverselectiontryonce>` |
| 268 | + - | **Data Type**: {+bool-data-type+} |
| 269 | + | **MongoDB\\Client Example**: ``$uriOptions = ['serverSelectionTryOnce' => false];`` |
| 270 | + | **Connection URI Example**: ``serverSelectionTryOnce=false`` |
| 271 | + |
| 272 | + * - :manual:`heartbeatFrequencyMS </reference/connection-string-options/#mongodb-urioption-urioption.heartbeatfrequencyms>` |
| 273 | + - | **Data Type**: {+int-data-type+} |
| 274 | + | **MongoDB\\Client Example**: ``$uriOptions = ['heartbeatFrequencyMS' => 30000];`` |
| 275 | + | **Connection URI Example**: ``heartbeatFrequencyMS=30000`` |
| 276 | + |
| 277 | + * - :manual:`socketCheckIntervalMS </reference/connection-string-options/#mongodb-urioption-urioption.socketcheckintervalms>` |
| 278 | + - | **Data Type**: {+int-data-type+} |
| 279 | + | **MongoDB\\Client Example**: ``$uriOptions = ['socketCheckIntervalMS' => 4000];`` |
| 280 | + | **Connection URI Example**: ``socketCheckIntervalMS=4000`` |
| 281 | + |
| 282 | +Miscellaneous Configuration |
| 283 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 284 | + |
| 285 | +.. list-table:: |
| 286 | + :header-rows: 1 |
| 287 | + :stub-columns: 1 |
| 288 | + |
| 289 | + * - Connection Option |
| 290 | + - Description |
| 291 | + |
| 292 | + * - :manual:`appName </reference/connection-string-options/#mongodb-urioption-urioption.appname>` |
| 293 | + - | **Data Type**: {+string-data-type+} |
| 294 | + | **MongoDB\\Client Example**: ``$uriOptions = ['appName' => 'myApp'];`` |
| 295 | + | **Connection URI Example**: ``appName=myApp`` |
| 296 | + |
| 297 | + * - :manual:`retryReads </reference/connection-string-options/#mongodb-urioption-urioption.retryreads>` |
| 298 | + - | **Data Type**: {+bool-data-type+} |
| 299 | + | **MongoDB\\Client Example**: ``$uriOptions = ['retryReads' => false];`` |
| 300 | + | **Connection URI Example**: ``retryReads=false`` |
| 301 | + |
| 302 | + * - :manual:`retryWrites </reference/connection-string-options/#mongodb-urioption-urioption.retrywrites>` |
| 303 | + - | **Data Type**: {+bool-data-type+} |
| 304 | + | **MongoDB\\Client Example**: ``$uriOptions = ['retryWrites' => false];`` |
| 305 | + | **Connection URI Example**: ``retryWrites=false`` |
| 306 | + |
| 307 | + * - :manual:`loadBalanced </reference/connection-string-options/#mongodb-urioption-urioption.loadbalanced>` |
| 308 | + - | **Data Type**: {+bool-data-type+} |
| 309 | + | **MongoDB\\Client Example**: ``$uriOptions = ['loadBalanced' => true];`` |
| 310 | + | **Connection URI Example**: ``loadBalanced=true`` |
| 311 | + |
| 312 | + * - :manual:`srvMaxHosts </reference/connection-string-options/#mongodb-urioption-urioption.srvmaxhosts>` |
| 313 | + - | **Data Type**: {+int-data-type+} |
| 314 | + | **MongoDB\\Client Example**: ``$uriOptions = ['srvMaxHosts' => 5];`` |
| 315 | + | **Connection URI Example**: ``srvMaxHosts=5`` |
| 316 | + |
| 317 | +API Documentation |
| 318 | +----------------- |
| 319 | + |
| 320 | +For more information about the ``MongoDB\Client`` class, see the following {+driver-short+} |
| 321 | +API documentation: |
| 322 | + |
| 323 | +- :phpclass:`MongoDB\Client` |
| 324 | + |
| 325 | +For more information about the ``MongoDB\Driver\ReadPreference`` class, see the following |
| 326 | +{+extension-short+} API documentation: |
| 327 | + |
| 328 | +- `MongoDB\\Driver\\ReadPreference <https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__ |
0 commit comments