Overview
This page describes the MongoDB connection and authentication options available in the PHP library.
Set Connection Options
You can configure your connection by specifying options in the connection URI or by passing them to the MongoDB\Client constructor.
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:
// 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);
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:
// 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);
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 MongoDB Server manual.
Important
Percent-Encoding
If the value of a connection option contains special characters, you must percent-encode 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:
rawurlencode in the PHP manual
Replica Set Options
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: |
Connection Options
TLS Options
To learn about the TLS options available in the PHP library, see the TLS page.
Timeout Options
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: |
Compression Options
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: |
Write Concern Options
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: | |
Data Type: |
Read Concern Options
Connection Option | Description |
|---|---|
Data Type: |
Read Preference Options
Connection Option | Description | ||||||
|---|---|---|---|---|---|---|---|
Data Type: MongoDB\Driver\ReadPreference | |||||||
Data Type: | |||||||
Data Type: Connection URI Example: |
Authentication Options
To learn about the authentication options available in the PHP library, see Authentication Mechanisms.
Server Selection and Discovery Options
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: | |
Data Type: | |
Data Type: | |
Data Type: | |
| Data Type: |
| Data Type: |
Miscellaneous Configuration
Connection Option | Description |
|---|---|
Data Type: | |
Data Type: | |
Data Type: | |
Data Type: | |
Data Type: |
Note
Load Shedding in Intelligent Workload Management (IWM)
If errors with SystemOverloadedError or RetryableError labels are causing failures in your application or appearing in your application logs, Load Shedding may be active on your cluster and your driver may not be upgraded to a version that supports it. We recommend upgrading your MongoDB PHP Library to version 2.3 or later. If you continue to see these errors after upgrading, you can address them through server-side changes, such as reviewing your Load Shedding configuration, or through application-side changes, such as implementing custom error handling or client-side request throttling. Depending on your situation, application-side changes may be preferable or necessary.
Load Shedding is a feature of IWM, and helps manage server load by rejecting excess operations during sustained overload. This feature is available on MongoDB 9.0 and later.
For more information about IWM or handling overload errors, see the Intelligent Workload Management page or Overload Errors page in the Atlas documentation.
API Documentation
For more information about the MongoDB\Client class, see the following PHP library API documentation:
For more information about the MongoDB\Driver\ReadPreference class, see the following PHP extension API documentation: