Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect a azure sql server from laravel on linux

I have a laravel application that connect a sql server db on Azure.

On my local Wamp server the application works. I have installed on my linux server using a docker image, and don't connect the Azure DB. Every time returns this error message:

SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (SQL: select * from [mytable])

MSDOBC Driver, sqlsrv and pdo_sqlsrv are correctly installed.

I suppose is something related with laravel because if I query the db with a php script works without problem.

My .ENV file has db settings:

DB_CONNECTION=sqlsrv
DB_HOST=db.database.windows.net
DB_PORT=1433
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD=pwd

Laravel log and docker log don't tell nothing interesting, I don't have any idea how to solve.

like image 411
cesare Avatar asked Jul 28 '20 10:07

cesare


People also ask

Can not connect to Azure SQL Server?

Steps to resolve persistent connectivity issuesSet up firewall rules to allow the client IP address. On all firewalls between the client and the Internet, make sure that port 1433 is open for outbound connections. Review Configure the Windows Firewall to Allow SQL Server Access for additional pointers.

Can laravel connect to SQL Server?

Laravel makes connecting with SQL Server database and running queries extremely simple out of the box. The SQL Server, MSSQL database configuration for your application is located at Laravel Project Root Folder, config/database. php .


1 Answers

Azure SQL Database supports only the tabular data stream (TDS) protocol (accessible over TCP and the default port of 1433) and uses its own IP-based firewall. So you may try the following:

  • Use connection string with protocol, server name and port. In your case you need to use tcp:db.database.windows.net,1433 as a value of DB_HOST.

  • Add the IP address of your LINUX server as a firewall rule. This is explained in the documentation:

When a computer tries to connect to your server from the internet, the firewall first checks the originating IP address of the request against the database-level IP firewall rules for the database that the connection requests.

If the address is within a range that's specified in the database-level IP firewall rules, the connection is granted to the database that contains the rule.

  • If the address isn't within a range in the database-level IP firewall rules, the firewall checks the server-level IP firewall rules.

  • If the address is within a range that's in the server-level IP firewall rules, the connection is granted. Server-level IP firewall rules apply to all databases managed by the server.

  • If the address isn't within a range that's in any of the database-level or server-level IP firewall rules, the connection request fails

like image 67
Zhorov Avatar answered Oct 11 '22 04:10

Zhorov