lundi 1 juin 2020

Laravel MSSQL Server Connection not working

Laravel version:7.13, SqlServer:2019

 SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: 
 An existing connection was forcibly closed by the remote host (SQL: select * from [users]).

Above is error message what I got.

I tried with core php and it works well.

Below is php code.

<?php

$serverName = "MYPCNAME";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"sa", "PWD"=>'MyPassword');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
     echo "Connection established.<br />";

     if(($result = sqlsrv_query($conn,"SELECT * FROM users")) !== false){
        while( $obj = sqlsrv_fetch_object( $result )) {
              echo $obj->name.'<br />';
        }
    }

}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

?>

I think SQL Server configuration is correct. I installed php_sqlsrv, php_pdo_sqlsrv extensions and they are displayed as installed in phpinfo()

Here is my Laravel configuration.

DB_CONNECTION=sqlsrv
DB_HOST=MYPCNAME
DB_PORT=1433
DB_DATABASE=DBNAME
DB_USERNAME=sa
DB_PASSWORD=MyPassword

Following is database config file.

 'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

I tried to change DB_HOST with 127.0.0.1 too but there was same issue. I was going to get users from users table.


My thought: I changed server name with 127.0.0.1 in core php and it displayed exact same error with laravel.

Can anyone help me? Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire