简体   繁体   English

从Azure C#应用程序到远程MySQL服务器的随机连接问题

[英]Random connection problems from Azure C# app to remote MySQL server

I have an ASP.NET MVC web app that connects to a remote MySQL server (Ubuntu). 我有一个连接到远程MySQL服务器(Ubuntu)的ASP.NET MVC Web应用程序。 I've never had any problems on my own Windows Server or localhost, but when I recently moved the app to Azure app services I get random connection errors (sometimes it works, sometimes not): 我从来没有在自己的Windows Server或本地主机上遇到任何问题,但是当我最近将应用程序移至Azure应用程序服务时,我得到了随机连接错误(有时它可以工作,有时不能):

Unable to connect to any of the specified MySQL hosts.
at MySql.Data.MySqlClient.NativeDriver.Open() 
at MySql.Data.MySqlClient.Driver.Open() 
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuildersettings)
at MySql.Data.MySqlClient.MySqlConnection.Open()

There's no inner exception. 没有内在的例外。 My code looks a bit like: 我的代码看起来像:

using(var con = new MySqlConnection(connectionString))
{
  con.Open();
  using(var cmd = CreateCommand(sql, con))
  {
    cmd.Execute();        
  }
  con.Close();
}

Connection string looks like: 连接字符串如下所示:

server=xxxx;uid=xxx;pwd=xxx;database=xxx;Connection Timeout=10;Pooling=false;Protocol=socket;Port=3306; 服务器= xxxx; uid = xxx; pwd = xxx;数据库= xxx;连接超时= 10;缓冲=假;协议=插座;端口= 3306;

This topic describes the same problem: 本主题描述了相同的问题:

It has nothing to do with the firewall on the Ubuntu server, when a connection fails I don't even see incoming requests in the firewall. 它与Ubuntu服务器上的防火墙无关,当连接失败时,我什至看不到防火墙中的传入请求。 I also tried to use other settings in the connection string. 我还尝试在连接字符串中使用其他设置。 Any ideas? 有任何想法吗?

protocol=socket tells your dot net instance to try to connect to a local MySQL server with UNIX domain sockets. protocol=socket告诉您的点网实例尝试使用UNIX域套接字连接到本地MySQL服务器。 But you're obviously trying to connect to a remote server with TCP/IP. 但是您显然正在尝试使用TCP / IP连接到远程服务器。 Omitting protocol=socket from your connection string may help. 从您的连接字符串中省略protocol=socket可能会有所帮助。

Does your Ubuntu server have a host name? 您的Ubuntu服务器有主机名吗? If so, use it in place of an IP address. 如果是这样,请使用它代替IP地址。 Is the IP route from Azure to your Ubuntu server stable, or does it go up and down? 从Azure到Ubuntu服务器的IP路由稳定吗? What happens if you ping your Ubuntu server from your Azure host? 如果从Azure主机ping Ubuntu服务器会怎样?

A note: consider using tls (ssl) for this connection. 注意:请考虑使用tls(ssl)进行此连接。 The MySQL protocol without tls is not very secure. 没有tls的MySQL协议不是很安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM