Class 'MongoDB\\Driver\\Manager' not found

2019-01-24 11:27发布

问题:

I'm integrating MongoDB with Laravel. I'm following information found in this link jenssegers/laravel-mongodb. I'm successful at integrating MongoDB. But when I run the code it throws this error

FatalErrorException in Client.php line 56: Class 'MongoDB\Driver\Manager' not found

Here is the code

Controller app/Http/Controller/NewController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\User;

class NewController extends Controller
{
    //
    public function index(){
        $var = User::all();
        var_dump($var);
    }
}

And here follows the user.php that is by default provided by Laravel when we create the project

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User  extends Eloquent 
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $collection = 'users_collection';
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Please tell me why am I getting this error.

Phpinfo --> mongodb section is coming. Here is a screen shot phpinfo

回答1:

I do not know if it's the most elegant solution, but it worked for me:

  1. Install the php driver $ sudo pecl install mongodb
  2. Create the extension file $ sudo nano /etc/php5/mods-available/mongodb.ini and write inside: extension=mongodb.so
  3. Create a symbolic link for this file $ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/apache2/conf.d/20-mongodb.ini
  4. Create an other symbolic link for this file $ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/cli/conf.d/20-mongodb.ini
  5. Restart apache or the server used $ sudo service apache2 restart

It may be necessary to reinstall jenssegers/mongodb: $ composer require jenssegers/mongodb



回答2:

You may also need to restart fpm on ubuntu/debian

sudo service php7.0-fpm restart

for php5

sudo service php5-fpm restart

Also, make sure extension=mongodb.so is in:

apache2/php.ini
cli/php.ini
fpm/php.ini

to check i do:

locate php.ini | xargs grep mongo

to see php.ini files:

php --ini


回答3:

As of 2017 this should work for PHP 5.6:

### Install MongoDB driver for PHP 5.6 ##############

echo -e "\n### Installing MongoDB PHP driver ##\n";
sudo apt-get install php-mongodb -y

# Make sure extension=mongodb.so is present

PhpConfigFile="/etc/php/5.6/fpm/php.ini"
if grep -q mongodb.so "$PhpConfigFile"; then
  echo "extension=mongodb.so already found, dont add."
else
  echo "extension=mongodb.so" | sudo tee "$PhpConfigFile"
  echo "Added extension=mongodb.so to $PhpConfigFile"
fi

It is part of my one-liner installer of PHP5.6 + nginx + MongoDB plugin for PHP, I've even made a domain for it: http://ubuntu-script.tk



回答4:

Maybe it could help you.

If you are using the new Wamp server, make sure that the php interpreter that your ide is using if the same use by wamp and where you have install the mongodb.dll.

I was having the same issue.

I resolved it when i installed mongodb.dll both on php 5.6.25 and 7.0.10, because my wamp server have the two versions. After the installation, i made a composer update. Now it is ok.



回答5:

I'm going to assume that you have php-mongodb installed correctly no matter what the process.

I hit this error when the mongo connection string was empty:

new MongoDB\Client('mongodb://');

Make sure your mongo connection string is populated properly.



回答6:

The answer can be found here: Why MongoDB Class doesn't work in Laravel?

apokryfos gives us the answer in the first comment:

Try new \MongoClient() instead

For anyone struggling to get the mongodb.so extension working for PHP, be careful, there are two mongo drivers for PHP. For the new driver follow these instructions: http://php.net/manual/en/mongodb.installation.manual.php

Make sure all your php.ini files contain:

extension=mongodb.so

From the root of your project:

composer require mongodb/mongodb

In your PHP file:

NOTE THE BACKSLASH BEFORE MongoDB

$client = new \MongoDB\Client("mongodb://127.0.0.1:27017");
$collection = $client->database->collection;

$cursor = $collection->find(
    [
        'some.nice.name' => 'true'
    ]
);