间歇性 sqlalchemy 连接问题 TCP 提供程序错误 0x68

问题描述 投票:0回答:1

我们有连接到 Azure SQL 数据库的 python 函数应用程序。它一直运行良好,直到 3 月底,当我们开始间歇性地遇到以下错误时:“[08001] [Microsoft][SQL Server 的 ODBC 驱动程序 17]TCP 提供程序:错误代码 0x68 (104) (SQLDriverConnect)”

在交易量较大的日子里,它似乎更频繁地发生,多达 15% 的调用会遇到此错误。所有研究似乎都指向网络问题,但是无论数量如何,都无法关闭 sql 会话也会导致此错误?还有什么可以解决这个问题吗?

AZSQL 和函数应用程序都设置为 1.2 TLS,并将 sqlalchemy 固定到 1.4.47.

python-3.x sqlalchemy azure-functions pyodbc
1个回答
0
投票

如果您的函数应用程序中的 Azure SQL 连接间歇性失败,请确保您的函数应用程序未在任何公司代理、LAN 或 VPN 后面运行,或者任何防火墙连接限制与 Azure SQL 的连接。

我尝试在受限的防火墙网络 LAN 中使用 Pyodbc 连接到 Azure SQL,即使 SQL 服务器的实例名称、数据库和连接字符串正确,Azure SQL 服务器网络选项卡中允许使用客户端 IP,也会收到以下错误。参考以下:-

错误:-

enter image description here

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')

当我尝试在没有任何网络限制的情况下使用客户端机器连接到我的 Azure SQL 时,我能够像下面这样查询 Azure SQL 数据库:-

我的 Python 代码:-

import pyodbc

  

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'

'Server=tcp:<sqlservername>.database.windows.net,1433;'

'Database=<database-name>;'

'Uid=<username>;'

'Pwd=<password>')

  
  

cursor = conn.cursor()

cursor.execute('SELECT * FROM StudentReviews')

  

for  i  in  cursor:

print(i)

  

cursor.close()

conn.close()

输出:-

enter image description here

为了检查 Azure SQL 连接失败,选择您的 Azure SQL Server > 诊断和解决问题 > 搜索连接:排除数据库可用性和连接错误 > 选择连接超时或断开连接并执行解决方案中给出的见解,如下所示: -

enter image description here

选择问题的开始时间并粘贴您的错误代码单击提交以获取见解:-

enter image description here

enter image description here

您可以从结果中得出见解。

您还可以运行建议中给出的 Powershell 脚本来解决间歇性超时或连接问题并获得见解,如下所示:-

enter image description here

您可以通过将 SSMS 中的脚本运行到系统表来检查连接问题,如下所示:-

代码:-

SELECT start_time, end_time, database_name, sku, avg_cpu_percent, max_worker_percent, max_session_percent 
FROM sys.resource_stats;

输出:-

enter image description here

参考:-

解决 Azure SQL 数据库的常见连接问题 - Azure SQL 数据库 |微软学习

错误代码 0x68 (104) (SQLGetData) · Issue #680 · mkleehammer/pyodbc · GitHub

[pyodbc.OperationalError: ('08001', '[08001] Microsoft][SQL Server 的 ODBC 驱动程序 17]TCP 提供程序:错误代码 0x2746 (10054) (SQLDriverConnect)') · Issue #610 · mkleehammer/pyodbc · GitHub

© www.soinside.com 2019 - 2024. All rights reserved.