Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix PDO sqlsrv connection timeout in Laravel?

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.

like image 758
Andy Marrel Avatar asked Jun 19 '16 14:06

Andy Marrel


1 Answers

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.

like image 156
Brian Avatar answered Nov 03 '22 17:11

Brian