Skip to content

Commit 1db15e2

Browse files
committed
DOCSP-50220: Break up FAQ
1 parent 83c6c62 commit 1db15e2

File tree

5 files changed

+251
-259
lines changed

5 files changed

+251
-259
lines changed

source/connect/client.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,75 @@ This example constructs a client and passes the following parameters:
118118
:language: php
119119
:copyable: true
120120

121+
Troubleshooting
122+
---------------
123+
124+
This section addresses issues that you might encounter when using a
125+
MongoDB client.
126+
127+
Client Persistence Errors
128+
~~~~~~~~~~~~~~~~~~~~~~~~~
129+
130+
Connections to the MongoDB deployment are handled by the ``libmongoc``
131+
library and the :php:`{+extension-short+} <mongodb>`. When you construct
132+
a :phpclass:`MongoDB\Client` instance, the {+library-short+} creates a
133+
:php:`MongoDB\Driver\Manager <class.mongodb-driver-manager>` instance by using the
134+
same connection string and options. The extension also uses those constructor
135+
arguments to derive a hash key for persistent ``libmongoc`` clients. If
136+
you previously persisted a ``libmongoc`` client by using a key, it is
137+
reused. Otherwise, a new ``libmongoc`` client is created and persisted
138+
for the lifetime of the PHP worker process. To learn more about
139+
this process, see the :php:`{+extension-short+} documentation
140+
<manual/en/mongodb.connection-handling.php>`.
141+
142+
Each ``libmongoc`` client maintains its own connections to the MongoDB deployment
143+
and a view of its topology. When you reuse a persistent ``libmongoc`` client, the
144+
{+library-short+} can avoid the overhead of establishing new connections and
145+
rediscovering the topology. This approach generally improves performance and is
146+
the driver's default behavior.
147+
148+
Persistent ``libmongoc`` clients are not freed until the PHP worker process
149+
ends. As a result, connections to a MongoDB deployment might remain open
150+
after a ``MongoDB\Driver\Manager`` object goes out of scope. While this is
151+
typically not an issue for applications that connect to one MongoDB deployment,
152+
it might cause errors in the following situations:
153+
154+
- PHP-FPM is configured with ``pm.max_requests=0``, so that workers never respawn, and a
155+
PHP application is deployed many times with small changes to its MongoDB
156+
connection string or options. This could lead to an accumulation of ``libmongoc``
157+
client objects in each worker process.
158+
159+
- An application occasionally connects to a separate MongoDB deployment in a
160+
backend component where request latency is not the most important aspect.
161+
162+
In the first case, restarting PHP-FPM as part of the application deployment
163+
allows the application to release any unused ``libmongoc`` clients and still use
164+
a persistent client for the latest connection string.
165+
166+
The second case requires a different solution. Specifying ``true`` for the
167+
``disableClientPersistence`` driver option instructs the {+library-short+} to
168+
create a new ``libmongoc`` client and ensure it is freed when the corresponding
169+
``MongoDB\Driver\Manager`` goes out of scope.
170+
171+
The following code demonstrates how to set the
172+
``disableClientPersistence`` option to ``true`` when creating a client:
173+
174+
.. code-block:: php
175+
:emphasize-lines: 6
176+
177+
<?php
178+
179+
$client = new MongoDB\Client(
180+
uri: getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/',
181+
uriOptions: [],
182+
driverOptions: ['disableClientPersistence' => true],
183+
);
184+
185+
.. note::
186+
187+
If you disable client persistence, the {+library-short+} requires more
188+
time to establish connections to the MongoDB deployment and discover its topology.
189+
121190
API Documentation
122191
-----------------
123192

source/connect/connection-options.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,93 @@ Miscellaneous Configuration
321321
| **MongoDB\\Client Example**: ``$uriOptions = ['srvMaxHosts' => 5];``
322322
| **Connection URI Example**: ``srvMaxHosts=5``
323323

