|
| 1 | +========================== |
| 2 | +Frequently Asked Questions |
| 3 | +========================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Common Extension Installation Errors |
| 14 | +------------------------------------ |
| 15 | + |
| 16 | +PHP Headers Not Found |
| 17 | +~~~~~~~~~~~~~~~~~~~~~ |
| 18 | + |
| 19 | +For example: |
| 20 | + |
| 21 | +.. code-block:: none |
| 22 | + |
| 23 | + /private/tmp/pear/install/mongodb/php_phongo.c:24:10: fatal error: 'php.h' file not found |
| 24 | + |
| 25 | + #include <php.h> |
| 26 | + ^~~~~~~ |
| 27 | + |
| 28 | +This error indicates that PHP's build system cannot find the necessary headers. |
| 29 | +All PHP extensions require headers in order to compile. Additionally, those |
| 30 | +headers must correspond to the PHP runtime for which the extension will be used. |
| 31 | +Generally, the ``phpize`` command (invoked by ``pecl``) will ensure that the |
| 32 | +extension builds with the correct headers. |
| 33 | + |
| 34 | +Note that the mere presence of a PHP runtime does not mean that headers are |
| 35 | +available. On various Linux distributions, headers are often published under a |
| 36 | +separate ``php-dev`` or ``php-devel`` package. On macOS, the default PHP runtime |
| 37 | +does not include headers and users typically need to install PHP (and headers) |
| 38 | +via `Homebrew <https://brew.sh/>`_ in order to build an extension. |
| 39 | + |
| 40 | +Multiple PHP Runtimes Installed |
| 41 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 42 | + |
| 43 | +If your system has multiple versions of PHP installed, each version will have |
| 44 | +its own ``pecl`` and ``phpize`` commands. Additionally, each PHP runtime may |
| 45 | +have separate ``php.ini`` files for each SAPI (e.g. FPM, CLI). If the extension |
| 46 | +has been installed but is not available at runtime, double-check that you have |
| 47 | +used the correct ``pecl`` command and have modified the appropriate ``php.ini`` |
| 48 | +file(s). |
| 49 | + |
| 50 | +If there is any doubt about the ``php.ini`` file being used by a PHP runtime, |
| 51 | +you should examine the output of :php:`phpinfo() <phpinfo>` for that particular |
| 52 | +SAPI. Additionally, :php:`php_ini_loaded_file() <php_ini_loaded_file>` and |
| 53 | +:php:`php_ini_scanned_files() <php_ini_scanned_files>` may be used to determine |
| 54 | +exactly which INI files have been loaded by PHP. |
| 55 | + |
| 56 | +To debug issues with the extension not being loaded, you can use the |
| 57 | +``detect-extension`` script provided in the tools directory. You can run this |
| 58 | +script from the CLI or include it in a script accessible via your web server. |
| 59 | +The tool will point out potential issues and installation instructions for your |
| 60 | +system. Assuming you've installed the library through Composer, you can call the |
| 61 | +script from the vendor directory: |
| 62 | + |
| 63 | +.. code-block:: none |
| 64 | + |
| 65 | + php vendor/mongodb/mongodb/tools/detect-extension.php |
| 66 | + |
| 67 | +If you want to check configuration for a web server SAPI, include the file in |
| 68 | +a script accessible from the web server and open it in your browser. Remember to |
| 69 | +wrap the script in ``<pre>`` tags to properly format its output: |
| 70 | + |
| 71 | +.. code-block:: php |
| 72 | + |
| 73 | + <pre><?php require(...); ?></pre> |
| 74 | + |
| 75 | +Loading an Incompatible DLL on Windows |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +Windows binaries are available for various combinations of PHP version, |
| 79 | +thread safety (TS or NTS), and architecture (x86 or x64). Failure to select the |
| 80 | +correct binary will result in an error when attempting to load the extension DLL |
| 81 | +at runtime: |
| 82 | + |
| 83 | +.. code-block:: none |
| 84 | + |
| 85 | + PHP Warning: PHP Startup: Unable to load dynamic library 'mongodb' |
| 86 | + |
| 87 | +Ensure that you have downloaded a DLL that corresponds to the following PHP |
| 88 | +runtime properties: |
| 89 | + |
| 90 | +- PHP version (``PHP_VERSION``) |
| 91 | +- Thread safety (``PHP_ZTS``) |
| 92 | +- Architecture (``PHP_INT_SIZE``) |
| 93 | + |
| 94 | +In addition to the aforementioned constants, these properties can also be |
| 95 | +inferred from :php:`phpinfo() <phpinfo>`. If your system has multiple PHP |
| 96 | +runtimes installed, double-check that you are examining the ``phpinfo()`` output |
| 97 | +for the correct environment. |
| 98 | + |
| 99 | +The aforementioned ``detect-extension`` script can also be used to determine the |
| 100 | +appropriate DLL for your PHP environment. |
| 101 | + |
| 102 | +Server Selection Failures |
| 103 | +------------------------- |
| 104 | + |
| 105 | +The following are all examples of |
| 106 | +:doc:`Server Selection </tutorial/server-selection>` failures: |
| 107 | + |
| 108 | +.. code-block:: none |
| 109 | + |
| 110 | + No suitable servers found (`serverSelectionTryOnce` set): |
| 111 | + [connection refused calling hello on 'a.example.com:27017'] |
| 112 | + [connection refused calling hello on 'b.example.com:27017'] |
| 113 | + |
| 114 | + No suitable servers found: `serverSelectionTimeoutMS` expired: |
| 115 | + [socket timeout calling hello on 'example.com:27017'] |
| 116 | + |
| 117 | + No suitable servers found: `serverSelectionTimeoutMS` expired: |
| 118 | + [connection timeout calling hello on 'a.example.com:27017'] |
| 119 | + [connection timeout calling hello on 'b.example.com:27017'] |
| 120 | + [TLS handshake failed: -9806 calling hello on 'c.example.com:27017'] |
| 121 | + |
| 122 | + No suitable servers found: `serverselectiontimeoutms` timed out: |
| 123 | + [TLS handshake failed: certificate verify failed (64): IP address mismatch calling hello on 'a.example.com:27017'] |
| 124 | + [TLS handshake failed: certificate verify failed (64): IP address mismatch calling hello on 'b.example.com:27017'] |
| 125 | + |
| 126 | +These errors typically manifest as a |
| 127 | +:php:`MongoDB\Driver\Exception\ConnectionTimeoutException <mongodb-driver-exception-connectiontimeoutexception>` |
| 128 | +exception from the driver. The actual exception messages originate from |
| 129 | +libmongoc, which is the underlying library used by the PHP driver. Since these |
| 130 | +messages can take many forms, it's helpful to break down the structure of the |
| 131 | +message so you can better diagnose errors in your application. |
| 132 | + |
| 133 | +Messages will typically start with "No suitable servers found". The next part of |
| 134 | +the message indicates *how* server selection failed. By default, the PHP driver |
| 135 | +avoids a server selection loop and instead makes a single attempt (according to |
| 136 | +the ``serverSelectionTryOnce`` connection string option). If the driver is |
| 137 | +configured to utilize a loop, a message like "serverSelectionTimeoutMS expired" |
| 138 | +will tell us that we exhausted its time limit. |
| 139 | + |
| 140 | +The last component of the message tells us *why* server selection failed, and |
| 141 | +includes one or more errors directly from the topology scanner, which is the |
| 142 | +service responsible for connecting to and monitoring each host. Any host that |
| 143 | +last experienced an error during monitoring will be included in this list. These |
| 144 | +messages typically originate from low-level socket or TLS functions. |
| 145 | + |
| 146 | +The following is not meant to be exhaustive, but will hopefully point you in the |
| 147 | +right direction for analyzing the contributing factor(s) for a server selection |
| 148 | +failure: |
| 149 | + |
| 150 | +- "connection refused" likely indicates that the remote host is not listening on |
| 151 | + the expected port. |
| 152 | +- "connection timeout" could indicate a routing or firewall issue, or perhaps |
| 153 | + a timeout due to latency. |
| 154 | +- "socket timeout" suggests that a connection *was* established at some point |
| 155 | + but was dropped or otherwise timed out due to latency. |
| 156 | +- "TLS handshake failed" suggests something related to TLS or OCSP verification |
| 157 | + and is sometimes indicative of misconfigured TLS certificates. |
| 158 | + |
| 159 | +In the case of a connection failure, you can use the ``connect`` tool to try and |
| 160 | +receive more information. This tool attempts to connect to each host in a |
| 161 | +connection string using socket functions to see if it is able to establish a |
| 162 | +connection, send, and receive data. The tool takes the connection string to a |
| 163 | +MongoDB deployment as its only argument. Assuming you've installed the library |
| 164 | +through Composer, you would call the script from the vendor directory: |
| 165 | + |
| 166 | +.. code-block:: none |
| 167 | + |
| 168 | + php vendor/mongodb/mongodb/tools/connect.php mongodb://127.0.0.1:27017 |
| 169 | + |
| 170 | +In case the server does not accept connections, the output will look like this: |
| 171 | + |
| 172 | +.. code-block:: none |
| 173 | + |
| 174 | + Looking up MongoDB at mongodb://127.0.0.1:27017 |
| 175 | + Found 1 host(s) in the URI. Will attempt to connect to each. |
| 176 | + |
| 177 | + Could not connect to 127.0.0.1:27017: Connection refused |
| 178 | + |
| 179 | +.. note:: |
| 180 | + |
| 181 | + The tool only supports the ``mongodb://`` URI schema. Using the |
| 182 | + ``mongodb+srv`` scheme is not supported. |
0 commit comments