Laravel 5.7 无法连接到 Windows 上的 SQLSRV 数据库(找不到驱动程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53799221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:13:00  来源:igfitidea点击:

Laravel 5.7 can not connect to SQLSRV Database on Windows (could not find driver)

sql-serverdatabaselaravel

提问by Lim Socheat

My env file

我的环境文件

DB_CONNECTION=sqlsrv
DB_HOST=DESKTOP-ATB1EFG
DB_DATABASE=frontdesk
DB_USERNAME=sa
DB_PASSWORD=something_secret123
DB_PORT=1433

I have tested with raw php as below, it is successfully connected and return "connected" string.

我已经用下面的原始 php 进行了测试,它已成功连接并返回“已连接”字符串。

serverName = "DESKTOP-ATB1EFG";
    $connectionOptions = array(
        "Database" => "frontdesk",
        "Uid" => "sa",
        "PWD" => "something_secret123"
    );
    //Establishes the connection
    $conn = sqlsrv_connect($serverName, $connectionOptions);
    if($conn){
        echo "Connected!";

    }else{
        echo "Failed";
    }

BUT, when I tried to migrate using php artisan migrate, it return QueryExeption

但是,当我尝试使用php artisan migrate 进行迁移时,它返回 QueryExeption

could not find driver (SQL: select * from sysobjects where type = 'U' and name = migrations)

找不到驱动程序(SQL:select * from sysobjects where type = 'U' and name = migrations)

Exception trace:

异常跟踪:

1 PDOException::("could not find driver")

C:\wamp64\www\frontdesk.sys\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

2
PDO::__construct("dblib:host=DESKTOP-ATB1EFG:1433;dbname=frontdesk;charset=utf8", "sa", "something_secret123", []) C:\wamp64\www\frontdesk.sys\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

1 PDOException::("找不到驱动程序")

C:\wamp64\www\frontdesk.sys\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

2
PDO::__construct("dblib:host=DESKTOP-ATB1EFG:1433;dbname=frontdesk;charset=utf8", "sa", "something_secret123", []) C:\wamp64\www\frontdesk.sys\vendor\ laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

I have checked with php 7.2.10, it already enables extensions

我已经检查过 php 7.2.10,它已经启用了扩展

extension=php_sqlsrv_72_nts_x64.dll
extension=php_pdo_sqlsrv_72_nts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll

php.ini extensions

php.ini 扩展

回答by Lim Socheat

I fixed the problem by adding extensions:

我通过添加扩展解决了这个问题:

extension=php_pdo_sqlsrv_71_ts_x64.dll
extension=php_sqlsrv_71_ts_x64.dll

to both php.iniin php7.1.22folder and php.iniin apachefolder.

双方php.iniphp7.1.22文件夹,并php.iniApache的文件夹中。

Thanks.

谢谢。

回答by Harvey Gmez Muoz Vito

Sometimes it doesn't work to run migrations with Git Bash in Xampp. In my case, use the Laragon terminal or you can try the Windows terminal.

有时在 Xampp 中使用 Git Bash 运行迁移不起作用。就我而言,请使用 Laragon 终端,或者您可以尝试使用 Windows 终端。

回答by Manojkiran.A

Currently laravel supports

目前 Laravel 支持

MySQL
PostgreSQL
SQLite
SQL Server

If you are using any of these just follow the tip

如果您正在使用其中任何一个,请按照提示操作

Open your .env file and you will find the bunch of variables

打开你的 .env 文件,你会发现一堆变量

and in that you will find these

你会发现这些

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

EG: just change DB_CONNECTIONto you current database such as SQLite

EG:只需更改DB_CONNECTION为您当前的数据库,例如SQLite

and fill you credentails and username ,databasename

并填写您的凭据和用户名,数据库名称

See the supported Database here https://laravel.com/docs/5.7/database#introduction

在此处查看支持的数据库 https://laravel.com/docs/5.7/database#introduction