简体   繁体   English

无法使用 Key Vault Secret 从 Python 连接 Azure SQL 数据库

[英]Unable to connect Azure SQL database from Python using Key Vault Secret

My Key vault secret is我的密钥保管库秘密是

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

I am able to connect to Azure SQL database using the above KV secret from Azure ADF.我能够使用来自 Azure ADF 的上述 KV 机密连接到 Azure SQL 数据库。 I am trying to do the same thru Python code:我正在尝试通过 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()

But the code fails with the below error:但是代码失败并出现以下错误:

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)')

Can you help me what changes I have to do in KV or still Azure does not support Python KV?您能帮我在 KV 中进行哪些更改,还是 Azure 不支持 Python KV? Thanks.谢谢。

You connection string should be like below.您的连接字符串应如下所示。

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;

I believe if you change the secret in kv, you can fix the issue.我相信如果你改变 kv 中的秘密,你可以解决这个问题。

Offical doc:官方文档:

Connect SQL Server 连接SQL服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 python 从 Azure Key Vault 获取机密/证书 | “KeyVaultManagementClient”对象没有“get_secret”属性 - unable to get the secret / certificate from Azure Key Vault using python | 'KeyVaultManagementClient' object has no attribute 'get_secret' 无法使用 python 访问 azure key vault secret - Cannot access azure key vault secret with python 从 Jupyter 交互式登录到 Azure 以从密钥库中获取 Secret - Interactive Login to Azure from Jupyter to fetch Secret from key vault 使用 Python 从 Azure Key Vault 解码 JsonWebKey - Decoding JsonWebKey from Azure Key Vault with Python 使用 Python 中 Azure-key-vault 中的密钥加密 azure blob - Encrypt azure blob using Key in Azure-key-vault in Python Django 1.9.9无法从Windows连接Azure SQL数据库 - Django 1.9.9 unable to connect Azure SQL database from windows Creating DataBricks Azure Key Vault Secret Scope Backend using Rest Api and DataBricks CLI - Creating DataBricks Azure Key Vault Secret Scope Backend using Rest Api and DataBricks CLI Azure 密钥保管库在 Python 中创建 - Azure key vault create in Python 在 Azure 中存储服务主体凭据以使用 Python 对 Key Vault 进行身份验证 - Storing Service Principal Credentials in Azure to Authenticate Key Vault using Python 使用 python w/rest api 与 Azure Key Vault 交互 - Interacting with Azure Key Vault using python w/ rest api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM