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|