错误:无法连接到任何指定的 MySQL 主机 - Synology NAS - MariaDB 10 - C# [英] ERROR: Unable to connect to any of the specified MySQL hosts - Synology NAS - MariaDB 10 - C#

查看:45
本文介绍了错误:无法连接到任何指定的 MySQL 主机 - Synology NAS - MariaDB 10 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 MariaDB 10 将 Visual Studio 2019 中的 C# 应用程序连接到 Snyology NAS 中的数据库时遇到问题.如果我使用 HeidiSQL 连接数据库效果很好,但如果我尝试使用相同的凭据连接 C# 应用程序,我会看到像这样的错误:

I have a problem to connect my C# application in Visual Studio 2019 to Database inside Snyology NAS using MariaDB 10. If I connected the Database with HeidiSQL works good, but if I try to connect C# App with the same credentials I see an error like this:

无法连接到任何指定的 MySQL 主机"

"Unable to connect to any of the specified MySQL hosts"

我尝试检查并确认:

  • 在 Synology NAS 上启用 TCP 功能
  • 用户名和密码正确,我拥有所有权限
  • 数据库名称正确
  • 端口也是正确的 3307(在 Synology 上设置默认)

这是我用来检查连接的代码:

This is the code that I use to check the connection:

string connectionString = "";
string server = "ip_address_NAS:3307";
string database = "my_Database";
string username = "my_user";
string password = "my_password";

MySql.Data.MySqlClient.MySqlConnection cn;

connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password;          
            
try
{

cn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);                
cn.Open();                
label1.Text = "Database ONLINE!";                
label1.ForeColor = System.Drawing.Color.Green;                
cn.Close();
            
}
            
catch(Exception ex)            
{
                
label1.Text = "Database OFFLINE!
" + ex.Message;                
label1.ForeColor = System.Drawing.Color.Red;
            
}

为什么会出现这个错误?你能帮助我吗?请.谢谢.

Why this error? Can you help me? please. Thank you.

推荐答案

从主机名中删除端口号.如果您连接到默认端口,则无需指定端口.如果您要连接到自定义端口,则必须按以下方式定义它

Remove the port number from the hostname. If you are connecting to the default port, there's no need to specify a port. If you are connecting to a custom port, it has to be defined in the following way

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword

文档

编辑

3307 不是 MySQL 或 MariaDB 的默认端口.因此,如果您的数据库在端口 3307 上运行(无论出于何种原因 Synology 选择它作为初始设置),您需要在连接字符串中指定该端口.但不是作为服务器名称的一部分,而是使用 Port=3307 属性.

3307 is not the default port for MySQL or MariaDB. So if your database is running on port 3307 (because for whatever reason Synology has chosen it to be the initial setting) you need to specify that port in the connection string. But not as part of the servername but with the Port=3307 property.

这篇关于错误:无法连接到任何指定的 MySQL 主机 - Synology NAS - MariaDB 10 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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