Laravel连接数据库报错:could not find driver 如何解决?
问题描述
已启用pdo_mysql扩展,且.env文件配置与phpMyAdmin中创建的fitnessapp数据库信息匹配,但运行php artisan migrate时抛出Illuminate\Database\QueryException错误,核心提示为could not find driver。
错误详情
Illuminate\Database\QueryException could not find driver (Connection: mysql, SQL: select exists (select 1 from information_schema.tables where table_schema = 'fitnessapp' and table_name = 'migrations' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED')) as `exists`) at vendor\laravel\framework\src\Illuminate\Database\Connection.php:825 821▕ $this->getName(), $query, $this->prepareBindings($bindings), $e 822▕ ); 823▕ } 824▕ ➜ 825▕ throw new QueryException( 826▕ $this->getName(), $query, $this->prepareBindings($bindings), $e 827▕ ); 828▕ } 829▕ } 1 vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:66 PDOException::("could not find driver") 2 vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:66 PDO::__construct()
.env配置信息
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=fitnessapp DB_USERNAME=root DB_PASSWORD=
解决方法
- 确认PHP CLI环境的pdo_mysql扩展状态
执行命令php -m,查看输出列表中是否包含pdo_mysql。如果没有,说明网页环境启用的扩展未同步到CLI环境(两者可能使用不同的php.ini配置文件)。 - 修改PHP CLI对应的php.ini文件
执行php --ini找到CLI模式下的php.ini路径,打开该文件,找到;extension=pdo_mysql行,去掉开头的分号;以启用扩展,保存后重启服务。 - 验证扩展生效
再次执行php -m,确认pdo_mysql出现在列表中,随后重新运行php artisan migrate。 - 补充系统依赖(Linux环境)
Debian/Ubuntu系统执行sudo apt-get install php-mysql(根据PHP版本调整,如php8.2-mysql);CentOS/RHEL系统执行sudo yum install php-mysqlnd。 - 清除Laravel配置缓存
依次执行php artisan config:clear和php artisan cache:clear,确保框架加载最新配置。
内容的提问来源于stack exchange,提问作者Pen4o
相关产品推荐
相关产品推荐

