简体   繁体   English

无法在工作中使用 c# 连接 mysql 服务器

[英]Can't connect with mysql server using c# at work

Why i failed to connect to a free cloud mysql DB using C# connector from Nuget in my work office PC while i can connect just fine to the same server using mysql CLI tool?为什么我无法在我的工作办公室 PC 中使用来自 Nuget 的 C# 连接器连接到免费的云 mysql 数据库,而我可以使用 mysql CLI 工具很好地连接到同一台服务器?

The message i get when i fail to connect is "Connect Timeout expired.".连接失败时收到的消息是“连接超时已过期。”。

I also tried on my PC at home and i can connect just fine using exactly the same C# code.我还在家里的 PC 上试过,我可以使用完全相同的 C# 代码很好地连接。 Mysql CLI tool also work from home. Mysql CLI 工具也可以在家工作。

Could it be a firewall or other security measure in my work company network?这可能是我工作公司网络中的防火墙或其他安全措施吗? Why it does not block the mysql CLI tool?为什么它不阻止 mysql CLI 工具?

I also tried with a localhost mysql (using mysqld on port 51255) at work.我还尝试在工作中使用 localhost mysql(在端口 51255 上使用 mysqld)。 I managed to connect using mysql CLI tool but got the same error as before on c# code.我设法使用 mysql CLI 工具进行连接,但在 c# 代码上遇到了与以前相同的错误。

EDIT1 - Code included EDIT1 - 包含代码

EDIT2 - localhost attempt EDIT2 - 本地主机尝试

private async Task<string> ConnectDB()
{
    MySqlConnectionStringBuilder dbinfo = new MySqlConnectionStringBuilder
    {
        Server = "******",
        Port = ****,
        Database = "*****",
        UserID = "*****",
        Password = "*****",
    };
    using(MySqlConnection connection = new MySqlConnection(dbinfo.ConnectionString))
    {           
        try
        {
            await connection.OpenAsync();           
        }
        catch (MySqlException error)
        {                                               
            message = error.Message;                
        }
                
        if (connection.State == ConnectionState.Open)
        {                   
            message = "Success!";
        } 

    }
    return message;
}

We see the same C# code works at home, but not at the office.我们看到相同的 C# 代码在家里工作,但在办公室却不行。 Therefore it's not the code;因此它不是代码; it must be something with the environment at the office.一定是办公室环境的问题。 But you can connect at the office via the command line tools, so it's also not the corporate firewall, which would have a very hard time telling the the difference between the two.但是你可以通过命令行工具在办公室连接,所以它也不是公司防火墙,很难区分两者之间的区别。

What's left is something on the local PC at work.剩下的是本地 PC 上的工作。 The local OS firewall and antivirus software both come to mind, and of the two the antivirus software sounds more suspect to me.本地操作系统防火墙和防病毒软件都浮现在我的脑海中,而这两者中的防病毒软件对我来说听起来更可疑。 One other option is DNS, where the DNS service at the office is resolving the host name for the database differently.另一种选择是 DNS,办公室的 DNS 服务以不同的方式解析数据库的主机名。 But without more info this is just a guess.但如果没有更多信息,这只是一个猜测。 I suggest adding a lot more logging;我建议添加更多的日志记录; right now we don't even know the full error message.现在我们甚至不知道完整的错误信息。


As an additional note, it's not common and considered very poor practice to directly expose a database to the internet.作为附加说明,将数据库直接公开到 Internet 并不常见,并且被认为是非常糟糕的做法。 Additionally, the application should be well-connected to the database.此外,应用程序应与数据库良好连接。 The definition for "well-connected" has changed over the years, but "public internet" is not going to count.多年来,“连接良好”的定义发生了变化,但“公共互联网”并不重要。 Typically, if you most host the database in another location you will have a web API endpoint in front of the database, where the server for this API can be in the same location as the DB and therefore well-connected.通常,如果您将数据库大部分托管在另一个位置,您将在数据库前面有一个 Web API 端点,该 API 的服务器可以与数据库位于同一位置,因此连接良好。

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

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