Asked  7 Months ago    Answers:  5   Viewed   40 times

I'm trying to execute this:

<?php

// connect
$m = new Mongo();

?>

But I get the following error:

Fatal error: Class 'Mongo' not found in C:wampwww.....


Info:

  • PHP: 5.38
  • MongoDB: mongodb-win32-i386-2.0.2
  • MongoDB PHP Driver: mongo-1.1.4-php5.3vc6ts
  • WampServer: 2.2
  • OS: Windows 7 Enterprise SP1

I've followed the MongoDB installation fine (incl. adding the DLL to ext & php.ini), I've started mongod.exe and WAMP is running.

What does this imply is missing/incorrect?


Edit:

I have got it running on another Windows 7 machine (Windows 7 Profressional). That got me curious. So now my Windows 7 Enterprise SP1 machine has the same http.conf, php.ini, C:wampbinmongodb contents (therefore DBs are the exact same) and the exact same batch file to start mongod.exe as my Win7 Professional machine.

But still, all I can do on my Win7 Enterprise SP1 machine is connect via command line. My Win7 Professional machine's phpinfo() contains:
Mongo section in php.ini My Win7 Enterprise SP1 machine doesn't contain any reference to MongoDB.

The contents of the batch file I use to start MongoDB:

"C:wampbinmongodbbinmongod.exe" --logpath "C:wampbinmongodblogserror.log" --logappend --dbpath "C:wampbinmongodbdatadb" --directoryperdb --service
Net Start "MongoDB"
pause

 Answers

41

The problem was the PHP driver (php_mongo.dll). Apache/WAMP just did not like the mongo-1.1.4-php5.3vc6ts version for some reason. My Windows 7 Professional machine had the mongo-1.2.5-php5.3vc9ts version. That was the only difference between the two machines.

I originally had been using the mongo-1.2.5-php5.3vc9ts version of the driver on the Windows 7 Enterprise machine but changed to the mongo-1.1.4-php5.3vc6tsversion when I was troubleshooting the problem. So the original problem could actually just have been that the database path datadb didn't exist/couldn't be found (and needed to be specified using --dbpath when running/starting mongod.exe).

Wednesday, March 31, 2021
 
iceduck
answered 7 Months ago
63

TL;DR

The class MongoClient is part of the legacy PECL package mongo but not anymore of the up-to-date mongodb package.

And since you have the mongodb extension installed, and not the mongo one, this is why you are getting the error

Fatal error: Class 'MongoClient' not found

On MongoDB PHP driver github repo, the release note about the version 1.0.0, is suggesting developers to use MongoDBDriverManager instead of MongoClient

Changes from our legacy mongo extension

Most significantly, the legacy driver's MongoClient, MongoDB, and MongoCollection classes have been obsoleted by the MongoDBDriverManager class, which is the new gateway for connecting and executing queries, commands, and write operations.

Source:: https://github.com/mongodb/mongo-php-driver/releases/tag/1.0.0

So, here is the replacement class documentation and the snippet of code that should replace yours :

$m = new MongoDBDriverManager("mongodb://localhost:27017");

As the documentation is prompting it, the class is deprecated.

Warning This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:

  • MongoDBDriverManager

Source: http://php.net/MongoClient


From what I read on their github repository release history, the class you are trying to use have been obsoleted since the version of mongodb 1.0.0, so, on the version 1.6.0 you are, this class is not even part of the dll anymore.

That is confirmed by this issue on their github

derickr commented on Apr 16

MongoClient is a class from the old legacy driver and is not supposed to be available in this one. The new driver has MongoDBDriverManager, and, the accompanying library has MongoDBClient.

You either need to install the old legacy extension (pecl install mongo) and use PHP 5.x, or update your code to use this new driver's classes as the old driver is not available for PHP 7. There is an upgrade guide at http://mongodb.github.io/mongo-php-library/upgrade-guide/

Source: https://github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288


Another way, as suggested by the MongoDB member quoted here above is to use this pecl extension: https://pecl.php.net/package/mongo instead of https://pecl.php.net/package/mongodb but please also notice the warning there stating:

This package has been superseded, but is still maintained for bugs and security fixes.

Wednesday, March 31, 2021
 
superfell
answered 7 Months ago
86

Try to change the timezone in the php.ini configuration file, and then restart the apache service. You should have php.ini somewhere inside your WAMP installation folder.

EDIT: You might have the php.ini file inside the folder:

/wamp/bin/php/phpX.X.X

where phpX.X.X is your php version.

Look for the "date.timezone" line and change it to something like this:

date.timezone = "America/Los_Angeles"

You can find the supported timezones here: http://www.php.net/manual/en/timezones.php

More technical information is available here: http://php.net/manual/en/datetime.configuration.php

The .htaccess file has a local range, normally to the folder and sub-folders where the file is created. Changing the php.ini file makes the changes global to your PHP installation.

Wednesday, March 31, 2021
 
keisar
answered 7 Months ago
90

As you mentioned you're using the new Mongo extension for PHP 7.

The class names have changed from the older version, i.e.

MongoClient is now MongoDBDriverManager

MongoDate is now MongoDBBSONUTCDateTime

I'm not sure how backwards compatible everything is, but this should get you started!

Wednesday, March 31, 2021
 
Powering
answered 7 Months ago
10

sadly this probably won't ever help anyone else who has this same problem, but I was able to get everything working by just changing the port to 465.

Monday, August 16, 2021
 
Justin Charles
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share