Laravel 9连接SQL Server提示could not find driver问题求助
Laravel 9连接MS SQL Server报"could not find driver"错误解决
问题场景
在Laravel 9项目中创建了test.php命令,尝试连接MS SQL Server并读取MECommssion表数据,handle函数代码如下:
/** * Execute the console command. * * @return int */ public function handle() { $crmData = DB::connection('core')->table('dbo.MECommssion')->get(); dd($crmData); }
数据库配置
config/database.php中的连接配置:
'core' => [ 'driver' => env('DB_CONNECTION_SYNTELLICORE'), 'host' => env('DB_HOST_SYNTELLICORE'), 'port' => env('DB_PORT_SYNTELLICORE'), 'database' => env('DB_DATABASE_SYNTELLICORE'), 'username' => env('DB_USERNAME_SYNTELLICORE'), 'password' => env('DB_PASSWORD_SYNTELLICORE'), 'charset' => 'utf8', 'prefix' => '', 'prefix_indexes' => true, ],
.env中的环境变量配置:
DB_CONNECTION_SYNTELLICORE=sqlsrv DB_HOST_SYNTELLICORE=192.168.4.59 DB_PORT_SYNTELLICORE=1433 DB_DATABASE_SYNTELLICORE=test DB_USERNAME_SYNTELLICORE=MSSB DB_PASSWORD_SYNTELLICORE=mypassword
错误信息
执行命令时终端抛出如下错误:
Illuminate\Database\QueryException could not find driver (SQL: select * from [dbo].[MECommssion]) at E:\laragon\www\InfoPortal\Backoffice\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 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDOException::("could not find driver") 2 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDO::__construct("dblib:host=192.168.4.59:1433;dbname=test;charset=utf8", "MSSB", "mypassword", [])
已尝试操作
- 安装ODBC驱动11、17、18版本
- 为PHP 8.1.10添加
php_pdo_sqlsrv_81_ts_x64和php_sqlsrv_81_ts_x64.dll扩展,在php.ini中启用 - 重启服务器与计算机
- 修改
.env后清除缓存 - 编写独立PHP文件用PDO连接,可正常获取数据
- 另一台机器搭建相同环境的Laravel 9项目,连接正常
- 执行
composer dump autoload
独立PHP文件能正常连接,但Laravel项目始终无法识别sqlsrv驱动,求解决办法。
解决方案
1. 确认CLI使用的php.ini文件
Laravel命令行用的是PHP CLI的配置,而非Web服务器的php.ini。执行php --ini查看Loaded Configuration File路径,检查该文件是否启用了对应扩展:
extension=php_sqlsrv_81_ts_x64.dll extension=php_pdo_sqlsrv_81_ts_x64.dll
确保文件名与实际扩展文件一致,且扩展文件在PHP的ext目录下,修改后保存并重启终端。
2. 检查配置缓存与驱动读取
错误信息显示PDO使用dblib驱动,说明Laravel未正确读取sqlsrv配置:
- 执行
php artisan config:clear清除配置缓存 - 运行
php artisan tinker,输入env('DB_CONNECTION_SYNTELLICORE'),确认输出为sqlsrv,若不是则检查.env拼写或缓存问题
3. 验证PHP CLI是否加载扩展
执行php -m,查看输出是否包含pdo_sqlsrv和sqlsrv。若没有,说明CLI环境未加载扩展,需检查:
- 扩展文件路径是否正确
- 扩展版本与PHP版本是否匹配(必须是TS、x64、PHP8.1对应版本)
4. 完善数据库配置
在config/database.php的core配置中:
- 给driver添加默认值,避免环境变量读取失败:
'driver' => env('DB_CONNECTION_SYNTELLICORE', 'sqlsrv'), - 添加ODBC 17+所需的参数:
'trust_server_certificate' => true,
5. 检查系统环境变量
确保ODBC驱动路径加入系统PATH环境变量(Windows下可执行echo %PATH%查看),比如ODBC 17的路径为C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn,确保CLI能访问到该路径。
内容的提问来源于stack exchange,提问作者SWAT 10101
相关产品推荐
相关产品推荐

