You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XAMPP中PHP 8.1无法加载MongoDB扩展的问题求助

Laravel连接MongoDB:PHP CLI无法加载mongodb扩展但Web端正常的问题

问题描述

我正尝试将Laravel项目连接到MongoDB,PHP版本为8.1。下载了对应PHP8.1的线程安全x64版本MongoDB扩展,将php_mongodb.dll放入XAMPP的PHP目录下的ext文件夹中,并在php.ini中通过extension=mongodb启用该模块。

但使用php --ini检查扩展是否激活时,出现以下错误:

PHP Warning: PHP Startup: Unable to load dynamic library 'mongodb' (tried: ext\mongodb (The specified module could not be found), ext\php_mongodb.dll (The specified module could not be found)) in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'mongodb' (tried: ext\mongodb (The specified module could not be found), ext\php_mongodb.dll (The specified module could not be found)) in Unknown on line 0
Configuration File (php.ini) Path

ext文件夹中的文件确实命名为php_mongodb.dll,但仍无法加载。不过在http://localhost/dashboard/phpinfo.php中查看,MongoDB扩展已显示且包含所有可用选项。

尝试将php.ini中的配置改为extension=php_mongodb.dll并重启XAMPP,报错如下:

PHP Warning: PHP Startup: Unable to load dynamic library 'php_mongodb.dll' (tried: ext\php_mongodb.dll (The specified module could not be found), ext\php_php_mongodb.dll.dll (The specified module could not be found)) in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'php_mongodb.dll' (tried: ext\php_mongodb.dll (The specified module could not be found), ext\php_php_mongodb.dll.dll (The specified module could not be found)) in Unknown on line 0
Configuration File (php.ini) Path:
Loaded Configuration File: D:\xampp\php81\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)

可见配置中是否带前缀php或后缀.dll并非问题所在,且其他扩展如mysql通过extension=pdo_mysql即可正常启用,请问哪里操作出错了?

解决方法

  • 确认CLI与Web端使用的php.ini一致:查看Web端phpinfo中的Loaded Configuration File路径,对比CLI执行php --ini显示的路径。XAMPP经常会让Apache和CLI使用不同的php.ini(比如Apache用xampp/apache/bin/php.ini,CLI用xampp/php/php.ini),需将mongodb扩展配置添加到CLI对应的php.ini中。
  • 检查PHP CLI的TS/NTS版本:执行php -v,若输出包含NTS,说明CLI是非线程安全版本,需下载对应NTS版本的mongodb扩展替换现有文件。
  • 安装对应VC++运行库:PHP8.1依赖VC15/VC16版本的微软VC++ redistributable包,确保系统已安装该组件。
  • 检查扩展文件状态与路径:
    • 确认php_mongodb.dll未被杀毒软件隔离,右键文件属性看是否有“解除锁定”选项;
    • 将PHP根目录和ext目录添加到系统PATH环境变量,避免CLI找不到依赖文件。
  • 重启CLI会话:修改配置或环境变量后,关闭当前命令行窗口,重新打开再执行php -m检查扩展加载情况。

内容的提问来源于stack exchange,提问作者amalu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:50:07