Connecting to MongoDB from WordPress ( PHP )

Hi,

I am trying to connect to my atlas cluster from my WordPress website. I am using the basic example recommended to me from my mongoDB atlas dashboard. Below is the code with swapped out username & password. After the code is the error I am receiving along with my phpinfo.

I am able to connect to the cluster using other apps like react apps etc. There are no outgoing hosting firewalls or IP blocking from my cluster.

<?php
use MongoDB\Client;

// Include the Composer autoloader to load the MongoDB PHP Library
require_once __DIR__ . '/vendor/autoload.php';

// Replace the placeholder with your actual Atlas connection string
$uri = 'mongodb+srv://username:password@cluster0.fme2j91.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('dbname)->command(['ping' => 1]);
    echo "Pinged your deployment. You successfully connected to MongoDB!\n";
} catch (Exception $e) {
    printf("Failed to connect to MongoDB: %s", $e->getMessage());
}

Result

Failed to connect to MongoDB: No suitable servers found (serverSelectionTryOnce set): [connection closed calling hello on 'ac-3jxjrla-shard-00-00.fme2j91.mongodb.net:27017'] [connection closed calling hello on 'ac-3jxjrla-shard-00-01.fme2j91.mongodb.net:27017'] [connection closed calling hello on 'ac-3jxjrla-shard-00-02.fme2j91.mongodb.net:27017']

PHP info

|MongoDB extension version|1.14.0|
|---|---|
|MongoDB extension stability|stable|
|libbson bundled version|1.22.0|
|libmongoc bundled version|1.22.0|
|libmongoc SSL|enabled|
|libmongoc SSL library|OpenSSL|
|libmongoc crypto|enabled|
|libmongoc crypto library|libcrypto|
|libmongoc crypto system profile|disabled|
|libmongoc SASL|enabled|
|libmongoc ICU|enabled|
|libmongoc compression|enabled|
|libmongoc compression snappy|disabled|
|libmongoc compression zlib|enabled|
|libmongoc compression zstd|enabled|
|libmongocrypt bundled version|1.5.0|
|libmongocrypt crypto|enabled|
|libmongocrypt crypto library|libcrypto|

Any mongodb side log to share?

looks like the server side closes the connection upon “hello”.

I was unable to find any logs via cpanel. I have written to my hosting company and asked about this.

My host wrote me with

I am sorry, but it is not possible to use MongoDB with our hosting plans. If you want to use mongodb the best solution is to use av VPS.

So dead end to my little project :confused: I guess I should delete this post from the forum?

Thanks

@Martin_Kariuki, welcome to the MongoDB Community forums!
It looks like the MongoDB Extension is up and running, and the PHP code can access the PHP driver and create an instance. Instead, it seems like a connection issue when connecting to your MongoDB instance.

If you use Atlas, did you allow the server’s IP to access the cluster?

Can you share which hosting company and server product/plan you’re using?

I wrote an article about MongoDB PHP errors. There are a few tests that may be of help:

https://www.mongodb.com/developer/languages/php/php-error-handling/
Let me know,

Thanks!

Hi Hubert,

Thanks for your response! Turns out it was a hosting problem. Everything worked well after I switched to a new hosting provider :slight_smile:

1 Like

Great! I can’t wait to see what you’re doing with WordPress and MongoDB. Keep us posted~