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

Docker部署PHP网站出现MongoClient PHP类不存在问题求助

Fixing "MongoClient Class Not Found" with Docker PHP-FPM

Hey there, let's break down why you're hitting this error even though your phpinfo shows the mongodb extension is loaded:

The Core Issue: Two Different MongoDB Extensions

You've installed the official modern mongodb extension (evidenced by docker-php-ext-mongodb.ini), but MongoClient belongs to the old, deprecated mongo extension—they're two completely separate packages.

The modern mongodb extension uses the MongoDB\Client class (with a namespace) instead of the old MongoClient. That's why your code can't find the class, even though the extension is present.

Step-by-Step Fixes

1. Update Your Code to Use the Modern Class

If you control the codebase, replace instances of:

$client = new MongoClient();

With the modern namespace-aware class:

// Option 1: Use the full namespace
$client = new MongoDB\Client();

// Option 2: Add a use statement at the top of your file
use MongoDB\Client;
$client = new Client();

2. Verify the Extension is Properly Loaded

Double-check that the mongodb extension is active:

  • Run this command inside your PHP-FPM container:
    php -m | grep mongodb
    
    You should see mongodb in the output.
  • Or look for the "mongodb" section in your phpinfo page—it should detail the extension version and configuration.

3. (Not Recommended) Install the Deprecated mongo Extension (Only for Legacy Apps)

If you're stuck with old code that relies on MongoClient, note that the mongo extension is no longer maintained and doesn't support PHP 7.0+. If your PHP version is <= 5.6, you can add this to your Dockerfile to install it:

RUN pecl install mongo-1.6.16 && docker-php-ext-enable mongo

Again, this is not ideal—upgrading your code to use the modern extension is the safer, long-term fix.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:39:49