简体   繁体   English

无法在Yii2上连接MongoDB ['未找到类'MongoDB \\ Driver \\ Manager'']

[英]Cannot connet MongoDB on Yii2 [ 'Class 'MongoDB\Driver\Manager' not found' ]

I'm starting to use Yii2, and I want to connecting it with Mongodb. 我开始使用Yii2,并且希望将其与Mongodb连接。 So I tried to follow the instruction from both website documents. 因此,我尝试遵循两个网站文档中的说明。

And seem everything good because I already made a simple test on my normal localhost, and it working fine. 看起来一切都很好,因为我已经在普通的本地主机上进行了一个简单的测试,并且工作正常。

but when I want to testing it on Yii2 Advanced template, I cannot connect Yii2 with Mongodb. 但是当我想在Yii2 Advanced模板上对其进行测试时,无法将Yii2与Mongodb连接。

it's always say the error about: 总是说关于以下内容的错误:

PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'MongoDB\Driver\Manager' not found'
in /Applications/MAMP/htdocs/yii/yii2-advanced/vendor/yiisoft/yii2-mongodb/Connection.php:355

Here the code on /yii2-advanced/frontend/web/index-test.php that I use: 这是我使用的/yii2-advanced/frontend/web/index-test.php上的代码:

<?php
// NOTE: Make sure this file is not accessible when deployed to production
if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
   die('You are not allowed to access this file.');
}

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$mongodb = new MongoClient("mongodb://root:1234567890@localhost:27017/yii2advanced");
$users = $mongodb->yii2advanced->users;
$user = $users->findOne();
print_r($user);

$config = require(__DIR__ . '/../config/test-local.php');

(new yii\web\Application($config))->run();

PS: PS:

  • I'm using MAMP PRO on Mac. 我在Mac上使用MAMP PRO。

  • I can get the good result of $user when I made a test on htdocs/index.php. 在htdocs / index.php上进行测试时,可以获得$ user的良好结果。

  • I'm sorry for my bad English. 对不起,我的英语不好。

After I searching and follow many instruction on the internet, I found the links below are useful for the new installer. 在搜索并遵循Internet上的许多说明后,我发现以下链接对于新安装程序很有用。

https:// docs.mongodb.com/master/tutorial/install-mongodb-on-os-x/?_ga=1.258923597.615822406.1480144338 https:// docs.mongodb.com/master/tutorial/install-mongodb-on-os-x/?_ga=1.258923597.615822406.1480144338

https://bigseadesign.com/web-development/how-to-install-mongo-php-osx-mamp/ https://bigseadesign.com/web-development/how-to-install-mongo-php-osx-mamp/

https://www.hugethoughts.com/blog/setup-mongodb-mamp-5-minutes/ https://www.hugethoughts.com/blog/setup-mongodb-mamp-5-minutes/

But I would notice to you that, when you wanna add extension=mongodb.so on the php.ini configuration I do recommend you to checking and put the correct path that you were installed MongoDB PHP Driver because, 但是我会通知您,当您想在php.ini配置上添加extension = mongodb.so时,我建议您检查一下并放入安装了MongoDB PHP Driver的正确路径,因为,

When I were installed "MongoDB PHP Driver" via brew 通过brew安装“ MongoDB PHP驱动程序”时

( http:// php.net/manual/en/mongodb.installation.homebrew.php ) (http:// php.net/manual/en/mongodb.installation.homebrew.php)

on Mac Os they will installed it this path: 在Mac OS上,他们将通过以下路径安装它:

/usr/local/opt/phpXX-mongodb/

So php.ini on your MAMP have to call mongo extention like this: 因此,您的MAMP上的php.ini必须像这样调用mongo扩展:

extension=/usr/local/opt/php56-mongodb/mongodb.so

By the way., Please note that the new "MongoDB PHP Driver" is "mongodb.so" not "mongo.so" because mongo.so already deprecated. 顺便说一句,请注意,新的“ MongoDB PHP驱动程序”是“ mongodb.so”而不是“ mongo.so”,因为mongo.so已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM