间歇性数据库连接失败

间歇性数据库连接失败

最近,我开始在 Web 应用程序的日志文件中看到间歇性错误。从应用程序的不同位置引发的错误始终与位于另一台服务器上的数据库连接失败有关:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

----

System.ComponentModel.Win32Exception (0x80004005): Access is denied

或者

The underlying provider failed on Open.

----

System.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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): Access is denied
   ....
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
ClientConnectionId:00000000-0000-0000-0000-000000000000

我在 Win Server 2008 R2 上使用 Microsoft SQL Server 标准版(64 位)10.50.4000.0

我检查了:

1 数据库自动关闭设置为离开

2 我检查了 APP 和 DB 服务器的事件日志,没有发现任何与 sqlserver 或网络错误相关的内容。但是事件日志包含 Schannel 错误,但从我谷歌搜索的结果来看,它与此无关。

答案1

如果您的网站出现错误时,您可以尝试通过其他方式(可能是 SSMS)连接到数据库,那就太好了。如果不能,您可以连接到数据库所在的服务器吗?如果您可以连接而网站无法连接,您使用的是同一个用户吗?重要的是确定网站无法连接到数据库是因为数据库问题(自行停止或太慢而无法响应)、连接到数据库服务器的网络错误,还是因为工作数据库的用户身份验证错误。采取什么行动取决于您遇到的问题。

至于如何实现这一点,您可能能够添加一些 Web 代码来捕获错误并向您发送电子邮件。然后,您可以在那个时间准确检查数据库。如果这不可能,也许您可​​以编写一个小型命令行应用程序来连接到数据库并将日期时间戳以及发生的任何错误写入文件。然后设置一个计划任务,每 5 分钟左右运行一次该程序。您甚至可以将该命令行应用程序放在 Web 服务器和另一台服务器上以获取更多信息(您也可以尝试以不同的用户身份运行该应用程序)。

例如,如果数据库正常,网络连接正常,但用户有时无法连接,那么用户可能偶尔会因为锁定策略而被锁定,因为网络上其他人/其他东西的无效登录尝试次数过多。或者,由于计划备份占用了数据库服务器上的所有可用资源,数据库可能在一天中的某些时间根本没有响应。等等。

相关内容