Windows下Laravel 9执行php artisan migrate提示could not find driver
Laravel迁移报错:could not find driver 排查方案
问题场景
将MySQL数据库连接到Laravel应用,已通过PHPMyAdmin创建数据库,配置了.env和database.php文件,但执行php artisan migrate时抛出could not find driver错误。已将PHP和MySQL加入环境变量,取消了php.ini中extension=pdo_mysql的注释,问题仍未解决。
错误信息
could not find driver (SQL: select * from information_schema.tables where table_schema = pl_project and table_name = migrations and table_type = 'BASE TABLE') at C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760 756▕ // If an exception occurs when attempting to run a query, we'll format the error 757▕ // message to include the bindings with SQL, which will make this exception a 758▕ // lot more helpful to the developer instead of just the database's errors. 759▕ catch (Exception $e) { ➜ 760▕ throw new QueryException( 761▕ $query, $this->prepareBindings($bindings), $e 762▕ ); 763▕ } 764▕ } 1 C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDOException::("could not find driver") 2 C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDO::__construct()
相关配置
.env 配置片段
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=pl_project DB_USERNAME=root DB_PASSWORD=
database.php 配置片段
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'pl_project'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ]
php -m 输出
[PHP Modules] bcmath calendar Core ctype curl date dom fileinfo filter hash iconv json libxml mbstring mysqlnd openssl pcre PDO Phar readline Reflection session SimpleXML SPL standard tokenizer xml xmlreader xmlwriter zip zlib [Zend Modules]
排查修复步骤
- 确认修改的是当前PHP使用的php.ini:执行
php --ini查看PHP加载的配置文件路径,确保你修改的是该路径下的php.ini,避免改了其他版本PHP的配置文件(比如集成环境自带PHP和系统全局PHP可能不是同一个)。 - 验证pdo_mysql扩展是否加载:从
php -m输出看,仅存在mysqlnd但无pdo_mysql,说明扩展未加载成功。可以创建phpinfo.php文件(内容为<?php phpinfo(); ?>),访问后搜索pdo_mysql确认是否存在。 - 检查PHP扩展目录配置:打开php.ini,确认
extension_dir指向正确的扩展文件夹(Windows下常见为extension_dir = "ext"或绝对路径如C:/php/ext),路径错误会导致扩展无法加载。 - 重启相关服务与终端:修改php.ini后必须重启终端,让环境变量生效;若使用Apache/Nginx等Web服务,也需重启服务。
- 检查版本兼容性:确保Laravel版本与PHP版本匹配(如Laravel 10要求PHP 8.1及以上),版本不兼容可能导致扩展加载异常。
- 重新安装/修复pdo_mysql扩展:手动安装的PHP需下载对应版本的
pdo_mysql扩展文件(注意线程安全/非线程安全要与PHP一致);集成环境可通过管理面板直接启用该扩展。
内容的提问来源于stack exchange,提问作者coraline98
相关产品推荐
相关产品推荐

