如何从Linux服务器连接Azure中的sql Server与Php?
I have been using azure mobile services for my android application. Then I want to access my database datas with my Localhost and Remote server using Php.
I tried the codes that is given by Azure docs below.
$conn = new PDO ( "sqlsrv:server = tcp:MYSERVERNAME.database.windows.net,1433; Database = MY_DATABASE_NAME", "MY_USER_NAME", "MY_PASSWORD" );
try {
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
print( "Error connecting to SQL Server." );
die( print_r( $e ) );
};
When The code is runned on Localhost or Remote server, that is Linux server, I get error below
EDIT
I thought it is about Azure firewall rules and I added my IP number in Azure Firewall rules for Server
When I tried to connect to Server via SQLPro I could connect successfuly and I started to query. But I can not to connect via Localhost with my Php codes.
The function you used PDO sqlsrv:server
requires SQLSRV extension
like php_pdo_sqlsrv_53_nts.dll
, but which is only compatible with PHP running on Windows.
To connect to SQL server in PHP in Unix, you can use ODBC
extension and Microsoft's SQL Server ODBC Driver for Linux.
You can refer to http://php.net/manual/en/ref.pdo-sqlsrv.php for details.
Another recommendation for connecting to Azure SQL DB from your Linux server is using sqlshim. The sqlshim project aims to replicate Microsoft SQL Server Driver for PHP (sqlsrv) on Linux/OS X.
Here is a link to the project: https://github.com/radsectors/sqlshim
Here is how you would install it:
Download the latest release from: https://github.com/radsectors/sqlshim/releases
Extract src/sqlshim.php and src/globals.php.
Include it.
require 'src/sqlshim.php';
Cheers,
Meet
Did you add the firewall exceptions to the database or the database instance?
Instance level(Run at master):
EXECUTE sp_set_firewall_rule N'my_rule','123.0.0.1','123.0.0.1';
--EXECUTE sp_set_firewall_rule N'rule_name','IP_Range_low','IP_Range_high';
Database Level (Run at target DB):
EXECUTE sp_set_database_firewall_rule N'my_db_rule';
,'123.0.0.1' --IP_Range_low
,'123.0.0.1' --IP_Range_high