Well here's an odd situation I've found. I've been using PHP and MongoDB together on a project, and updated the associated software. After the upgrade, I did make sure to update the new conf files and all is well --- except when I try to use the MongoClient
class, I get the following fatal error:
Fatal error: Uncaught Error: Class 'MongoClient' not found in /srv/http/test.php
So my first instinct was to check my /etc/php/php.ini
to make sure that mongodb.so was correctly included. I found it after the other extension
lines:
extension=mcrypt.so
extension=zip.so
extension=mongodb.so ; <--- here it is
With that checked, I loaded up a quick phpinfo()
script to check that I was using the right php.ini
file, and confirmed that I am. And not only that, the mongodb driver also appears to be loaded correctly!
Loaded Configuration File /etc/php/php.ini
. . .
mongodb support enabled
mongodb version 1.1.1
mongodb stability stable
libmongoc version 1.3.1-dev
libbson version 1.3.0
So I restarted httpd and tried again. No dice, still can't find the MongoClient
class. So I tried to install it from pecl
instead of my distribution's package manager, and the situation is the same as described above.
So here's a quick rundown of the situation:
phpinfo()
.pecl
and my distro's package manager.Other info that may be useful:
mongod
is runningStill not working? If you experience problems after a PHP upgrade, you can always revert to an earlier version. To do this, use the PHP version tool in cPanel. For more information about how to change the PHP version for your web site, please see this article. You may receive deprecation errors when you view your web site after a PHP upgrade.
Then there’s PHP 7.0, which reached its end of life on December 3, 2018. It, too, is no longer a supported version of PHP. Still, almost 20% of WordPress sites are on PHP 7.0. If you’re thinking, “wasn’t PHP 7.0 just released?!”
Not only will upgrading give you immediate performance gains, but your site will also be better protected against security vulnerabilities found in older versions of PHP. Before upgrading your site, make sure you check your site’s compatibility with the PHP version you want to switch to and run a backup.
Upgrading PHP on Your Own Server If you administer your own server, you can upgrade to PHP 7.2 yourself using the migration guides provided in the documentation at php.net. It’s important that you pay close attention to new features and functions, as well as any deprecated features that might impact your site.
Well I found the answer.
The class has been changed. It's no longer \MongoClient
, and is now \MongoDB\Driver\Manager
All that I need is a refactor. The new driver is designed to be a lower-level driver for high-level functionality to be filled with userspace libraries, so anyone else upgrading to php7, make sure you check that you'll be comfortable with the new driver, or that the userspace libraries exist when you do it. Hopefully this question/answer helps others in the future.
Commenter ianaz below has posted this good userspace library for MongoDB: http://mongodb.github.io/mongo-php-library/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With