简体   繁体   中英

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

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.

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

could not find driver (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

I have checked with php 7.2.10, it already enables extensions

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 扩展

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.ini in php7.1.22 folder and php.ini in apache folder.

Thanks.

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.

This link helped me with proper installation:

https://www.danhendricks.com/2017/11/installing-microsoft-sql-server-php-extensions-plesk-onyx/

I didn't even need to make 3. step. You need to install Microsoft ODBC Driver for SQL Server and Microsoft Drivers for PHP for SQL Server.

And then you can check connection through Tinker. Just be sure you use the sam PHP version for both environments.

php artisan tinker
DB::connection('your_sqlsrv_connection_name')->getPdo();

Currently laravel supports

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

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_CONNECTION to you current database such as SQLite

and fill you credentails and username,databasename

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM