无法使用 pyodbc 连接到 Synapse Sql

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

我正在尝试使用 Azure 函数中的 Python 使用托管标识连接到 Synapse Sql。下面是代码。

def fun():
    server = 'servername'
    db_name = 'dbname'
    conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER={'+server+'};Authentication=ActiveDirectoryIntegrated;DATABASE={'+db_name+'};')
    conn.timeout = 3000
    return conn
    

我收到以下错误。

('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

知道我在这里缺少什么吗?

谢谢。

python azure-active-directory azure-functions pyodbc azure-managed-identity
1个回答
0
投票

我尝试使用以下代码使用 python 将我的 Synapse sql 数据库与活动目录身份验证连接:

import pyodbc
server = '<servername>'
db_name = '<dbname>'
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER={'+server+'};Authentication=ActiveDirectoryIntegrated;DATABASE={'+db_name+'};')
conn.timeout = 3000
return conn

我遇到以下错误:

enter image description here

this的帮助下,我为SQL驱动程序安装了ODBC 13,并使用以下代码对其进行了检查:

import sqlalchemy as sa
   
username = "username"
password = "password"
host = "<servername>"
database = "<dbname>"
authentication = "ActiveDirectoryPassword"
conn_string = sa.engine.url.URL.create(
     "mssql+pyodbc",
     username=username,
     password=password,
     host=host,
     port=1433,
     database=database,
     query={"driver": "ODBC Driver 13 for SQL Server","authentication": authentication},
 )
engine = sa.create_engine(conn_string, pool_timeout=30)
connection = engine.connect()

连接成功,没有任何错误。

enter image description here

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