('HYT00', '[HYT00] [Microsoft][SQL Server 的 ODBC 驱动程序 17] 登录超时已过期 (0) (SQLDriverConnect)')

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

我尝试使用 OBDC 连接到 SQL 服务器。它在本地完美运行,docker 在 docker 桌面上本地运行。但是,当我尝试从 Azure 中的容器的 Web 应用程序连接时,它会抛出错误。

错误:root:错误消息:('HYT00','[HYT00] [Microsoft] [SQL Server 的 ODBC 驱动程序 17] 登录超时已过期(0)(SQLDriverConnect)') 错误:root:错误类型:OperationalError

    connect_string = f"DRIVER={DATABASE_DRIVER};"
    connect_string += f"SERVER={DATABASE_PROTOCOL}:{DATABASE_SERVER},"
    connect_string += f"{DATABASE_PORT};"
    connect_string += f"DATABASE={DATABASE_NAME};"
    connect_string += f"UID={DATABASE_USER};"
    connect_string += f"PWD={DATABASE_PASSWORD}"
    

    try:
        conn = pyodbc.connect(connect_string)

我试过:

  • 确保 SQL Server 可以访问。
  • 已验证的登录凭据是正确的
  • 防火墙未阻止连接可用于 IP 地址
  • 增加登录超时值

我也尝试过 Stack overflow 中的其他答案,但我仍然无法连接到服务器。

azure pyodbc unixodbc
1个回答
0
投票

我尝试了以下步骤:-

import  pyodbc

conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};''SERVER=tcp:sql-server.database.windows.net,1433;''DATABASE=<DATABASE>; UID=siliconserver;PWD=<Password>;')

#conn.commit()

cursor = conn.cursor()

cursor.execute('Select * FROM StudentReviews')

#conn.commit()

for  i  in  cursor:

print(i)

cursor.close()

conn.close()

输出:-

enter image description here

当我在连接字符串中拼错了 SQL Server 名称时,我得到了与您相同的错误代码,此外,如果您不允许在 SQL Server 网络中使用 Web 应用程序的 IP 地址,连接会超时。参考以下:-

enter image description here

确保在 SQL Server 网络选项卡中添加您的 Azure Web 应用程序或容器应用程序 IP 地址,如下所示:-

enter image description here

您可以找到您的 Web 应用程序和容器应用程序的 IP 地址,如下所示:-

enter image description here

enter image description here

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