【问题标题】:Azure Function - Connection String to On-Premises DatabaseAzure 函数 - 到本地数据库的连接字符串
【发布时间】:2021-12-24 08:54:15
【问题描述】:

我为 Azure 函数设置了 Azure 混合连接,以便连接到本地 SQL Server 数据库。但是,我将以下内容添加为连接字符串;我在尝试连接时收到以下错误。此处有关如何格式化连接字符串以使用混合连接的指导将不胜感激。我在下面使用的错误消息、代码和连接字符串。

Azure 函数使用的是 .NET Framework,而我使用的是 SQL Server 2019

A connection was successfully established with the server, but then an error occurred during the login process (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

Azure 函数代码

using System.Linq
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System.Data.SqlClient;

namespace AzureHybridConnectionTest
{
   public static class TestOnPremConnection
{
    [FunctionName("TestHybridConnection")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
    {
        log.Info("Testing Connection to On Premise SQL Database.");

        string connectionString = System.Environment.GetEnvironmentVariable("HybridConnectionString");
        log.Info(connectionString);

        using(SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            log.Info($"SQL Connection open to database {conn.Database}");
        }

        log.Info("SQL Connection closed");

        return req.CreateResponse(HttpStatusCode.OK, "Success!");

       }
     }
   }

连接字符串

Server=tcp:[hybrid connection endpoint];Initial Catalog=[db name];Persist Security Info=False;User ID=[user];Password=[password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;

【问题讨论】:

  • 它是一个非常旧或非常未打补丁的 SQL Server 实例,至少不支持 TLS 1.2?
  • @AlwaysLearning 我正在使用 SQL Server 2019
  • 如果您有权访问本地服务器,您是否查看了错误日志中的错误 18456 事件?将报告的 State 值与 MSSQLSERVER_18456 进行比较,看看 SQL Server 认为实际问题是什么。
  • 请参阅此answer。您的本地网络可能不允许该端口上的传入连接

标签: sql-server azure azure-functions database-connection hybrid


【解决方案1】:

谢谢Anand Sowmithiran。发布您的建议作为答案,以便对面临类似问题的其他社区成员有所帮助。

根据doc,如果您使用的是本地 sql 服务器,则可能存在端口问题,例如网络不允许传入连接。

混合连接可以将Azure App Service Web 应用程序连接到使用静态 TCP 端口的本地资源。支持的资源包括 Microsoft SQL Server、MySQL、HTTP Web API、移动服务和大多数自定义 Web 服务。

有关更多信息,请查看SO 并查看MSSQL 18456 错误日志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2019-04-24
    • 2018-07-24
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多