Fatal error: Uncaught Error: Class ‘MongoDB\Driver\Manager’

Hello, I am trying to connect via the PHP driver. I have followed the installation procedures from MongoDB’s PHP Driver documentation regarding pecl install for the extension and composer install of mongodb into the root directory.

I have used the below code to launch the php driver and establish the connection to the database (note that I did change the inputs for db, pass, and cluster in the actual code).

<?php

require_once dirname( __FILE__ ) . '/vendor/autoload.php';

$client = new MongoDB\Client(
    'mongodb+srv://<my-database>:<my-password>@<my-cluster>.lkffbtl.mongodb.net/?retryWrites=true&w=majority');

var_dump( $client );

However, when I run this, I receive the following fatal error:

Fatal error : Uncaught Error: Class ‘MongoDB\Driver\Manager’ not found in /home3/coradase/public_html/cora-staging/wp-content/plugins/MongoDB/vendor/mongodb/mongodb/src/Client.php:124 Stack trace: #0 /home3/coradase/public_html/cora-staging/wp-content/plugins/MongoDB/conf.php(6): MongoDB\Client->__construct(‘mongodb+srv://c…’) #1 /home3/coradase/public_html/cora-staging/wp-content/plugins/MongoDB/mongodb.php(28): require_once(‘/home3/coradase…’) #2 /home3/coradase/public_html/cora-staging/wp-includes/class-wp-hook.php(307): cora_mongodb_admin_page(‘’) #3 /home3/coradase/public_html/cora-staging/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(‘’, Array) #4 /home3/coradase/public_html/cora-staging/wp-includes/plugin.php(476): WP_Hook->do_action(Array) #5 /home3/coradase/public_html/cora-staging/wp-admin/admin.php(259): do_action(‘toplevel_page_c…’) #6 {main} thrown in /home3/coradase/public_html/cora-staging/wp-content/plugins/MongoDB/vendor/mongodb/mongodb/src/Client.php on line 124

Client.php (the file referenced in the error code) is a default file that came with the Composer installation. I have not edited the file. The function in Client.php that contains line 124 (referenced in the error) is shown below:

    public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
    {
        $driverOptions += ['typeMap' => self::$defaultTypeMap];

        if (! is_array($driverOptions['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
        }

        if (isset($driverOptions['autoEncryption']['keyVaultClient'])) {
            if ($driverOptions['autoEncryption']['keyVaultClient'] instanceof self) {
                $driverOptions['autoEncryption']['keyVaultClient'] = $driverOptions['autoEncryption']['keyVaultClient']->manager;
            } elseif (! $driverOptions['autoEncryption']['keyVaultClient'] instanceof Manager) {
                throw InvalidArgumentException::invalidType('"keyVaultClient" autoEncryption option', $driverOptions['autoEncryption']['keyVaultClient'], [self::class, Manager::class]);
            }
        }

        $driverOptions['driver'] = $this->mergeDriverInfo($driverOptions['driver'] ?? []);

        $this->uri = (string) $uri;
        $this->typeMap = $driverOptions['typeMap'] ?? null;

        unset($driverOptions['typeMap']);

        $this->manager = new Manager($uri, $uriOptions, $driverOptions);
        $this->readConcern = $this->manager->getReadConcern();
        $this->readPreference = $this->manager->getReadPreference();
        $this->writeConcern = $this->manager->getWriteConcern();
    }

For reference, line 124 is:
$this->manager = new Manager($uri, $uriOptions, $driverOptions);

Again, this code comes directly from the composer installation of mongodb, and has not been edited at all.

I appreciate any insight form the team or anyone who has had this same problem in trying to debug so that the code will properly establish the connection to the database.

Thank you!

Did you remember to add the line extension=mongodb.so to the end of your /etc/php/8.1/cli/php.ini or whatever ini file is appropriate to your php installation?

Hi Jack - yes, I did add the extension line to php.ini.

For reference, I am trying to connect MongoDB to a web app running on Wordpress.

Well, what’s happening is that the classes built into the mongodb.so are not being found. Whatever the reason. Try loading php at the command line …

$ php
<?php phpinfo(); ?>

Then ctl-D to exit and PHP should spew a lot of configuration info.
Look for lines like:

mongodb

MongoDB support => enabled
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 => disabled
libmongoc ICU => enabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongoc compression zstd => disabled
libmongocrypt bundled version => 1.5.0
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto

and if you don’t find them, then the extension is not being loaded.

2 Likes

Hi
My extension have loaded, but get the same error. Also, I want to have connect in local db (it seems runing on 127.0.1.1) - what connection string should I use?

mongodb://user:password should be good enough assuming you really mean 127.0.0.1

Thanks for reply, but problem is still here
I use mongodb from another application and its connecting without prompt and also located in 127.0.1.1 (I use command mongodb --host 127.0.1.1) And in php file its:
$client = new MongoDB\Driver\Manager(‘mongodb://127.0.1.1’);
I try to change file Client.php with manually adding “127.0.1.1”, but it doesn`t work too

I’ve just tried to create a free DB on Atlas and I’ve used the string from docs

$serverApi = new ServerApi(ServerApi::V1); $client = new MongoDB\Client( 'mongodb+srv://user:<password>@<url>.mongodb.net/?retryWrites=true&w=majority', [], ['serverApi' => $serverApi]); $db = $client->test;

I have the same problem, so it’s something with mongodb/compose deployment

Just try to reboot server - it was a solution to me