Installing the PHP 7 MongoDB Client/Driver?

2022-08-30 13:16:31

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.

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

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

Thanks in advance and much appreciated!


答案 1

The Mongo extension for PHP Version 5.99.99 or older has been superseded:

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

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

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

You can install a PECL/PEAR extension automatically:

pecl install mongodb

or manually.

The classes have been changed too:

new \MongoClient(); // legacy class!

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

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

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

Additional information regarding compatibility can be found here:

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


答案 2

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

Update confirmed there is currently no brew script, though there is an active pull request to add one.php70-mongo

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

pecl channel-update pecl.php.net

pecl install mongodb

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

推荐