Laravel-mongodb: Class 'MongoClient' not found?

Created on 10 Sep 2013  Â·  35Comments  Â·  Source: jenssegers/laravel-mongodb

Class 'MongoClient' not found in Connection.php on line 132. This is the error received when I try to run migrations - ($ php artisan migrate). Any Ideas? Thanks in advance.

Most helpful comment

If your "php -i" shows that Mongo enabled, but you're still getting the same error, that means you need to restart something. Lost a few hours before it downed on me to restart php-fpm.

Solution for php-fpm (Ubuntu):

  1. Create new file in /etc/php5/cli/conf.d/ :
    sudo vi /etc/php5/cli/conf.d/20-mongo.ini
  2. Type "extension=mongo.so" in that file and save file.
  3. After that you need to restart php-fpm service:
    sudo service php5-fpm restart

As you have now have two declaration "extension=mongo.so", it would be better to comment line "extension=mongo.so" in your php.ini

All 35 comments

Does this only happen when using migrations? Because MongoClient is the native PHP class.

Looks like an issue with the php-mongo extension not being loaded by PHP. Check phpinfo() and see if the Mongo extension is being loaded, if not check your php.ini file or load the extension through a separate mongo.ini file in conf.d

What is I get the same error (Class 'MongoClient' not found) but Mongo is enabled within PHP.
I also checked phpinfo() within laravel to make sure it loads it (and not only via cli/configuration files).

Any idea?

Same problem @cloud-ops

Can you create an instance of MongoClient without this library? I don't think this error is related to this library, but with your Mongo installation.

Thanks @brunocascio , you last (above) comment was very helpful :smile:

There is also a new project that is creating a MongoDB driver that you can include using composer: https://github.com/mongofill/mongofill

But I have not tested it yet.

Same here @brunocascio, that last comment helped me too, thanks :smile_cat:

Same here @brunocascio, your last comment helped me.
As soon as i setup mongo PHP driver message disappears

@jenssegers I am getting this same error. I have confirmed via a phpinfo() statement that the Mongo driver has loaded

My experience using Ubuntu is that once the PHP MongoDB driver is correctly installed you will have to add the line extension = mongo.so to both the /etc/php5/cli/php.ini and /etc/php/apache2/php.ini.

I had the exact error and that is what solved it for me.

If your "php -i" shows that Mongo enabled, but you're still getting the same error, that means you need to restart something. Lost a few hours before it downed on me to restart php-fpm.

Solution for php-fpm (Ubuntu):

  1. Create new file in /etc/php5/cli/conf.d/ :
    sudo vi /etc/php5/cli/conf.d/20-mongo.ini
  2. Type "extension=mongo.so" in that file and save file.
  3. After that you need to restart php-fpm service:
    sudo service php5-fpm restart

As you have now have two declaration "extension=mongo.so", it would be better to comment line "extension=mongo.so" in your php.ini

It can be a Mongo integration problem, but if it isn't, and if like me, you are using native MongoDB commands with PHP, try to put a backslashe before the class, like so: "\MongoClient()"

I had the same problem . after I upgraded my Yosemite to 10.10.5 . all answers above did not solved my problem . my php --ini displayed that the mongo drive is loaded . But still got the error .
and php -v showed that I am running php version 5.6.
What worked for me eventually copying the libexec/apace2/phpLibModule to the apace libexec directory .
It turned out that the apache was running version 5.5 .

Thanks man. I am using Linux Mint. To solve this issue i added "extension=mongo.so" (Without quotes) in two directories -

  1. /etc/php5/cli/php.ini
  2. /etc/php5/apache2/php.ini

I have same problem on php new release, 7.0.2 . it has no mongoClient anymore and uses mongoDB extension! Now how can I use jenssegers/laravel-mongodb in my new server? please help about this asap.

@a-ghasemi There's a separate branch for this. Will be released as a new major version soon.

Thanks for your afforts
On Sat, 30 Jan 2016 at 14:08, Jens Segers [email protected] wrote:

@a-ghasemi https://github.com/a-ghasemi There's a separate branch for
this. Will be released as a new major version soon.

—
Reply to this email directly or view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/36#issuecomment-177144533
.

is there expected release date for support mongodb 3.2?

@mvladk Version 2.3.x works well with mongodb 3.2.

@jenssegers I see your 3.0 branch that uses mongodb pecl extension but I can't figure out how to install this with composer. if I put in "3.*" in my composer.json it yells at me because you haven't released it yet. How might I be able to use this or is there a release date for this?