324+
Troubleshooting
325+
---------------
326+
327+
This section addresses issues related to MongoDB connection options.
328+
329+
Server Selection Errors
330+
~~~~~~~~~~~~~~~~~~~~~~~
331+
332+
The following code shows possible server selection error
333+
messages that your application might generate:
334+
335+
.. code-block:: none
336+
:copyable: false
337+
338+
No suitable servers found (`serverSelectionTryOnce` set):
339+
[connection refused calling hello on 'a.example.com:27017']
340+
[connection refused calling hello on 'b.example.com:27017']
341+
342+
No suitable servers found: `serverSelectionTimeoutMS` expired:
343+
[socket timeout calling hello on 'example.com:27017']
344+
345+
No suitable servers found: `serverSelectionTimeoutMS` expired:
346+
[connection timeout calling hello on 'a.example.com:27017']
347+
[connection timeout calling hello on 'b.example.com:27017']
348+
[TLS handshake failed: -9806 calling hello on 'c.example.com:27017']
349+
350+
No suitable servers found: `serverselectiontimeoutms` timed out:
351+
[TLS handshake failed: certificate verify failed (64): IP address mismatch calling hello on 'a.example.com:27017']
352+
[TLS handshake failed: certificate verify failed (64): IP address mismatch calling hello on 'b.example.com:27017']
353+
354+
The {+extension-short+} usually represents these errors as
355+
:php:`MongoDB\Driver\Exception\ConnectionTimeoutException <mongodb-driver-exception-connectiontimeoutexception>`
356+
exceptions. However, the exception messages originate from
357+
``libmongoc``, which is the underlying system library used by the extension. Since
358+
these messages can take many forms, we recommend breaking down the structure of
359+
the message so you can better diagnose errors in your application.
360+
361+
Messages typically start with "No suitable servers found". The next part of
362+
the message indicates *how* server selection failed. The extension
363+
avoids a server selection loop and makes a single attempt by default, according to
364+
the ``serverSelectionTryOnce`` connection string option. If the extension is
365+
configured to use a loop, a message that includes the phrase "serverSelectionTimeoutMS expired"
366+
indicates that you exhausted its time limit.
367+
368+
The last component of the message tells us *why* server selection failed and
369+
includes one or more errors directly from the topology scanner, which is the
370+
service responsible for connecting to and monitoring each host. Any host that
371+
previously experienced an error during monitoring will be included in this list. These
372+
messages typically originate from low-level socket or TLS functions.
373+
374+
The following list describes the possible meanings of common phrases in the last error message
375+
component:
376+
377+
- "Connection refused": The remote host might not be not listening on
378+
the expected port.
379+
- "Connection timeout": There might be a routing problem, a firewall error, or
380+
a timeout due to latency.
381+
- "Socket timeout": You likely established an initial connection that
382+
was dropped or timed out due to latency.
383+
- "TLS handshake failed": TLS or OCSP verification did not succeed, and you might
384+
have misconfigured TLS certificates.
385+
386+
In the case of a connection failure, you can use the ``connect`` tool for
387+
more troubleshooting information. This tool tries to connect to each host in a
388+
connection string by using socket functions, and then attempts to interact with
389+
data. If you used Composer to install the library, you can use the following command
390+
to use the ``connect`` tool:
391+
392+
.. code-block:: none
393+
394+
php vendor/mongodb/mongodb/tools/connect.php <connection URI>
395+
396+
If the server you are connecting to does not accept connections, the output
397+
resembles the following code:
398+
399+
.. code-block:: none
400+
401+
Looking up MongoDB at <connection URI>
402+
Found 1 host(s) in the URI. Will attempt to connect to each.
403+
404+
Could not connect to <host>:<port>: Connection refused
405+
406+
.. note::
407+
408+
The tool supports only the ``mongodb://`` URI schema. Using the
409+
``mongodb+srv`` scheme is not supported.
410+
324411
API Documentation
325412
-----------------
326413

0 commit comments

Comments
 (0)