SQL Connection from Azure Linux App Service web app - sql-server

I have an asp net core web app and SQL Server database hosted in Plesk via a hosting company.
I have deployed a copy of the web app to an Azure Linux App Service. It uses the Plesk hosted SQL Server database. When i use the Azure App Service I get a connection error when accessing the Plesk hosted database.
SqlException: 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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
If I use the Plesk hosted web app it connects to sql server correctly as expected. If I then immediately use the Azure Linux web app it manages to get a SQL connection! In all cases the connection string is identical. Its like it needs waking up!
How can this be the case. I am at a loss as to why this is happens. Any ideas folks?

Properly, you need to add the outbound IP addresses in Azure App Service into the firewall of SQL Server which your SQL Server database existed.
edit
You could check the following aspects on your side refer to this.
Make sure SQL Server Service is running
If a named instance, make sure SQL Server browser service is running
Make sure SQL Server is configured to allow remote connections
Examine the SQL Server error log for messages confirming that SQL is listening on the expected network interfaces and ports
Test server connectivity with PING from the client machine
Test port connectivity using TELNET or PowerShell to the server and port (from step 4) from the client machine. For example a. TELNET
1433 b. PowerShell: 1433 | % { echo ((new-object
Net.Sockets.TcpClient).Connect("YourServerName",$)) "server listening
on TCP port $" }
Check firewall settings if step 5 or 6 connectivity test fails
For more information:
Troubleshoot connecting to the SQL Server Database Engine
Networking Related Commands for Azure App Services

Related

IIS can't connect to SQL Server using SQL Server authentication

I have a very strange problem that I have tried to figure out for some time now but unsuccessfully. At work I am using a laptop for development. We have our application run locally on each developer's laptop, but we all use the same dev database. So when I take my work laptop home and connect it to our vpn, my laptop's IIS connects to SQL Server and everything works just fine.
The problem is when I try to use my personal desktop for work I connect it to the same vpn I connect my laptop to run the same code that is on the laptop with the same connection strings, but IIS can't connect to SQL Server and I get this error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
On both computers I am able to connect to SQL Server using SSMS with no problem. On both computers I can run Integration Tests that hit the database just fine (integration tests use the exact same connection string that the application is using), but if I try to run the application it can't connect. I have tried changing all sorts of settings in IIS but to no avail.
Thanks for your help in advance.
Make sure SQL Server Service is running
If a named instance, make sure SQL Server browser service is running
Make sure SQL Server is configured to allow remote connections
Examine the SQL Server error log for messages confirming that SQL is listening on the expected network interfaces and ports
Test server connectivity with PING from the client machine
Test port connectivity using TELNET or PowerShell to the server and port (from step 4) from the client machine. For example
a. TELNET 1433
b. PowerShell: 1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",$)) "server listening on TCP port $" }
Check firewall settings if step 5 or 6 connectivity test fails

Cannot connect to SQL Server From Clients in LAN

Yesterday I had no problems. Now when any client (even MSSMS from a client machine) tries to access sql server located on a server in the domain I get the following error: TITLE: Connect to Server
Cannot connect to SERVER\Instance
A network-related or instance-specific error occurred while establishing a connection to SQL Server. ...
SQL server is 2014 on a Windows 2012 R2 Server which also hosts the Active Directory.
I can connect using MSSMS from the server itself to the database. The log states that the service started and is listening on port 63782. I can also connect to sql server from the host machine using ip:port (192.168.0.2, 63782). But I cannot connect to it this way from the clients. (I made no changes to the firewall and the rule for lan-lan is allow all services and all connections).
Shared Memory, TCP/IP and Named Pipes are enabled on the sql server configuration Manager.
I can ping to the server from the clients and to the clients from the server.
The main difference between yesterday and today is that I had to reinstall the DNS on the server. The DNS appears to be working very well and I can't find any DNS errors on the server. I don't think its the firewall but I added two rules that allow UDP and TCP specifically within the local network (even though there is a policy which allows all traffic within the local network).
I am at a loss and would appreciate some assistance into solving the problem or even where to look.

“No such host” when Azure web app attempts to communicate with Azure database

Now, I have both my App Service and Database deployed in Azure, they are fine. I can browse to the site perfectly and it all seems to function.
Similarly, I've added a firewall rule in Azure to enable me to connect to the database server and query the database from my local SQL Server Management Studio.
Additionally, when I change my local web application (on my Local IIS Express server) to point to the Azure database it also works fine.
However, when I deploy the application I keep getting: "No such host is known":
[SqlException (0x80131904): 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: TCP Provider, error: 0 - No such host is
known.)] I've tried adding the IP address of the App Service to the
database server. I've even tried white listing the entire IP address
range just to completely discount that and it doesn't work either!
Any ideas?
If you're publishing from Visual Studio, make sure that you don't overwrite with a different remote connection string when deploying.
Also make sure you allow for Azure Services access.

Azure WebApp not able to communicate with Azure WebSql Database

The Scenario:
A webApp (Not WebSite nor WebService) on Azure using WebAPI fails to connect to its Azure Database. There is no WebApp IP to open in the database firewall although I have opened (temporarily) 0.0.0.0 - 255.255.255.255 which did not help.
We receive the following generic error message:
"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)"
Running the Web App locally it connects to the Azure database fine.
The WebApp connects to the database through EF6.
So we know the connection string works, we know the database is accessible and the WebAPI works ... it only fails when the WebApp is placed on Azure.
How can we resolve this?
Ok, the solution is a little more complex than it needs to be. You can't get a webApp to communicate with a WebSql database without serious fudging. However, the correct although more costly solution is this:
Create a Ms SQL Database VM.
Create a Ms Windows VM for the API
Create a vNetwork and place both VMs on the network
Configure the API to use the internal IP of the network that belongs to the
Database VM
Voila.

Unable to access database on different subnet from IIS application

I have a web server in a DMZ on a different VLAN and subnet than the trusted network. This web server must communicate with a database server on the trusted network, so the appropriate firewall rules have been applied. I am able to connect to the database from the web server using powershell and by using the ODBC Data Source Administrator snapin. However, running an IIS application throws the following error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Inner Exception:
Source: .Net SqlClient Data Provider
I am using the same connection string in the web.config file of the IIS application as the one I use in powershell, which connects without any issue. Here is the connection string from the web.config:
"Data Source=192.168.1.xxx\SQLExpress;Database=mydb;uid=myuserid;password=mypassword;integrated security=false;"
The only difference in the powershell connection string is that I do not specify the provider. If I leave that out of the web.config there is no difference.
I have even disabled all firewalls on both servers for test purposes, but still get the same error.
As ping and tracert from the web server to the database server complete successfully and I am able to connect via powershell and ODBC Data Source Administrator, I have no idea why the IIS application cannot connect.
Is the TCP/IP Protocol enabled for the database instance?
On the machine with the Database instance installed, go to Start -> MS SQL Server 20XX -> Configuration Tools -> SQL Server Configuration Manager.
There go to 'SQL Server Network Configuration' -> 'Protocols for SQLExpress'. Check that TCP/IP is enabled (By default it is disabled).

Resources