Laravel框架使用MongoDB遇到的问题解决

之前说的,我一直是将macOS作为开发平台,直接在macOS系统配置Nginx PHP及MySQL,由于项目需求,需要使用到MongoDB,这篇就是一部在macOS系统下运行Laravel框架使用MongoDB遇到问题解决问题的流水账。

为了不污染正式开发的代码,我新建了一个Laravel工程,版本是5.5 LTS,MongoDB扩展使用的是laravel-mongodb,根据文档,安装该扩展的命令为:

composer require jenssegers/mongodb

但是一执行这条命令,就出现了一大堆的错误:

zocoMac:laravel-test zoco$ composer require jenssegers/mongodb
Using version ^3.4 for jenssegers/mongodb
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - jenssegers/mongodb v3.4.0 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.2.0, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2].
    - jenssegers/mongodb v3.4.1 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.2.0, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2].
    - jenssegers/mongodb v3.4.2 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.2.0, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2].
    - mongodb/mongodb 1.4.2 requires ext-mongodb ^1.5.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.4.1 requires ext-mongodb ^1.5.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.4.0 requires ext-mongodb ^1.5.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.3.2 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.3.1 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.3.0 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.2.0 requires ext-mongodb ^1.3.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.1.2 requires ext-mongodb ^1.2.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.1.1 requires ext-mongodb ^1.2.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.1.0 requires ext-mongodb ^1.2.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.5 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.4 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.3 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.2 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.1 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.0 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - Installation request for jenssegers/mongodb ^3.4 -> satisfiable by jenssegers/mongodb[v3.4.0, v3.4.1, v3.4.2].

  To enable extensions, verify that they are enabled in your .ini files:
    - /usr/local/etc/php/7.1/php.ini
    - /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.

可以看出,这一步说的是该扩展依赖PHP的MongoDB扩展,那我们动手装上就是了。

macOS系统安装PHP的MongoDB扩展

上次说了,我目前的开发环境是直接用Homebrew安装的Nginx、PHP、MySQL等组件,之前是可以直接用brew安装相应的PHP扩展的,比如说:

brew install php71-mongodb

但是从Homebrew 1.5.0开始,这种方法被遗弃了,会提示Error: No available formula with the name “php71-mongodb”。如果系统不自带相应的扩展,就需要我们手动使用pecl安装,根据PHP官方文档,在macOS/Linux/Unix系统下,只需这一行命令即可:

sudo pecl install mongodb

当然,那只是一般情况,在国内特色网络情况下,如果直接运行很可能会因为网络原因而报错,建议挂个命令行dl再进行安装试试。如果实在是下载不下来,也可以到这里下载对应版本的源码包进行手动安装。

如果自动安装成功,最终看到会有这么一段输出:

Build process completed successfully
Installing '/usr/local/Cellar/php@7.1/7.1.22/pecl/20160303/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.5.3
Extension mongodb enabled in php.ini

既然成功了,PHP里面肯定会有对应的MongoDB模块了,命令行下使用php -m可以列出所有已安装的模块。

接下来,再次运行上面哪行composer安装命令:

zocoMac:laravel-test zoco$ composer require jenssegers/mongodb
Using version ^3.4 for jenssegers/mongodb
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install jenssegers/mongodb v3.4.2
    - Conclusion: don't install jenssegers/mongodb v3.4.1
    - Conclusion: remove laravel/framework v5.5.43
    - Installation request for jenssegers/mongodb ^3.4 -> satisfiable by jenssegers/mongodb[v3.4.0, v3.4.1, v3.4.2].
    - Conclusion: don't install laravel/framework v5.5.43
    - jenssegers/mongodb v3.4.0 requires illuminate/events ^5.6 -> satisfiable by illuminate/events[v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.2, v5.7.3, v5.7.4, v5.7.5].
    - don't install illuminate/events v5.6.0|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.1|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.10|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.11|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.12|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.13|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.14|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.15|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.16|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.17|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.19|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.2|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.20|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.21|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.22|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.23|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.24|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.25|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.26|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.27|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.28|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.29|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.3|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.30|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.31|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.32|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.33|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.34|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.35|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.36|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.37|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.38|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.4|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.5|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.6|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.7|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.8|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.6.9|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.0|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.1|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.2|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.3|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.4|don't install laravel/framework v5.5.43
    - don't install illuminate/events v5.7.5|don't install laravel/framework v5.5.43
    - Installation request for laravel/framework (locked at v5.5.43, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.43].

放狗搜了一把也没找到问题出在哪里,但是无意中发现了一个解决办法,就是安装指定版本的jenssegers/mongodb,由于我的Laravel版本为5.5.x,最好的就是安装3.3.x版本的jenssegers/mongodb,命令如下:

composer require jenssegers/mongodb=3.3.*

./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing mongodb/mongodb (1.4.2): Downloading (100%)
  - Installing jenssegers/mongodb (v3.3.1): Downloading (100%)
jenssegers/mongodb suggests installing jenssegers/mongodb-session (Add MongoDB session support to Laravel-MongoDB)
jenssegers/mongodb suggests installing jenssegers/mongodb-sentry (Add Sentry support to Laravel-MongoDB)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: jenssegers/mongodb
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Package manifest generated successfully.

这下终于成功安装了jenssegers/mongodb。

后来查看jenssegers/mongodb文档的时候才发现,在jenssegers/mongodb文档安装命令下面就列出了相应的Laravel兼容版本,只是自己太大意没有仔细看文档:

Laravel version Compatibility

Laravel Package
4.2.x 2.0.x
5.0.x 2.1.x
5.1.x 2.2.x or 3.0.x
5.2.x 2.3.x or 3.0.x
5.3.x 3.1.x or 3.2.x
5.4.x 3.2.x
5.5.x 3.3.x
5.6.x 3.4.x

根据这个表格,哪个版本的Laravel安装哪个版本的jenssegers/mongodb开发者都有明确的说明,如果你的Laravel版本和我的不一致,可能需要在上述安装命令后面更改成对应的版本号。