无法使用密钥Vault Secret从Python连接Azure SQL数据库
我的秘密是无法使用密钥Vault Secret从Python连接Azure SQL数据库,python,azure,azure-sql-database,pyodbc,azure-keyvault,Python,Azure,Azure Sql Database,Pyodbc,Azure Keyvault,我的秘密是 integrated security=False;encrypt=True;connection timeout=30;data source=yyy.database.windows.net;initial catalog=db-xxxx;user id=xx-user;password=pwd-xx 我能够使用来自Azure ADF的上述KV机密连接到Azure SQL数据库。 我正试图通过Python代码执行相同的操作: from azure.keyvault.secret
integrated security=False;encrypt=True;connection timeout=30;data source=yyy.database.windows.net;initial catalog=db-xxxx;user id=xx-user;password=pwd-xx
我能够使用来自Azure ADF的上述KV机密连接到Azure SQL数据库。
我正试图通过Python代码执行相同的操作:
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
import pyodbc
KVUri = "https://yyy-kv.vault.azure.net/"
credential = DefaultAzureCredential()
client = SecretClient(vault_url=KVUri, credential=credential)
secretName = "xxxx"
print("Retrieving your secret")
retrieved_secret = client.get_secret(secretName)
print(f"Your secret is '{retrieved_secret.value}'.")
print(" done.")
# The code fails after this.
with pyodbc.connect(retrieved_secret.value) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
但代码失败,出现以下错误:
Traceback (most recent call last):
File "first.py", line 25, in <module>
with pyodbc.connect(retrieved_secret.value) as conn:
pyodbc.InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
回溯(最近一次呼叫最后一次):
文件“first.py”,第25行,在
使用pyodbc.connect(检索到的\u secret.value)作为conn:
pyodbc.InterfaceError:('IM002'、'[IM002][unixODBC][Driver Manager]未找到数据源名称,并且未指定默认驱动程序(0)(SqlDriverConnection)'))
你能帮我在KV中做些什么更改吗?或者Azure仍然不支持Python KV?谢谢。您的连接字符串应该如下所示
Driver={ODBC Driver 17 for SQL Server};Server=yy.database.windows.net,1433;Database=dbname;Uid=sasasa;Pwd={pwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;
我相信如果你改变kv的秘密,你就能解决这个问题
官方文件:
谢谢,杰森。我知道连接字符串。我正在尝试使用Azure Key Vault secret连接到Azure SQL。