安装 PHP 7 MongoDB 客户端/驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34486808/
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 00:02:14  来源:igfitidea点击:

Installing the PHP 7 MongoDB Client/Driver?

phpmongodbphp-7php-mongodb

提问by Vladimir Fazilov

I am very eager to start working with PHP 7 however one issue is getting in the way... I primarily use MongoDB for the database, and the problem is that I don't know how to install the MongoDB driver/client for PHP 7.

我非常渴望开始使用 PHP 7,但是有一个问题正在妨碍...我主要使用 MongoDB 作为数据库,问题是我不知道如何为 PHP 7 安装 MongoDB 驱动程序/客户端.

My current installation is PHP 5.6 and on my Mac and brew install php56-mongodoes the trick.

我当前的安装是 PHP 5.6,在我的 Mac 上brew install php56-mongo可以解决这个问题。

Can anyone recommend how I can get this working on my Mac or an Ubuntu install?

谁能推荐我如何在我的 Mac 或 Ubuntu 安装上使用它?

Thanks in advance and much appreciated!

提前致谢,非常感谢!

回答by Daniel W.

The Mongo extension for PHP Version 5.99.99 or olderhas been superseded:

PHP 5.99.99 或更早版本的 Mongo 扩展已被取代:

https://pecl.php.net/package/mongo

https://pecl.php.net/package/mongo

Use the newer one for PHP Version 7.99.99or older instead:

对 PHP 7.99.99或更早版本使用较新的版本:

https://pecl.php.net/package/mongodb

https://pecl.php.net/package/mongodb

You can install a PECL/PEAR extension automatically:

您可以自动安装 PECL/PEAR 扩展:

pecl install mongodb

ormanually.

手动

The classeshave been changed too:

课程已被更改过:

new \MongoClient(); // legacy class!

see http://php.net/manual/en/book.mongo.php

http://php.net/manual/en/book.mongo.php

new \MongoDB\Driver\Manager(); // new classes! 

see http://php.net/manual/en/set.mongodb.php

http://php.net/manual/en/set.mongodb.php

Additional information regarding compatibility can be found here:

可以在此处找到有关兼容性的其他信息:

https://docs.mongodb.org/ecosystem/drivers/php/#compatibility

https://docs.mongodb.org/ecosystem/drivers/php/#compatibility

回答by developerHyman

The MongoDB driver that supports PHP 7 was only released December 22nd- its likely downstream repositories like brew haven't updated.

支持 PHP 7 的 MongoDB 驱动程序仅在 12 月 22 日发布- 其可能的下游存储库(如 brew)尚未更新。

Update confirmed there is currently no php70-mongobrew script, though there is an active pull requestto add one.

更新确认目前没有php70-mongobrew 脚本,尽管有一个活动的拉取请求来添加一个。

You may be able to install it manually via peclin the meantime:

您可以同时通过 pecl 手动安装它

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

回答by Jorge Omar MH

You can try install mongodb driver with:

您可以尝试使用以下命令安装 mongodb 驱动程序:

sudo apt-get install php-mongodb

回答by Ish Kav

How to connect php 7.0 with MongoDB in ubuntu 16.04 lts?

如何在 ubuntu 16.04 lts 中将 php 7.0 与 MongoDB 连接?

1)Install LAMP using the following link. It installs Apache2, mysql and php 7.0. https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

1) 使用以下链接安装 LAMP。它安装 Apache2、mysql 和 php 7.0。 https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

2)Install the MongoDB community Edition on Ubuntu using the steps in the following link. http://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

2) 使用以下链接中的步骤在 Ubuntu 上安装 MongoDB 社区版。 http://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

3)Type the following command to get the mongoDB extension from pecl

3)输入以下命令从pecl获取mongoDB扩展

sudo apt install php-pear

4)Add the following to the php.ini file at /etc/php/apache2/7.0

4)将以下内容添加到php.ini文件中 /etc/php/apache2/7.0

extension=mongodb.so

Important - The classes have been changed too:

重要 - 类也已更改:

new MongoClient();  //Old Class

new MongoDB\Driver\Manager(); // New Class

Refer - http://php.net/manual/en/set.mongodb.php

参考 - http://php.net/manual/en/set.mongodb.php

回答by Will

No, the legacy driver does not support PHP7, unfortunately. Here's the commitand the JIRA Ticketwhere this was officially finalized.

不,很遗憾,旧版驱动程序不支持 PHP7。这里的提交JIRA票,其中此正式敲定。

