PHP cannot find the class MongoClient
Class 'MongoClient' not foundShort answer
MongoClient came from the legacy mongo extension, which MongoDB declared end-of-life. Current PHP uses the mongodb extension together with the mongodb/mongodb Composer library, and the class is MongoDB\Client. Install both, load the Composer autoloader, and change the code that builds the client.
Fix it fast
1Find out which cause you have
Find out which MongoDB classes your project uses and whether Composer's MongoDB library is installed. A MongoClient call means the code was written for the legacy extension.
grep -rn "MongoClient" --include=*.php . | head composer show mongodb/mongodb2Match what you see, then jump to its fix
- 1
The project calls new MongoClient(...), and composer show finds no mongodb/mongodb packageThe code was written for the legacy mongo extensionFix - 2
composer show lists mongodb/mongodb, but the script that fails never requires vendor/autoload.phpThe library is installed but the Composer autoloader is not includedFix - 3
The detect script prints that the extension is missing, or it passes on the command line while the web server failsThe mongodb extension is not loaded in the PHP that runs the appFix
- 1
What does the error look like?
Class 'MongoClient' not foundFatal error: Class 'MongoClient' not found
Cause 1 of 3
The code was written for the legacy mongo extension
1Check
The legacy driver's repository says it is end-of-life, with no further development, bug fixes or maintenance, and tells users to migrate to the new driver, ext-mongodb. The class in the new library is MongoDB\Client.
grep -rn "new MongoClient\|new Mongo(" --include=*.php .2Fix
Install the extension with PIE and the library with Composer, as the MongoDB PHP library's getting-started page shows, then build the client with MongoDB\Client.
pie install mongodb/mongodb-extension
composer require mongodb/mongodb
<?php
require_once __DIR__ . '/vendor/autoload.php';
// old: $client = new MongoClient("mongodb://localhost:27017");
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;3Verify
Run the detect script that ships with the library; it reports whether the extension is loaded.
php vendor/mongodb/mongodb/tools/detect-extension.phpConfirmed by mongodb.com
Cause 2 of 3
The library is installed but the Composer autoloader is not included
1Check
The library is a Composer package. Its classes are found only after the autoloader is loaded at the start of the script or in the application's bootstrap code.
grep -rn "vendor/autoload.php" --include=*.php . | head2Fix
Require the autoloader once, before any MongoDB class is used.
<?php
require_once __DIR__ . '/vendor/autoload.php';3Verify
Run the script again and confirm that the client is created.
php your-script.phpConfirmed by php.net
Cause 3 of 3
The mongodb extension is not loaded in the PHP that runs the app
1Check
The getting-started page points to phpinfo() and php_ini_loaded_file() to see which php.ini a PHP loads. The command line and the web server can use different ones.
php vendor/mongodb/mongodb/tools/detect-extension.php
php -r 'echo php_ini_loaded_file(), PHP_EOL;'2Fix
Install the extension with pie install mongodb/mongodb-extension, then make sure it is enabled in each php.ini that your command line and web server use. For the full steps on a missing Driver class, see the related page.
pie install mongodb/mongodb-extension3Verify
Run the detect script again, and load a page that calls phpinfo() through the web server.
php vendor/mongodb/mongodb/tools/detect-extension.phpConfirmed by mongodb.com
How do I stop it coming back?
- Update the extension and the library as a pair, using the versions the getting-started page shows.
- Port every call to MongoDB\Client instead of keeping any code that still expects the old classes.
- Run the detect script from the library in your deployment pipeline, so a missing extension stops the release and not the first request.
- Install the extension for the PHP that your web server runs, not only for your terminal.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP says the class MongoDB\Driver\Manager is not found
Fatal error: Class 'MongoDB\Driver\Manager' not found
MongoDB\Driver\Manager comes from the mongodb PHP extension, not from Composer.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix