PHP MongoDB:致命错误:找不到“MongoClient”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37261957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:08:05  来源:igfitidea点击:

PHP MongoDB: Fatal error: Class 'MongoClient' not found

phpmongodb

提问by Tom

When executing the following PHP code:

执行以下 PHP 代码时:

$m = new MongoClient("mongodb://localhost:27017");

I get the following error:

我收到以下错误:

Fatal error: Class 'MongoClient' not found in (...)

致命错误:在 (...) 中找不到类“MongoClient”

MongoDB extension seems properly installed (I copied php_mongodb.dllto ext folder and updated php.ini).

MongoDB 扩展似乎已正确安装(我复制php_mongodb.dll到 ext 文件夹并更新php.ini)。

PHP seems to confirm that the extension is running properly as the following code confirms it is loaded:

PHP 似乎确认扩展程序运行正常,因为以下代码确认它已加载:

echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";

Also, phpinfo()shows that mongodb extension has been loaded.

此外,phpinfo()显示 mongodb 扩展已加载。



UPDATE: my problem is still not solved.

更新:我的问题仍未解决。

phpinfo()clearly shows that the driver is loaded:

phpinfo()清楚地表明驱动程序已加载:

enter image description here

在此处输入图片说明

But I am still receiving the same fatal error.

但我仍然收到同样的致命错误。

回答by β.εηοιτ.βε

TL; DR

TL; DR

The class MongoClientis part of the legacy PECL package mongobut not anymore of the up-to-date mongodbpackage.

该类MongoClient是旧版 PECL 包mongo 的一部分,但不再是最新的mongodb包的一部分。

On MongoDB PHP driver github repo, the release note about the version 1.0.0, is suggesting developers to use MongoDB\Driver\Managerinstead of MongoClient

在 MongoDB PHP driver github repo 上,关于版本 1.0.0 的发行说明建议开发人员使用MongoDB\Driver\Manager而不是MongoClient

Changes from our legacy mongo extension

Most significantly, the legacy driver's MongoClient, MongoDB, and MongoCollection classes have been obsoleted by the MongoDB\Driver\Manager class, which is the new gateway for connecting and executing queries, commands, and write operations.

对我们旧版 mongo 扩展的更改

最重要的是,遗留驱动程序的 MongoClient、MongoDB 和 MongoCollection 类已被 MongoDB\Driver\Manager 类淘汰,MongoDB\Driver\Manager 类是连接和执行查询、命令和写入操作的新网关。

Source:https://github.com/mongodb/mongo-php-driver/releases/tag/1.0.0

来源:https : //github.com/mongodb/mongo-php-driver/releases/tag/1.0.0

So, here is the replacement class documentationand the snippet of code that should replace yours :

所以,这里是替换类文档和应该替换你的代码片段:

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");


As the documentation is prompting it, the class is deprecated.

由于文档提示,该类已被弃用。

WarningThis extension that defines this class is deprecated. Instead, the MongoDBextension should be used. Alternatives to this class include:

警告定义此类的此扩展已弃用。相反,应该使用MongoDB扩展。此类的替代方案包括:

Source:http://php.net/MongoClient

来源:http : //php.net/MongoClient



From what I read on their github repository release history, the class you are trying to use have been obsoleted since the version of mongodb 1.0.0, so, on the version 1.6.0you are, this class is not even part of the dllanymore.

从我在他们的 github 存储库发布历史中读到的内容,您尝试使用的类自 的版本以来已过时mongodb 1.0.0,因此,在1.6.0您所在的版本中,该类甚至dll不再是 的一部分。

That is confirmed by this issue on their github

他们的github上的这个问题证实了这一点

derickr commented on Apr 16

MongoClient is a class from the old legacy driver and is not supposed to be available in this one. The new driver has \MongoDB\Driver\Manager, and, the accompanying library has \MongoDB\Client.

You either need to install the old legacy extension (pecl install mongo) and use PHP 5.x, or update your code to use this new driver's classes as the old driver is not available for PHP 7. There is an upgrade guide at http://mongodb.github.io/mongo-php-library/upgrade-guide/

derickr 于 4 月 16 日发表评论

MongoClient 是来自旧的遗留驱动程序的一个类,不应该在这个类中可用。新驱动程序有\MongoDB\Driver\Manager,并且附带的库有\MongoDB\Client。

您需要安装旧的遗留扩展 (pecl install mongo) 并使用 PHP 5.x,或者更新您的代码以使用这个新驱动程序的类,因为旧驱动程序不适用于 PHP 7。http 上有升级指南 : //mongodb.github.io/mongo-php-library/upgrade-guide/

Source:https://github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288

来源:https : //github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288



Another way, as suggested by the MongoDB member quoted here above is to use this pecl extension: https://pecl.php.net/package/mongoinstead of https://pecl.php.net/package/mongodbbut please also notice the warning there stating:

另一种方法,正如上面引用的 MongoDB 成员所建议的那样是使用这个 pecl 扩展名:https://pecl.php.net/package/mongo而不是https://pecl.php.net/package/mongodb但也请请注意那里的警告说明:

This package has been superseded, but is still maintained for bugs and security fixes.

此软件包已被取代,但仍保留用于错误和安全修复。