【发布时间】:2020-07-30 07:05:23
【问题描述】:
我在 Azure 上设置了一个 Linux 虚拟机,并部署了一个 Apache Web 服务器和 MySQL。我有一个 C# 应用程序,我想从我的虚拟机连接 MySQL 数据库。 我安装了 MySQL 连接器并遵循如下教程:
string connString = "Database=Virtual Machine IP;port=3306;username=root;password=;database=testing";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Company";
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["CompanyID"].ToString());
}
Console.ReadLine();
但它不起作用,因为我收到此错误:“无法连接到任何指定的 MySQL 主机。”,所以连接不成功,有人知道怎么做吗?
附:我在本地主机上用我的数据库尝试了代码,它工作了。
【问题讨论】:
-
您是否在托管 MySQL 实例的 Linux VM 上打开了正确的端口?
-
您是否在 azure 中为尝试连接数据服务器的开发机器的 ip 地址授予防火墙例外并尝试过?
-
我之前没有使用过 Azure,大学向我们提供了如何设置 LAMP 服务器的说明,所以我不知道如何授予防火墙例外或应该打开哪些端口以及如何操作.我只需要我的 C# 应用程序的数据库。您能告诉我步骤或提供有用的链接吗?
-
问你的导师,你的连接使用3306端口来访问数据库,但是当这个端口没有打开时,我就无法访问它。
-
@MonicaHotea - 请查看 Azure 文档。有很多关于为虚拟机设置入站端口规则的信息。
标签: c# mysql azure azure-virtual-machine