MongoDB Atlas connection from Wordpress/Php throws segmentation fault

Hi :wave:
I’m trying to connect to MongoDB Atlas from my Wordpress theme.
But I get a segmentation fault error.

I’m using the snippet of code provided by the Atlas interface:


use Exception;
use MongoDB\Client;

$uri = mongodb+srv://<username>:<password>@mondocluster.iyop7xn.mongodb.net/?retryWrites=true&w=majority;

// Create a new client and connect to the server
$client = new MongoDB\Client($uri);

try {
    // Send a ping to confirm a successful connection
    $client->selectDatabase('admin')->command(['ping' => 1]);
    echo "Pinged your deployment. You successfully connected to MongoDB!\n";
} catch (Exception $e) {
    printf($e->getMessage());
}

This is the error I get in the server log:

[Sun Aug 13 18:28:48.497295 2023] [core:notice] [pid 48484] AH00052: child pid 68759 exit signal Segmentation fault (11)

That PHP code in a freshly installed WordPress version fails and produced the server error.
If I run the same PHP code in a standalone PHP application everything works fine.

Any idea on how to investigate the problem? Where shall I look?

My environment:

  • PHP Version 8.0.30
  • MongoDB extension version 1.16.1
  • Server version: Apache/2.4.57
  • macOS 13.3

Hello @Giulio_Andreini , and welcome to the MongoDB Community forums! I’ve used MongoDB with WordPress for an eCommerce application, so I’m always curious about how others use MongoDB with WP!

Could you give us more context about how you integrated the MongoDB PHP Library into your WordPress theme? Did you use Composer to include the library and its dependencies?

Also, when looking with step-by-step debugging, the $client object is valid?

Let us know,
Hubert

Hi @Hubert_Nguyen1,
thanks for your answer.

I built a forecast service for surfers called Mondo Surf (mondo.surf) and I recently took all the forecast logic out of WordPress and built a dedicated PHP service.

The service writes data in MongoDB Atlas and then I need to retrieve all this forecast data from my WordPress application.
I store in MongoDB the weekly forecast data for each surf spot, so I need to retrieve the JSON with the forecast every time a user opens a page of a surf spot.
I can connect to MongoDB Atlas from my server/production version but not from my local computer (I added both IPs in Network Access).

I added MongoDB via composer ( "mongodb/mongodb": "^1.16").
In the vendor folder I have:

  • jean85
  • MongoDB
  • symfony

This is what the $client object looks like:

I keep trying and still get the same segfault error:
[Mon Aug 14 15:48:20.524888 2023] [core:notice] [pid 80719] AH00052: child pid 55145 exit signal Segmentation fault (11)

Also, the connection to MongoDB works correctly from a local test PHP application. It stops working (and generating the segfault) when I move it inside WordPress (even a fresh new installation).

Let me know if you need more information
Thanks in advance for any help you could provide!

best

Thanks for the additional data. The client seems to be created OK.

The AH00052 is specific to Apache (or one of the modules), and is a memory crash error :confused:

Since isolating the MongoDB code execution outside of WP works, I wondered… In WordPress, some of the PHP settings concerning memory size or buffer sizes may be different (overwritten in wp-config or even .htaccess ) and could be worth looking at.

Disabling Apache modules not needed might help too.

You mention the same code works on the production server. Is it fair to assume you’re replicating the same setup as much as possible on your local system? Are you running this locally on a Mac directly or via a VM using a Linux flavor?

Hi @Hubert_Nguyen1
I removed completely the .htaccess file and in the wp-config.php I just have the basic initial setup of WP (DB configuration, authentication unique keys, WP_DEBUG, ABSPATH).
I tried to increase the WP_MEMORY_LIMIT but with no success.

These are the active modules in my httpd.conf:
mpm_prefork_module, authn_file_module, authn_core_module, authz_host_module, authz_groupfile_module, authz_user_module, authz_core_module, access_compat_module, auth_basic_module, socache_shmcb_module, reqtimeout_module, filter_module, mime_module, log_config_module, env_module, headers_module, setenvif_module, version_module, ssl_module, unixd_module, status_module, autoindex_module, dir_module, alias_module, rewrite_module, php_module

I tried to randomly remove some of these but with no success (very often the local server gets broken).

I’m running this on my mac directly (no VM).

Thanks for looking at these potential friction points.

I wondered if there’s a crash report in the Apache logs with more details. There might be an indication as for which library or module crashes.

Hi @Hubert_Nguyen1
I’ve spent a lot of time on this, and tried everything that came up to my mind.

One question: is it possible to query MongoDB Atlas using a cURL request from PHP? (and not using the MongoDB library)

Best
g

Hi @Giulio_Andreini ,

The data API might be what you’re looking for: https://www.mongodb.com/docs/atlas/app-services/data-api/

Keep in mind that it’s not as efficient as using the driver, but sometimes it’s handy to have HTTPS access for devices that don’t support a driver for various reasons (IoT etc…)

For the segmentation fault, it’s hard to track what the issue is. Another alternative to consider is using containers to avoid these compatibility-type issues.

I’ve used http://devilbox.org/ (it’s a pre-configured Docker-based tool) in the past and it is quick to get a WP website up and running. MongoDB is supported out of the box and if you need anything else, there are ways to customize things.

Cheers,
Hubert