Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mirkin93's avatar

Illuminate\Database\QueryException SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired

When I try to connect to my SQL Server with my Laravel App I got the error: Illuminate\Database\QueryException SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired.

When I try to the same connection on my server with sqlcmd -S {host} -d {database} -U {user} -P {pass} -I -Q {query} I got a result.

How can I fix the error in the Laravel App?

0 likes
11 replies
mirkin93's avatar

My .env file.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:s9/HEFeFdMltze4NxAKacADUAuwGeOQl+SjYUSF3z/0=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=sqlsrv
DB_HOST=127.20.2.10
DB_PORT=1433
DB_DATABASE=DATA
DB_USERNAME=saWeb
DB_PASSWORD='PASSWORD'

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
munazzil's avatar

Change below credentials in your .env

DB_HOST=127.0.0.1
mirkin93's avatar

I use a external MS SQL server, on ip 172.20.2.10 and the TCP port 1433.

munazzil's avatar

Just change this and check because your still in localhost.

DB_HOST=127.0.0.1

and show your config/database.php

mirkin93's avatar

I don't understand what you're try to say/test. When I change it to 127.0.0.1, it's my localhost. (My own server).

On the localhost there isn't a MS SQL Server installed, the MS SQL Server is installed on 172.20.2.10. Why should I change my DB_HOST to 172.0.0.1?

mirkin93's avatar

@MUNAZZIL - I read the article yesterday, with sqlcmd the connection is successful. I already reinstalled the pdo_sqlsrv & sqlsrv driver.

When I try the follow PHP script:

<?php
$serverName = "172.20.2.10";
$connectionOptions = array(
    "Database" => "DATA",
    "Uid" => "saWeb",
    "PWD" => "PASSWORD"
);

//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
//Select Query
$tsql= "SELECT @@Version as SQL_VERSION;";
//Executes the query
$getResults= sqlsrv_query($conn, $tsql);
//Error handling

if ($getResults == FALSE)
    die(FormatErrors(sqlsrv_errors()));
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
    echo ($row['SQL_VERSION']);
    echo ("<br/>");
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors )
{
    /* Display errors. */
    echo "Error information: <br/>";

    foreach ( $errors as $error )
    {
        echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
        echo "Code: ".$error['code']."<br/>";
        echo "Message: ".$error['message']."<br/>";
    }
}
?>

It gives the following output:

Microsoft SQL Server 2014 (SP1-GDR) (KB4019091) - 12.0.4237.0 (X64) Jul 5 2017 22:03:42 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor) 

That output shows me that the drivers installed correctly and there is a problem with Laraval and MS SQL?

mirkin93's avatar
mirkin93
OP
Best Answer
Level 6

@Noggin79s I made a mistake, the db host was wrong. I had swapped 127 with 172.

Ratto's avatar

Same here with Docker (Laravel Sail)

Please or to participate in this conversation.