从Azure C#应用程序到远程MySQL服务器的随机连接问题 [英] Random connection problems from Azure C# app to remote MySQL server

查看:60
本文介绍了从Azure C#应用程序到远程MySQL服务器的随机连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC Web应用程序,该应用程序连接到远程MySQL服务器(Ubuntu).我从来没有在自己的Windows Server或本地主机上遇到任何问题,但是当我最近将应用程序移至Azure应用程序服务时,我得到了随机的连接错误(有时它可以工作,有时不能):

I have an ASP.NET MVC web app that connects to a remote MySQL server (Ubuntu). 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):

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();
}

连接字符串如下:

服务器= x.x.x.x; uid = xxx; pwd = xxx;数据库= xxx;连接超时= 10;缓冲=假;协议=套接字;端口= 3306;

server=x.x.x.x;uid=xxx;pwd=xxx;database=xxx;Connection Timeout=10;Pooling=false;Protocol=socket;Port=3306;

本主题描述了相同的问题:

This topic describes the same problem:

它与Ubuntu服务器上的防火墙无关,当连接失败时,我什至看不到防火墙中的传入请求.我还尝试在连接字符串中使用其他设置.有什么想法吗?

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. I also tried to use other settings in the connection string. Any ideas?

推荐答案

protocol = socket 告诉您的点网实例尝试使用UNIX域套接字连接到本地MySQL服务器.但是您显然正在尝试使用TCP/IP连接到远程服务器.从您的连接字符串中省略 protocol = socket 可能会有所帮助.

protocol=socket tells your dot net instance to try to connect to a local MySQL server with UNIX domain sockets. But you're obviously trying to connect to a remote server with TCP/IP. Omitting protocol=socket from your connection string may help.

您的Ubuntu服务器有主机名吗?如果是这样,请使用它代替IP地址.从Azure到Ubuntu服务器的IP路由稳定吗?如果从Azure主机ping Ubuntu服务器会发生什么情况?

Does your Ubuntu server have a host name? If so, use it in place of an IP address. Is the IP route from Azure to your Ubuntu server stable, or does it go up and down? What happens if you ping your Ubuntu server from your Azure host?

注意:考虑使用tls(ssl)进行此连接.没有tls的MySQL协议不是很安全.

A note: consider using tls (ssl) for this connection. The MySQL protocol without tls is not very secure.

这篇关于从Azure C#应用程序到远程MySQL服务器的随机连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