@jensk it might work well with mongodb 3.2 but php 7 now uses a new pecl extension called "mongodb" instead of "mongo" and this is causing the "MongoClient not found" error. That is what he is talking about.

@bgarrison25 Use the master branch.

"jenssegers/mongodb": "dev-master"

I think the 3.0 version is not finished yet. I use the master branch to do all my test before shipping to php7 server. The "MongoClient not found" is because you are using a old mongo client.

I had a diferente issue, but I guess you can get rid of this message, compiling _libmongoc_ again, and installing the mongo client via pecl. Look up https://github.com/mongodb/mongo-php-driver/issues/219#issuecomment-179858933

@bgarrison25 Indeed, php 7 requires a new driver called mongodb. (only supported by the current master branch, no release yet) Though, in my post I just answered the question about mongodb 3.2 support. ;-)

@bgarrison25, exactly. finally it some how worked with "mongodb" and composer
"laravel/framework": "5.1.*",
"jenssegers/mongodb": "~3.0",

@mvladk - thank you. Upgrading composer to ~3.0 jenssegers/mongodb fixed it for me.

Running Windows + php 7.0.1 + mongodb 3.2.1

composer.json
"laravel/framework": "5.2.*",
"jenssegers/mongodb": "^3.0"

php -i | grep
mongodb support => enabled
mongodb version => 1.1.2
mongodb stability => stable
libmongoc version => 1.1.2

The versioning is from:
https://pecl.php.net/package/mongodb

Some tips:

1) Make sure CLI php and apache php is the same (as mentioned before). Hopefully composer will fail on installation without the proper mongodb extension.
2) initially I had the .dll named as "mongo.dll" ... I recommend keeping it as "php_mongo.dll".
3) Make sure php is in Windows' path
3) http://php.net/manual/en/mongodb.installation.windows.php

The following php code runs fines on command line, but it failed to open in browser, with error class not found. There's one possible cause is that even the phpinfo() page shows /etc/php-zts.d/mongodb.ini is loaded, but it doesn't show anything else for mongodb module, the mongodb.so file is already defined in the ini file, spent long time can't figure out the reason, pls advise how to proceed?

Fatal error: Class 'MongoDB\Driver\Manager' not found in /var/www/vendor/mongodb/mongodb/src/Client.php on line 56

test.php

$filename='/var/www/ee14dev/vendor/autoload.php';
require $filename;
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>

For all having problems with MongoClient function on php7 please check the following link:
http://stackoverflow.com/questions/34486808/installing-the-php-7-mongodb-client-driver

Basically as php7 uses a new driver class for MongoDB they has changed the function to be called.
Hope this can help

Having the same issue as @goopter. Lets revisit this issue here on github please

Thanks to @Rents answer I fixed this issue.
If you installed PHP with brew, do brew services restart php70 or php56 if you use version 5.6.x.

I do not know but all the things above did not work for me.

Here is my workaround:

Install MongoDB client
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Install MongoDB driver for PHP
http://www.bictor.com/2015/02/15/installing-mongodb-for-php-in-ubuntu-14-04/

If you come across with configure: error: sasl.h not found! error, use:

sudo apt-get install libsasl2-dev

for CentOS:
yum install cyrus-sasl-devel

I have the same problem on homestead with php 5.6 - laravel give me the same error but strange thing is I can work with mongo in php artisan tinker and I can migrate but in browser it shows : Class 'MongoDB\Driver\Manager' not found. I'm using "jenssegers/mongodb": "^3.1", in my composer.json file.

i got the error as Fatal error: Class 'Mongoclient' not found in D:\xampp\htdocs\mongo.php on line 3
for fixing this to get mongodb driver i have added this extension=php_mongodb.dll in php.ini file, but still the error was not fixed. can anyone please help me to get out of this issue

Hi! For those who use vagrant/homestead (also different versions of jenssegers/mongodb for two projects on one homestead)and as I stumbled on this problem, my solution will be useful. So, homestead (6.4.0) use php cli 7.1 as default and command 'php artisan' run by default cli. So if you face 'MongoClient' problem, that means you use not right cli version. In order to use right cli version you have to specify 'php5.6 artisan' or 'php7.1 artisan'. Also you must have right extension in php.ini. mongo.so - for old projects, mongodb.so - for new projects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yupangestu picture yupangestu  Â·  3Comments

sanjay1688 picture sanjay1688  Â·  3Comments

phuocduy1988 picture phuocduy1988  Â·  3Comments

viacheslavpleshkov picture viacheslavpleshkov  Â·  3Comments

pirmax picture pirmax  Â·  3Comments