MongoDB PHP 扩展在 macOS XAMPP 上安装失败

问题描述 投票:0回答:1

我正在尝试在我的 MacBook Pro M1 中安装 MongoDB PHP 扩展。我在安装过程中遇到以下错误。

我用于安装的命令

sudo pecl install mongodb

错误

/private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/pear/temp/mongodb/src/libmongoc/src/libmongoc/src/mongoc/mongoc-scram.c:85:10: fatal error: 'unicode/usprep.h' file not found
#include <unicode/usprep.h>
         ^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [src/libmongoc/src/libmongoc/src/mongoc/mongoc-scram.lo] Error 1
ERROR: `make' failed

这是

which php
which pecl
命令的输出

/Applications/XAMPP/bin//php
/Applications/XAMPP/bin//pecl

PHP版本

PHP 8.1.4 (cli) (built: Mar 17 2022 03:15:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.4, Copyright (c) Zend Technologies

我尝试过的(遇到同样的错误)

  1. sudo /Applications/XAMPP/bin/pecl 安装 mongodb
  2. sudo /Applications/XAMPP/xamppfiles/bin/pecl 安装 mongodb
  3. 尝试过这个解决方案在macos上安装xampp的mongodb扩展
  4. brew 安装 icu4c

Mac版本:

Ventura 13.3.1 (a) 

php mongodb macos xampp php-mongodb
1个回答
0
投票

我在运行后遇到了一些类似的问题

sudo pecl install mongodb

将会显示以下错误信息

Build process completed successfully
Installing '/opt/homebrew/Cellar/php/8.3.9/pecl/20230831/mongodb.so'

Warning: mkdir(): File exists in System.php on line 294
PHP Warning:  mkdir(): File exists in /opt/homebrew/Cellar/php/8.3.9/share/php/pear/System.php on line 294

Warning: mkdir(): File exists in /opt/homebrew/Cellar/php/8.3.9/share/php/pear/System.php on line 294
ERROR: failed to mkdir /opt/homebrew/Cellar/php/8.3.9/pecl/20230831

最后,通过删除

中的“pecl”文件解决了这个问题
/opt/homebrew/Cellar/php/8.3.9

并创建一个名为“pecl”的新文件夹,并在其中创建一个文件夹“20230831”。

之后运行

sudo pecl install mongodb

问题又解决了。

之后在 php.ini 文件中添加 mongodb.so 文件的位置,如下所示

;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop
extension="/opt/homebrew/Cellar/php/8.3.9/pecl/20230831/mongodb.so"

; The MIBS data available in the PHP distribution must be installed.
; See https://www.php.net/manual/en/snmp.installation.php
;extension=snmp

;extension=soap
;extension=sockets

我的例子中的 php.ini 文件路径是

/opt/homebrew/etc/php/8.3/php.ini

现在,你可以跑步了

php --ri mongodb

检查安装是否成功,并应给出以下输出

╰─ php --ri mongodb                                                          ─╯


mongodb

MongoDB support => enabled
MongoDB extension version => 1.19.3
MongoDB extension stability => stable
libbson bundled version => 1.27.2
libmongoc bundled version => 1.27.2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc SRV => enabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongoc compression zstd => enabled
libmongocrypt bundled version => 1.10.0
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
crypt_shared library version => unknown

Directive => Local Value => Master Value
mongodb.debug => no value => no value

在此之后您可以添加以下代码

Backend/config/database.php

文件

'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'options' => [
    'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
],

确保您已正确设置“.env”文件。

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=swedesh_intern
DB_USERNAME=
DB_PASSWORD=

现在,您已准备就绪,只需运行迁移即可

php artisan migrate

这就是解决问题所需的全部内容。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.