5

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:

  • Linux kernel 4.3.3
  • PHP Version 7.0.1
  • php-mongodb version 1.1.1
  • Seems properly configured, and mongodb is enabled and shown in phpinfo().
  • Tried installing the mongodb driver through pecl and my distro's package manager.

Other info that may be useful:

  • MongoDB Version 3.2.0
  • mongod is running
  • Also tried rebooting, no change.
dotVezz
  • 869

3 Answers3

7

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.

Here's one of the big Mongo libraries to use for PHP now: mongo-php-library.

dotVezz
  • 869
3

As you mention, the class structure to interact with MongoDB from PHP7 has changed and you would need to refactor your code (a lot depending on how it was initially planned and written).

One solution that you might want to look into is https://github.com/alcaeus/mongo-php-adapter

I've noticed that in most cases including the adapter library is all you need to do in order to have your old PHP5 code work properly.

Then I'd strongly suggest that you migrate your code to the new MongoDB PHP Library (https://github.com/mongodb/mongo-php-library) or that you use the native driver to benefit from the speed gains.

Tomi
  • 31
0
  • First we need to install MongoDB in the machine through the steps mentioned in official MongoDB website
  • The next step is to install a MongoDB drive to use with PHP.
  • The MongoDB driver for PHP Version 5.99.99 or older was: package/mongo to install this we need to use:

    sudo apt-get install php-pear php5-dev
    
    sudo pecl install mongo
    
  • From PHP 7 onwards, this is the new driver package/mongodb To install that use: sudo pecl install mongodb

  • If you are using Laravel framework or projects with composer, then this library is the most apt one: https://github.com/jenssegers/Laravel-MongoDB Use version 3.0.0 to get PHP 7 & Laravel 5 support and otherwise use the older version 2.2.2 Composer command:

    composer require jenssegers/mongodb
    
  • If you are using other PHP frameworks without composer, use this library: https://github.com/mongodb/mongo-php-library which is also used in the above mentioned library