Skip to content

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

Merged
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
45739f4
wip
mongoKart Aug 21, 2024
1df7cf0
formatting
mongoKart Aug 22, 2024
7d07a4d
format
mongoKart Aug 22, 2024
d505ef1
test
mongoKart Aug 22, 2024
5fcd405
test
mongoKart Aug 22, 2024
aa3a2f3
test
mongoKart Aug 22, 2024
d7e5227
test
mongoKart Aug 22, 2024
0dbe1e6
test
mongoKart Aug 22, 2024
916ed85
test
mongoKart Aug 22, 2024
73dc918
test
mongoKart Aug 22, 2024
9767bfd
test
mongoKart Aug 22, 2024
bf43a6c
test
mongoKart Aug 22, 2024
6bfb367
test
mongoKart Aug 22, 2024
3a4ead0
test
mongoKart Aug 22, 2024
d68b868
test
mongoKart Aug 22, 2024
8f5a381
test
mongoKart Aug 22, 2024
2296609
make one column
mongoKart Aug 22, 2024
c90ad5d
test
mongoKart Aug 22, 2024
b31f3fc
test
mongoKart Aug 22, 2024
790ffdf
test
mongoKart Aug 22, 2024
8ca2f3d
test
mongoKart Aug 22, 2024
fa9622a
test
mongoKart Aug 22, 2024
69584e5
test
mongoKart Aug 22, 2024
96999b3
test
mongoKart Aug 22, 2024
3d777f0
test
mongoKart Aug 22, 2024
5779b96
add emphasis
mongoKart Aug 22, 2024
3c2a761
remove extra file
mongoKart Aug 22, 2024
a1470a6
add source constant
mongoKart Aug 22, 2024
57e6728
Apply suggestions from code review
mongoKart Aug 22, 2024
db5a290
feedback
mongoKart Aug 22, 2024
b6088f8
feedback
mongoKart Sep 11, 2024
7cd7c30
phpclass
mongoKart Sep 11, 2024
fa842ac
Merge remote-tracking branch 'upstream/php-standardization' into docs…
mongoKart Sep 27, 2024
5a519a4
feedback
mongoKart Sep 27, 2024
2eb98ea
add toc link
mongoKart Sep 27, 2024
20a4817
stable api link
mongoKart Sep 27, 2024
dca4565
Apply suggestions from code review
mongoKart Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ toc_landing_pages = [
"/indexes",
"/security",
"/data-formats",
"/upgrade"
"/upgrade",
]

sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

[substitutions]
php-library = "MongoDB PHP Library"
Expand All @@ -46,3 +46,6 @@ stable-api = "Stable API"
library-short = "PHP library"
api = "https://www.mongodb.com/docs/php-library/current/reference"
php-manual = "https://www.php.net/manual/en"
string-data-type = "``string``"
bool-data-type = "``bool``"
int-data-type = "``int``"
4 changes: 1 addition & 3 deletions source/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ Connect to MongoDB

/connect/client
/connect/connection-targets
/connect/tls

.. TODO:
/connect/connection-options
/connect/tls
/connect/stable-api

Overview
Expand Down
328 changes: 328 additions & 0 deletions source/connect/connection-options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
.. _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.

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 ``$uriOptions`` parameter and in the connection
URI, the value in ``$uriOptions`` takes precedence.

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.

.. important:: Percent-Encoding

If the value of a connection option contains special characters, you must
:wikipedia:`percent-encode <Percent-encoding>` the value before including it
in the connection URI. You can use the ``rawurlencode()`` method to encode
these values according to the URI syntax specified in RFC 3986.

Don't percent-encode connection options when including them in the
``$uriOptions`` parameter.

To learn more, see the following resources:

- `RFC 3986 <http://www.faqs.org/rfcs/rfc3986.html>`__
- `rawurlencode <{+php-manual+}/rawurlencode>`__ in the PHP manual

Replica Set Options
~~~~~~~~~~~~~~~~~~~

.. list-table::
:header-rows: 1
:stub-columns: 1

* - Connection Option
- Description

* - :manual:`directConnection </reference/connection-string-options/#mongodb-urioption-urioption.directConnection>`
- | **Data Type**: {+bool-data-type+}
| **MongoDB\\Client Example**: ``$uriOptions = ['directConnection' => true];``
| **Connection URI Example**: ``directConnection=true``

* - :manual:`replicaSet </reference/connection-string-options/#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-options/#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-options/#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-options/#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-options/#mongodb-urioption-urioption.zlibcompressionlevel>`
- | **Data Type**: {+int-data-type+}
| **MongoDB\\Client Example**: ``$uriOptions = ['zlibCompressionLevel' => 3];``
| **Connection URI Example**: ``zlibCompressionLevel=3``

Write Concern Options
~~~~~~~~~~~~~~~~~~~~~

.. list-table::
:header-rows: 1
:stub-columns: 1

* - Connection Option
- Description

* - :manual:`w </reference/connection-string-options/#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-options/#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-options/#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-options/#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-options/#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-options/#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-options/#mongodb-urioption-urioption.readpreferencetags>`
- | **Data Type**: ``array``
| **MongoDB\\Client Example**:

.. code-block:: php

$uriOptions = [
'readPreferenceTags' => [
['dc' => 'ny', 'rack' => 'r1'],
[],
],
];

**Connection URI Example**: ``readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=``

Authentication Options
Copy link
Member

Choose a reason for hiding this comment

The 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)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 authMechanismProperties option, which I don't see anywhere on the Authentication page. Does the PHP lib. support any mechanisms that use this option?

Copy link
Member

Choose a reason for hiding this comment

The 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: gssapiServiceName and canonicalizeHostname (still documented for Manager::__construct()).

MONGODB-AWS also supports a AWS_SESSION_TOKEN property, but I see that wasn't demonstrated in #139 (cc: @norareidy).

~~~~~~~~~~~~~~~~~~~~~~

To learn about the authentication options available in the {+driver-short+}, see
:ref:`Authentication Mechanisms. <php-auth>`

Server Selection and Discovery Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. list-table::
:header-rows: 1
:stub-columns: 1
:widths: 30 70

* - Connection Option
- Description

* - :manual:`localThresholdMS </reference/connection-string-options/#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-options/#mongodb-urioption-urioption.serverselectiontimeoutms>`
- | **Data Type**: {+int-data-type+}
| **MongoDB\\Client Example**: ``$uriOptions = ['serverSelectionTimeoutMS' => 5000];``
| **Connection URI Example**: ``serverSelectionTimeoutMS=40000``

* - :manual:`serverSelectionTryOnce </reference/connection-string-options/#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-options/#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-options/#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-options/#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-options/#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-options/#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-options/#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-options/#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>`__
22 changes: 22 additions & 0 deletions source/includes/connect/connection-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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
// Replace the placeholder with the actual path to the certificate key file
$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
2 changes: 0 additions & 2 deletions source/reference/class/MongoDBClient.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. _php-api-mongodbclient:

=====================
MongoDB\\Client Class
=====================
Expand Down
Loading