Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure website cannot access Azure DB

This error drives me crazy and I can't even reproduce it!

I've published my website to Windows Azure and SQL Azure and it worked great. All the sudden and it doesn't work, I get this nasty error:

Here is the error I get:

http://jsfiddle.net/shimmy/pcS7g/embedded/result

Here's the 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)

I want to emphasize that I can access the SQL via SSMS or VS and see my tables etc.

I have no clue what causes this annoying error that happens again and again after every deployment.

Stack trace:

[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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295167
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5309659
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +5311874
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83
   System.Data.SqlClient.SqlConnection.Open() +96
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +76

[HttpException (0x80004005): Unable to connect to SQL Server database.]
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +131
   System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89
   System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +27
   System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386

Update

Here is a copy of the connection strings as they appear on the server's web.config:

<connectionStrings>
  <add name="Context" connectionString="Data Source=tcp:abcdefg.database.windows.net,1433;Initial Catalog=Database;User Id=shimmy@tcp:abcdefg;Password=my123password;" providerName="System.Data.SqlClient" />
  <add name="Storage" connectionString="DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey==" />
</connectionStrings>

I even tried to remove the connection strings entirely from the server's web.config. I have no clue where this App_Data thing is coming in from!

My DbContext looks like this:

public Context()
  : base("name=Context") //I also tried "Context" alone
{
}
like image 938
Shimmy Weitzhandler Avatar asked Dec 05 '12 13:12

Shimmy Weitzhandler


People also ask

Can't connect to Azure Database?

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.

How can we connect Azure Web app to an Azure SQL Database?

Choosing the "Allow access to Azure services" option will allow the app service to connect to the MySQL server. On the MySQL server blade, under the Settings heading, click Connection Security to open the Connection Security blade for Azure Database for MySQL. Select ON in Allow access to Azure services, then Save.

Can not connect to the database in its current state Azure?

This usually indicates you may have hit a pricing/budget limit on your subscription/resource. Please reach out to your billing administrator to check if any budget restrictions were applied to this resource. If it is still not resolved, please reach out to Azure Support and submit this as a "Billing Issue".


2 Answers

I know this is a really old post, but I just battled with the same issue...

I had the same issue and although I don't understand it, I have found that if I am currently logged in (on my site, not Azure console), I need to log out and then back in again and this database error goes away. It makes no sense to me, but it is what works for me after I deploy. I have had many times where I have deployed and everything works great (no need to logout/login). Sometimes it jumps into this mode. Not sure what is going on there.

It has nothing to do with the connection strings in your web.config - they are all being transformed properly.

like image 105
CPank Avatar answered Oct 21 '22 13:10

CPank


It appears that your application is expecting to be able to set up a database using an attached file in your project on a local instance of SQL Server Express. The key line in your error message is:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.

You probably need to update the connection string in your configuration file to match the connection string in your Azure website. This information can be obtained in the Azure dashboard once you set up the website with a SQL Server, and be set through the same dashboard.

Here's good instructions for doing that: http://blog.davidebbo.com/2012/09/managing-database-connections-in-azure.html

like image 42
Brian Knight Avatar answered Oct 21 '22 12:10

Brian Knight