The new PHP MongoDB driver can be found in PECL here(or GitHub).

新的 PHP MongoDB 驱动程序可以在 PECL 中找到(或GitHub)。

To install, just:

要安装,只需:

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

The documentation for the new driver can be found here. I'd like to include a note from the documentation:

可以在此处找到新驱动程序的文档。我想包含文档中的注释:

Ultimately, this extension is not intended to be used alone. Users should considering using this driver alongside one or more userland PHP libraries, such as mongo-php-library.

最终,此扩展不打算单独使用。用户应该考虑将此驱动程序与一个或多个用户级 PHP 库一起使用,例如mongo-php-library

The new mongodbdriver / PHP extension is a lot more low-level than the legacy mongodriver, and you are encouraged to use a higher-level library on top of the driver rather than using it directly in your code.

新的mongodb驱动程序/PHP 扩展比旧mongo驱动程序低得多,我们鼓励您在驱动程序之上使用更高级别的库,而不是直接在代码中使用它。

The Mongo PHP Library(releases) is the official high-level library for PHP, and it's what is recommended to use in your projects. It's still in Beta, but this still seems to be the safest and most-future-proof path forward with PHP7.

蒙戈PHP库发行版)是PHP官方高层库,它是什么,建议在项目中使用。它仍处于 Beta 阶段,但这似乎仍然是 PHP7 最安全、最适合未来发展的途径。

It might be possible for someone to port the legacy driver to PHP7, but there probably isn't much of a need for it, as there are many other problems with the legacy driver.

有人可能会将旧版驱动程序移植到 PHP7,但可能没有太大必要,因为旧版驱动程序还有许多其他问题。

回答by ajayarjunan

回答by FredTheWebGuy

I almost gave up, too. For The MongoDB driver for PHP 7x, Ubuntu 18.04 Pecl will not work. Instead, try:

我也差点放弃了。对于 PHP 7x 的 MongoDB 驱动程序,Ubuntu 18.04 Pecl 将不起作用。相反,请尝试:

sudo apt-get install php-mongodb  

Then in the base of your project folder install the mongodb library https://docs.mongodb.com/php-library/current/tutorial/install-php-library/

然后在您的项目文件夹的底部安装 mongodb 库https://docs.mongodb.com/php-library/current/tutorial/install-php-library/

composer require mongodb/mongodb
composer install

Which accesses the lower level functions provided by the driver.

它访问驱动程序提供的较低级别的功能。

Lastly, go to php.ini and add

最后,转到 php.ini 并添加

extension = mongo.so

and restart apache

并重新启动apache

To test, try adding this to a php file:

要进行测试,请尝试将其添加到 php 文件中:

<?php
     require_once __DIR__ . "/vendor/autoload.php";
     $collection = (new MongoDB\Client)->test->users;
     print_r($collection);
?>

回答by Guillermo García

Complementing answers and publishing what worked for me:

补充答案并发布对我有用的内容:

1 followed this guide in order to install lamp https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04(The third step is needed only for installing the mongo client)

1 按照本指南安装灯https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04(第三步只需要安装mongo客户端)

2 $ sudo apt-get install php7.0-dev

2 $ sudo apt-get install php7.0-dev

3 $ sudo pecl install mongodb

3 $ sudo pecl install mongodb

4 $ sudo nano /etc/php/7.0/apache2/php.ini

4 $ sudo nano /etc/php/7.0/apache2/php.ini

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

在文件中添加以下行:

扩展 = mongo.so;

(您可能需要指定文件的确切位置。就我而言,该文件位于 /usr/lib/php/20151012/mongodb.so。)

And thats all for installing just the mongo client for php 7.0

这就是为 php 7.0 安装 mongo 客户端的全部内容

I am complementing the Pransh Tiwari answer

我正在补充 Pransh Tiwari 的回答

回答by Aleksandar

Old question, but new excellent solution. Just use Mongostead7automated script for installing all needed stuff. Worked for me just fine. No additional work needed.

老问题,但新的优秀解决方案。只需使用Mongostead7自动化脚本来安装所有需要的东西。为我工作得很好。不需要额外的工作。

Use it as follows:

使用方法如下:

sudo curl -sS https://raw.githubusercontent.com/zakhttp/Mongostead7/master/mongoHomestead7.sh | sudo sh

回答by Parth Vora

This worked for me on Ubuntu for PHP7:

这在 Ubuntu for PHP7 上对我有用:

sudo apt-get install php7.0-mongodb