我有一个 Linux 应用服务和一个 SQL Server。应用服务需要连接到 SQL 数据库。这两种资源都集成在具有两个子网的 VNET 中:
我的 SQL Server 网络设置是:
一切都按预期进行,但只是在随机的时间内(分钟 - 天)。此后,我的应用服务将无法再连接到 SQL Server。例外情况如下:
未处理的异常。 Microsoft.Data.SqlClient.SqlException (0x80131904): 发生网络相关或实例特定的错误 同时建立与 SQL Server 的连接。服务器没有 已找到或无法访问。验证实例名称是否正确 并且 SQL Server 配置为允许远程连接。 (提供者:TCP 提供者,错误:35 - 捕获内部异常)
2024-09-12T11:05:35.8086896Z Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
2024-09-12T11:05:35.8087667Z ---> System.Net.Sockets.SocketException (00000005, 0xFFFDFFFF): Name or service not known
2024-09-12T11:05:35.8087732Z at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, Nullable`1 startingTimestamp)
2024-09-12T11:05:35.8087778Z at System.Net.Dns.GetHostAddresses(String hostNameOrAddress, AddressFamily family)
2024-09-12T11:05:35.8087840Z at Microsoft.Data.SqlClient.SNI.SNICommon.GetDnsIpAddresses(String serverName)
2024-09-12T11:05:35.8087960Z at Microsoft.Data.SqlClient.SNI.SNITCPHandle.Connect(String serverName, Int32 port, TimeSpan timeout, Boolean isInfiniteTimeout, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo)
2024-09-12T11:05:35.8088009Z at Microsoft.Data.SqlClient.SNI.SNITCPHandle..ctor(String serverName, Int32 port, Int64 timerExpire, Boolean parallel, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo, Boolean tlsFirst, String hostNameInCertificate, String serverCertificateFilename)
2024-09-12T11:05:35.8088048Z at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
2024-09-12T11:05:35.8088096Z at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
2024-09-12T11:05:35.8088140Z at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnectionString connectionOptions, Boolean withFailover)
2024-09-12T11:05:35.8088210Z at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
2024-09-12T11:05:35.8088258Z at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
2024-09-12T11:05:35.8088304Z at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
2024-09-12T11:05:35.8088362Z at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
2024-09-12T11:05:35.8088410Z at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
2024-09-12T11:05:35.8088476Z at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
2024-09-12T11:05:35.8088524Z at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternaloldConnection)
2024-09-12T11:05:35.8088568Z at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
2024-09-12T11:05:35.8088624Z at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
2024-09-12T11:05:35.8088659Z at Microsoft.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
2024-09-12T11:05:35.8088906Z --- End of stack trace from previous location ---
2024-09-12T11:05:35.8088981Z at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
2024-09-12T11:05:35.8089031Z at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
2024-09-12T11:05:35.8089080Z at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
2024-09-12T11:05:35.8089131Z at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
2024-09-12T11:05:35.8089177Z --- End of stack trace from previous location ---
2024-09-12T11:05:35.8089226Z at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
2024-09-12T11:05:35.8089300Z --- End of stack trace from previous location ---
2024-09-12T11:05:35.8089352Z at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
2024-09-12T11:05:35.8089400Z --- End of stack trace from previous location ---
2024-09-12T11:05:35.8089454Z at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
2024-09-12T11:05:35.8089507Z at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
2024-09-12T11:05:35.8089555Z at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
2024-09-12T11:05:35.8089607Z at [...].Data.DbInitializer.InitializeAsync(IServiceProvider serviceProvider) in C:\[...].cs:line 17
2024-09-12T11:05:35.8089669Z at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
2024-09-12T11:05:35.8089713Z at System.Threading.ThreadPoolWorkQueue.Dispatch()
2024-09-12T11:05:35.8089757Z at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
应用服务计划升级后也会发生这种情况。
可能是什么原因?
应用服务通过 VNET 失去与 SQL Server 的连接
一般来说,您提到的错误是由于专用端点配置不完整或可能是由于引用不正确的专用端点引起的。
正如 VenkatV 提到的,我们需要重新创建一个私有端点,并使用正确的端点再次引用它。
删除并读取专用端点将触发与端点关联的 DNS 记录的更新,这将确保是否删除任何过期的 DNS 条目并更新为新条目,并且此方法还将重置连接。
参考:
https://learn.microsoft.com/en-us/azure/private-link/private-endpoint-overview
https://learn.microsoft.com/en-us/azure/private-link/troubleshoot-private-endpoint-connectivity