【问题标题】:Unable to connect an Azure WebJob to SQL database on Azure VM无法将 Azure WebJob 连接到 Azure VM 上的 SQL 数据库
【发布时间】:2021-04-01 22:00:25
【问题描述】:

我正在尝试连接到托管在 Azure 上的虚拟机中的 SQL 数据库。

我的应用服务和虚拟机在不同的订阅中。我有一个 VM 的 DNS 名称,它也有一个公共 IP。我在 Azure VM 防火墙中添加了一条防火墙规则,为应用服务 IP 启用 1433 端口(在属性-->其他出站 IP 地址中找到)。

在同一规则中,我添加了我的 IP(目的是检查规则是否正确应用)并且我能够使用我的 PC 上的 SSMS 连接到数据库。

我尝试在 App Service 上发布一个非常简单的 WebJob,只是为了尝试连接。这是我正在使用的代码:

static void Main(string[] args)
        {
            using (SqlConnection connection = new SqlConnection("Server=myvirtualmachine.westeurope.cloudapp.azure.com;Database=mydatabase;User ID=myuser;Password=mypassword;MultipleActiveResultSets=False;Connection Timeout=60;TrustServerCertificate=False;Persist Security Info=False"))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM myTable", connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                Console.WriteLine(reader.GetValue(i));
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
        }

如果我运行此 WebJob,我会收到错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.

将 Azure 应用服务连接到 Azure 虚拟机上的 SQL 数据库是否必须使用 VNET 或混合连接,还是可以通过其他方式实现?

【问题讨论】:

    标签: azure azure-web-app-service azure-virtual-machine


    【解决方案1】:

    是的,您需要使用 VNET 或启用公共 IP 才能通过 VM 内的 SQL 与 Appservice 建立连接。

    您阅读更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多