8

developer!

I'm using Laravel framework and I need to connect to MS SQL 2014 Express database. I've made all the proper configurations in .env (and I even tried tried to put connection parameters straight away to the database.php config) but on the first request i'm always getting timeouted with error:

PDOException in Connector.php line 55:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

After the timeout I always get this php fatal:

FatalErrorException in Connector.php line 55
Invalid handle returned.

As a dev tools I'm using XAMPP, and when I'm restarting apache server, sometimes I can establish connection successfully, but only in 1 out of 20-30 tries.

Laravel is using PDO driver, so i tried to use sqlsrv_connect() - and it worked! No errors, everything is fine.

Can somebody help me configure PDO to work? Laravel only uses PDO driver, so I can't just switch to sqlsrv_connect.

Andy Marrel
  • 103
  • 1
  • 6

5 Answers5

2

I had this problem running on my local machine. I restarted the laravel server and ensured that the ms sql localdb was running and the problem resolved it self.

Brian
  • 431
  • 8
  • 18
0

I had the same error.

After removing the host // DB_HOST entries from your .env-File and from your config/database.php it worked for me.

cracker182
  • 37
  • 1
  • 6
0

What I did for connection are

  1. Installed the correct version of sqlsrv dlls.
  2. Updated doctrine dbal package in Laravel.

My database configuration is below

'sqlsrv3' => [
            'driver' => 'sqlsrv',
            'host' => '<host-name>',
            'database' => '<database-name>',
            'username' => '<your-username>',
            'password' => '<your-password>',
    ]  
Anurag Prashant
  • 995
  • 10
  • 31
0

After one day of search in this problem, I found that the problem in DB_PORT, when pass port number 1433 the problem happen, so leave it empty, if you change it in .env, change it like this:

DB_CONNECTION=sqlsrv
DB_HOST=YourHostName
DB_PORT=
DB_DATABASE=YourDBName
DB_USERNAME=YourDBUser
DB_PASSWORD=YourDBPassword
DB_TRUST_SERVER_CERTIFICATE=true

Or if you change it on config/database.php, change it like this:

'sqlsrv' => [
    // ...
    'port' => env('DB_PORT', null), // <<<< see here
],

Try it :)

AnasSafi
  • 5,353
  • 1
  • 35
  • 38
-1

Had the same error, turns out it was my password that was wrong. Do confirm you have the right values.

DB_HOST={youservername}.database.windows.net
DB_DATABASE={dbname}
DB_USERNAME={username}  
DB_PASSWORD={yourpassword}
DB_PORT=1433

If it doesn't fix do reset the password and try again

kabangi julius
  • 2,709
  • 2
  • 16
  • 25