-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41959 - Connection Options #112
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 32 commits
45739f4
1df7cf0
7d07a4d
d505ef1
5fcd405
aa3a2f3
d7e5227
0dbe1e6
916ed85
73dc918
9767bfd
bf43a6c
6bfb367
3a4ead0
d68b868
8f5a381
2296609
c90ad5d
b31f3fc
790ffdf
8ca2f3d
fa9622a
69584e5
96999b3
3d777f0
5779b96
3c2a761
a1470a6
57e6728
db5a290
b6088f8
7cd7c30
fa842ac
5a519a4
2eb98ea
20a4817
dca4565
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,356 @@ | ||
.. _php-connection-options: | ||
|
||
========================== | ||
Specify Connection Options | ||
========================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, Atlas, settings, configure | ||
|
||
Overview | ||
-------- | ||
|
||
This page describes the MongoDB connection and authentication options | ||
available in the {+driver-short+}. | ||
|
||
Set Connection Options | ||
---------------------- | ||
|
||
You can configure your connection by specifying options in the connection URI or by | ||
passing them to the ``MongoDB\Client`` constructor. | ||
|
||
.. _php-connection-uri: | ||
|
||
Using the Connection URI | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
If you pass a connection URI to the ``MongoDB\Client`` constructor, you can include | ||
connection options in the URI as ``<name>=<value>`` pairs. In the following example, | ||
the connection URI sets the ``tls`` option to ``true`` and the | ||
``tlsCertificateKeyFile`` option to ``/path/to/file.pem``: | ||
|
||
.. literalinclude:: /includes/connect/connection-options.php | ||
:language: php | ||
:copyable: true | ||
:start-after: // start-connection-uri | ||
:end-before: // end-connection-uri | ||
:emphasize-lines: 2,5 | ||
|
||
.. _php-client-object: | ||
|
||
Using a MongoDB\\Client Object | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
You can pass connection options to the ``MongoDB\Client`` constructor | ||
instead of including them in your connection URI. | ||
Configuring the connection this way makes it easier to | ||
change settings at runtime and helps you catch errors during compilation. | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following example shows how to use the ``$uriOptions`` parameter of the | ||
``MongoDB\Client`` constructor to set connection options: | ||
|
||
.. literalinclude:: /includes/connect/connection-options.php | ||
:language: php | ||
:copyable: true | ||
:start-after: // start-client-options | ||
:end-before: // end-client-options | ||
:emphasize-lines: 5-8, 11 | ||
|
||
.. note:: | ||
|
||
If you specify an option in both the ``MongoDB\Client`` object and in the connection | ||
URI, the value in the ``MongoDB\Client`` object takes precedence. | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Connection URI Options | ||
---------------------- | ||
|
||
The following sections describe the options that you can set for your connection to | ||
MongoDB. Each connection option links to its corresponding | ||
entry in the {+mdb-server+} manual. | ||
|
||
Replica Set Options | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`directConnection </reference/connection-string/#mongodb-urioption-urioption.directConnection>` | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'directConnection' => true ];`` | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| **Connection URI Example**: ``directConnection=true`` | ||
|
||
* - :manual:`replicaSet </reference/connection-string/#mongodb-urioption-urioption.replicaSet>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'replicaSet' => 'replicaSetName' ];`` | ||
| **Connection URI Example**: ``replicaSet=replicaSetName`` | ||
|
||
Connection Options | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
TLS Options | ||
``````````` | ||
To learn about the TLS options available in the {+driver-short+}, see the | ||
:ref:`TLS <php-tls>` page. | ||
|
||
Timeout Options | ||
``````````````` | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`connectTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.connecttimeoutms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'connectTimeoutMS' => 20000 ];`` | ||
| **Connection URI Example**: ``connectTimeoutMS=20000`` | ||
|
||
* - :manual:`socketTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.sockettimeoutms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'socketTimeoutMS' => 400000 ];`` | ||
| **Connection URI Example**: ``socketTimeoutMS=400000`` | ||
|
||
.. _php-compression-options: | ||
|
||
Compression Options | ||
``````````````````` | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`compressors </reference/connection-string/#mongodb-urioption-urioption.compressors>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'compressors' => 'snappy,zstd,zlib' ];`` | ||
| **Connection URI Example**: ``compressors=snappy,zstd,zlib`` | ||
|
||
* - :manual:`zlibCompressionLevel </reference/connection-string/#mongodb-urioption-urioption.zlibcompressionlevel>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'zlibCompressionLevel' => 3 ];`` | ||
| **Connection URI Example**: ``zlibCompressionLevel=3`` | ||
|
||
Connection Pool Options | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`maxPoolSize </reference/connection-string/#mongodb-urioption-urioption.maxpoolsize>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: N/A | ||
| **Connection URI Example**: ``maxPoolSize=75`` | ||
|
||
* - :manual:`waitQueueTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.waitqueuetimeoutms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: N/A | ||
| **Connection URI Example**: ``waitQueueTimeoutMS=10000`` | ||
|
||
Write Concern Options | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`w </reference/connection-string/#mongodb-urioption-urioption.w>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'w' => 'majority' ];`` | ||
| **Connection URI Example**: ``w=majority`` | ||
|
||
* - :manual:`wTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.wtimeoutms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'wTimeoutMS' => 10000 ];`` | ||
| **Connection URI Example**: ``wTimeoutMS=10000`` | ||
|
||
* - :manual:`journal </reference/connection-string/#mongodb-urioption-urioption.journal>` | ||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'journal' => true ];`` | ||
| **Connection URI Example**: ``journal=true`` | ||
|
||
Read Concern Options | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`readConcernLevel </reference/connection-string/#mongodb-urioption-urioption.readconcernlevel>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'readConcernLevel' => 'majority' ];`` | ||
| **Connection URI Example**: ``readConcernLevel=majority`` | ||
|
||
Read Preference Options | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 22 78 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`readPreference </reference/connection-string/#mongodb-urioption-urioption.readpreference>` | ||
- | **Data Type**: `MongoDB\\Driver\\ReadPreference <https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__ | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'readPreference' => 'secondaryPreferred' ];`` | ||
| **Connection URI Example**: ``readPreference=secondaryPreferred`` | ||
|
||
* - :manual:`maxStalenessSeconds </reference/connection-string/#mongodb-urioption-urioption.maxstalenessseconds>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'maxStalenessSeconds' => 30 ];`` | ||
| **Connection URI Example**: ``maxStalenessSeconds=30`` | ||
|
||
* - :manual:`readPreferenceTags </reference/connection-string/#mongodb-urioption-urioption.readpreferencetags>` | ||
- | **Data Type**: ``array`` | ||
| **MongoDB\\Client Example**: | ||
|
||
.. code-block:: php | ||
|
||
$uriOptions = [ | ||
'readPreferenceTags' => [ | ||
[ 'region' => 'east' ], | ||
[ 'region' => 'west'] | ||
] | ||
];`` | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
**Connection URI Example**: ``readPreferenceTags=region:east,region:west`` | ||
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. If you review readPreferenceTags in the connection string options docs, you'll see this is incorrectly specifying a single tag set with conflicting Quoting the above docs:
The equivalent URI string segment for the previous code example (with an empty tag set), would thus be:
That seems a bit unwieldy, so I might suggest you just use the same example we see in the server manual. That conveniently shows both how to express a single tag set with multiple key/value pairs and express an empty tag set:
|
||
|
||
Authentication Options | ||
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. Isn't there an existing auth mechanism tutorial that we can link to, as you did for the TLS options above? I'm not sure if that existed at the original time you opened this PR. Or should this stay anyway since it's only three options (noting TLS has many more)? 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. No, I think it works well to link out instead. I do see that this page documented the 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. GSSAPI supports a few mechanism properties, two of which were originally top-level URI options before being deprecated: MONGODB-AWS also supports a |
||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`authMechanism </reference/connection-string/#mongodb-urioption-urioption.authmechanism>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'authMechanism' => 'MONGODB-X509' ];`` | ||
| **Connection URI Example**: ``authMechanism=MONGODB-X509`` | ||
|
||
* - :manual:`authMechanismProperties </reference/connection-string/#mongodb-urioption-urioption.authmechanismproperties>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: | ||
| ``$uriOptions = [ 'authMechanismProperties' => 'AWS_SESSION_TOKEN:12345' ];`` | ||
| | ||
| **Connection URI Example**: ``authMechanismProperties=AWS_SESSION_TOKEN:12345`` | ||
|
||
* - :manual:`authSource </reference/connection-string/#mongodb-urioption-urioption.authsource>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'authSource' => 'admin' ];`` | ||
| **Connection URI Example**: ``authSource=admin`` | ||
|
||
Server Selection and Discovery Options | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 30 70 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`localThresholdMS </reference/connection-string/#mongodb-urioption-urioption.localthresholdms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'localThresholdMS' => 20 ];`` | ||
| **Connection URI Example**: ``localThresholdMS=20`` | ||
|
||
* - :manual:`serverSelectionTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.serverselectiontimeoutms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'serverSelectionTimeoutMS' => 40000 ];`` | ||
| **Connection URI Example**: ``serverSelectionTimeoutMS=40000`` | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* - :manual:`serverSelectionTryOnce </reference/connection-string/#mongodb-urioption-urioption.serverselectiontryonce>` | ||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'serverSelectionTryOnce' => false ];`` | ||
| **Connection URI Example**: ``serverSelectionTryOnce=false`` | ||
|
||
* - :manual:`heartbeatFrequencyMS </reference/connection-string/#mongodb-urioption-urioption.heartbeatfrequencyms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'heartbeatFrequencyMS' => 50000 ];`` | ||
| **Connection URI Example**: ``heartbeatFrequencyMS=50000`` | ||
|
||
* - :manual:`socketCheckIntervalMS </reference/connection-string/#mongodb-urioption-urioption.socketcheckintervalms>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'socketCheckIntervalMS' => 4000 ];`` | ||
| **Connection URI Example**: ``socketCheckIntervalMS=4000`` | ||
|
||
Miscellaneous Configuration | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Connection Option | ||
- Description | ||
|
||
* - :manual:`appName </reference/connection-string/#mongodb-urioption-urioption.appname>` | ||
- | **Data Type**: {+string-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'appName' => 'myApp' ];`` | ||
| **Connection URI Example**: ``appName=myApp`` | ||
|
||
* - :manual:`retryReads </reference/connection-string/#mongodb-urioption-urioption.retryreads>` | ||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'retryReads' => false ];`` | ||
| **Connection URI Example**: ``retryReads=false`` | ||
|
||
* - :manual:`retryWrites </reference/connection-string/#mongodb-urioption-urioption.retrywrites>` | ||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'retryWrites' => false ];`` | ||
| **Connection URI Example**: ``retryWrites=false`` | ||
|
||
* - :manual:`loadBalanced </reference/connection-string/#mongodb-urioption-urioption.loadbalanced>` | ||
- | **Data Type**: {+bool-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'loadBalanced' => true ];`` | ||
| **Connection URI Example**: ``loadBalanced=true`` | ||
|
||
* - :manual:`srvMaxHosts </reference/connection-string/#mongodb-urioption-urioption.srvmaxhosts>` | ||
- | **Data Type**: {+int-data-type+} | ||
| **MongoDB\\Client Example**: ``$uriOptions = [ 'srvMaxHosts' => 5 ];`` | ||
| **Connection URI Example**: ``srvMaxHosts=5`` | ||
|
||
API Documentation | ||
----------------- | ||
|
||
For more information about the ``MongoDB\Client`` class, see the following {+driver-short+} | ||
API documentation: | ||
|
||
- :phpclass:`MongoDB\Client` | ||
|
||
For more information about the ``MongoDB\Driver\ReadPreference`` class, see the following | ||
{+extension-short+} API documentation: | ||
|
||
- `MongoDB\\Driver\\ReadPreference <https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__ | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// start-connection-uri | ||
// Replace the placeholders with your actual hostname, port, and path to the certificate key file | ||
$uri = "mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem"; | ||
|
||
// Create a MongoDB client | ||
$client = new MongoDB\Client($uri); | ||
// end-connection-uri | ||
|
||
// start-client-options | ||
// Replace the placeholders with your actual hostname and port | ||
$uri = "mongodb://<hostname>:<port>/"; | ||
|
||
// Set the connection options | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$uriOptions = [ | ||
'tls' => true, | ||
'tlsCertificateKeyFile' => '/path/to/file.pem' | ||
]; | ||
|
||
// Create a MongoDB client with the URI and options | ||
$client = new Client($uri, $uriOptions); | ||
// end-client-options |
Uh oh!
There was an error while loading. Please reload this page.