PHP Fatal error: Uncaught Error: Class 'MongoClient' not found

Hi there,

I have installed MongoBD Community Edition on Windows Server 2016 and enabled Remote Access as well as normal authentication i.e. User and Password. I have tested using Compass everything works fine.

I would like to access it using PHP 8.0 which is installed on IIS Server on a separate server (Server 2016). I downloaded MongoDB dll extension file and pasted in ext folder also added on php.ini

When running my php file with below code. I get the error Uncaught Error: Class ‘MongoClient’ not found

try
{
$m = new MongoClient("mongodb://mydbservername:27017", array("username" => "joe", "password" => "test"));
} 
catch (MongoDBDriverExceptionException $e) 
{
		echo 'Failed to connect to MongoDB, is the service intalled and running?<br /><br />';
		echo $e->getMessage();
		exit();
}

I also tried, MongoDB\Client , MongoDB\Driver\Manager. Please help

Hello @Wilbard_Mtei , welcome to the MongoDB Community!

You can check if the MongoDB extension has been loaded and active in PHP, by creating a page with

<?php
  phpinfo();
?>

and look at the output. You should see a “mongodb” section with the extension version etc. If you see that, MongoDB is active and well in PHP. If not, check for loading errors of the mongodb PHP extension.

phpinfo will also tell you which php.ini is currently active. Look for “Loaded Configuration File”

If outputting phpinfo() in a webpage is not convenient, you can use the following command lines

php --ini

Will show the curent php.ini path

php -m

Shows all the loaded PHP extensions. You should see ‘mongo’ among them, and if you don’t it means the extension has not been loaded, perhaps due to a misconfiguration. You’ll have to check the logs.

Next, it could be that the MongoDB PHP Library (PHPLIB) has not been been included in your PHP source file, or has a path issue. I’m not sure if you’re using Composer or are including files manually.

Let us know,
Hubert

1 Like

Thanks for your response @Hubert_Nguyen1 , I not using a compose I downloaded a DDL file from php.net . below is my php info.

@Wilbard_Mtei ,

The MongoClient class has been deprecated:
http://php.adamharvey.name/manual/en/class.mongoclient.php

Using the most recent PHPLIB, the init code looks like this:
$client = new \MongoDB\Client(CONNECTION_STRING );

There’s a great PHP setup tutorial here
https://www.mongodb.com/quickstart/php-setup

And another one that shows how to perform CRUD operations: