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

Laravel中could not find driver错误排查(PDO_MySQL已启用仍报错)

问题描述

访问域名时触发数据库驱动错误:

could not find driver (Connection: mysql, SQL: select * from posts order by created_at desc limit 1)

路由代码

Route::get('/', [PostController::class, 'index'])
    ->name('home');

PostController代码

class PostController extends Controller
{
    public function index()
    {   
        $expTime = now()->addHours(5);

        // cache latest
        if (Cache::has('latest')) {
            $latest = Cache::get('latest');
        } else {
            $latest = Cache::remember('latest', $expTime, function () {
                return Post::latest()->first();
            });
        }
    }
}

错误触发于return Post::latest()->first();行,已启用pdo_mysql扩展但仍报错,PHP版本为8.2.4,运行模式为LASPI。


解决方案

针对该问题,即便已启用pdo_mysql,可按以下步骤排查修复:

  • 确认PHP环境扩展一致性
    LASPI环境中,Web服务使用的PHP配置可能和CLI版本不一致。执行php -m查看CLI已加载扩展,再通过phpinfo()页面查看Web环境的扩展列表,确保两者均包含pdo_mysql。若Web环境缺失,修改对应php.ini文件添加extension=pdo_mysql,重启Web服务。

  • 检查扩展文件权限与存在性
    通过phpinfo()的extension_dir字段找到PHP扩展目录,确认pdo_mysql.so(Linux)或php_pdo_mysql.dll(Windows)存在。同时确保Web服务器进程(如nginx、apache)对该文件有读取权限。

  • 验证Laravel数据库配置
    检查.env文件的数据库连接参数:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_db_name
    DB_USERNAME=your_db_user
    DB_PASSWORD=your_db_pass
    

    确认DB_CONNECTION设为mysql,其余参数正确。执行php artisan config:clear清除配置缓存,避免旧配置生效。

  • 重新安装pdo_mysql扩展
    若上述步骤无效,尝试重装扩展:

    • Debian/Ubuntu:sudo apt-get install php8.2-mysql
    • RHEL/CentOS:sudo yum install php8.2-mysqlnd
    • Windows:确保php.ini中已取消注释extension=pdo_mysql,且扩展文件路径正确。
  • 检查LASPI环境特殊配置
    LASPI集成环境通常有独立的扩展管理面板,进入面板确认pdo_mysql已启用并应用,之后重启整个LASPI服务。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 06:33:14