Connect to SQL Server database with Azure App Service - sql-server

I'm deploying an website for my company and it was, originally, hosted in an on-premise server. Now I want to upload it to the cloud (Azure App Service) but I'm having some issues concerning the database's connection string.
While the website was being developed, I used to connect to the SQL Server Database with user authentication via PHP:
$connectionInfo = array('Database'=>"database_name", "CharacterSet"=>"UTF-8");
$connCRM = sqlsrv_connect($serverName, $connectionInfo);
Now that it's in the process of deployment, I want to connect to the database with a db user and password:
$connectionInfo = array('Database'=>"database_name", "CharacterSet"=>"UTF-8", "UID"=>"user", "PWD"=>"pass");
$connCRM = sqlsrv_connect($serverName, $connectionInfo);
Considering I'm not connected to the VPN (the website is not located locally anymore), it seems like it can't find my server, even though I'm accessing it through TS (in the destination server).
Already tried to authenticate in SSMS with this user and password and it's working.
Error message: Array ( [0] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 11001 [code] => 11001 [2] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2AF9 [message] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2AF9 ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 11001 [code] => 11001 [2] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

Your App Service is hosted in Azure, so it is indide Azure network, and you are trying to connect to a database server inside your organization network(since you indicate it is on-prem), so unless you configure your org firewall to allow inbound connection on the port in which db server is listening, connection would not be established. If your SQL Server is using default port, you need to allow inbound on port 1433.
Update: As an alternate, if solution must not require changes to firewall, then Hybrid Connections is suitable one.

Related

Microsoft SQL Server on linux container

I installed the following SQL Server on a Linux Container as this site:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
But when I am trying to connect from outside I get the following errors:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: The wait operation timed out.
.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error
has occurred while establishing a connection to SQL Server. Server is not found or not accessible.
Check if instance name is correct and if SQL Server is configured to allow remote connections. For
more information see SQL Server Books Online..
From inside the container I am able to connect either as localhost either from the container;s ip.
Any suggestion?

Pyodbc: Login timeout expired

I am trying to connect to MS SQL Server using pyodbc on local system and on connect to instance i get error:
[2020-06-21 15:39:04.110750]: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. (-1) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (-1)')
My SQL Server is Express 2005 and try with drivers:
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Native Client}
I test in sqlcmd with this command:
C:\Users\Moein>sqlcmd -S '.\Moein' -U 'sa' -P 'xxxx'
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
My Code :
import pyodbc
conn = pyodbc.connect(f'DRIVER={SQL Native Client};SERVER=.\Moein;DATABASE=Moein;UID=sa;PWD=xxxx',autocommit=True)
cursor = conn.cursor()
More:
Test -l 600 switch for login timeout more in sqlcmd > not answer recived
Test computername\instance and 127.0.0.1\instance > Not changed
Try full reinstall sql server on local > noting changed things
i found my problem
i use this connection string with pipe mode:
conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};ServerName=Provider=SQLOLEDB.1;SERVER=.\testsv;DATABASE=test;Persist Security Info=False;UID=sa;PWD=xxxxx;Data Source=np:\\.\pipe\MSSQL$testsv\sql\query',autocommit=True)

[Microsoft][ODBC Driver 17 for SQL Server]Cannot use Authentication option with Integrated Security option

I am trying to connect to a SQL server using PHP using Windows Active Directory Authentication. My code is below:
<?php
$serverName = 'vgissql';
$connectionInfo = array('Database'=>'gis', "Authentication"=>'ActiveDirectoryPassword');
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($con){
echo "Connection established";
}else{
echo 'Connection failed<br>';
die(print_r(sqlsrv_errors(), True));
}
?>
I am getting an error:
Connection failed
Array ( [0] => Array ( [0] => FA001 [SQLSTATE] => FA001 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Cannot use Authentication option with Integrated Security option. [message] => [Microsoft][ODBC Driver 17 for SQL Server]Cannot use Authentication option with Integrated Security option. ) [1] => Array ( [0] => FA001 [SQLSTATE] => FA001 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Cannot use Authentication option with Integrated Security option. [message] => [Microsoft][ODBC Driver 17 for SQL Server]Cannot use Authentication option with Integrated Security option. ) )
I can't find a way to turn off Integrated Security. And I'm not specifying to use it anywhere as far as I can tell.
If you want to connect to SQL Server with Windows authentication, don't set Authentication connection option. Execute your script like this:
<?php
$serverName = 'vgissql';
$connectionInfo = array('Database'=>'gis');
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($con){
echo "Connection established";
}else{
echo 'Connection failed<br>';
die(print_r(sqlsrv_errors(), True));
}
?>
Based on documentation about how to Connect Using Azure Active Directory Authentication, "Authentication"=>'ActiveDirectoryPassword' config option is used to connect to SQL Server using Azure AD.

Could not open a connection to SQL Server after 2017 update

I installed SQL Sever before version 2016 and have updated. It is 2017 version now.
Now when I open SQL Sever I get this error:
Cannot connect to ..
Additional information:
A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow
remote connections. (provider: Named Pipes Provider, error:
40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)
The system cannot find the file specified
I entered
Server Type: Database Engine
Server name: .
Authentication: SQL Server Authentication
Login: sa
Resolution:
While verifying the Services i found that the MSSQLSERVER service was stopped, after starting this service my issue was resolved.

SQL Server backup fails when issued via PDO

I'm talking to an SQL server 2005 using PHP's PDO API:
$mssql = new PDO('dblib:host=foohost;dbname=foodb', 'foouser', 'foopassword');
Subsequently, I want to backup the database to a local file as shown in the code below - which then fails.
$query = "
BACKUP DATABASE foodb
TO DISK = 'D:\\migration\\test_backup.bak';
GO
";
$stmt = $mssql->prepare($query);
$stmt->execute();
The error I'm getting is this (result from $stmt->errorInfo()):
[0] => HY000
[1] => 102
[2] => General SQL Server error: Check messages from the SQL Server [102] (severity 15) [(null)]
[3] => -1
[4] => 15
I've tried to do the same thing using SQL Server Management Studio, and the exact same query works. So - any hints on how I could get this to work (Note: I'm forced to use PHP on a remote linux host, so any local windows tools won't work)

Resources