gpt4 book ai didi

python - 在Python中,是否可以使用Active Directory密码身份验证连接Azure SQL Server?

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:18 25 4
gpt4 key购买 nike

我尝试使用 pyodbc 库连接 sql server,

driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+host+';PORT=1443;DATABASE='+database+';UID='+user+';PWD='+ password+';Authentication=ActiveDirectoryPassword')

在上面的代码中,我选择了 ActiveDirectoryPassword(AAD) 作为我的身份验证类型,因为我只有 AAD 访问权限。但是收到以下错误消息。

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

当我将驱动程序更改为 ODBC Driver 13 时,收到不同的错误消息。

driver= '{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+host+';PORT=1443;DATABASE='+database+';UID='+user+';PWD='+ password+';Authentication=ActiveDirectoryPassword')

这次我收到以下错误消息。

OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The Microsoft Online Services Sign-In Assistant could not be found. Install it from http://go.microsoft.com/fwlink/?LinkId=234947. If it is already present, repair the installation. [2].  (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection (2); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Invalid connection string attribute (0)')

是否可以使用pyodbc通过AAD身份验证连接azure sql server?如果可能的话,使用 AAD 验证 sql server 的正确方法是什么?

最佳答案

首先,根据Additional considerations小节官方文档使用 Azure Active Directory 身份验证通过 SQL 进行身份验证,如下,pyodbc可以通过 AAD 身份验证连接 Azure SQL 数据库。

ODBC version 13.1 supports Azure Active Directory authentication however bcp.exe cannot connect using Azure Active Directory authentication because it uses an older ODBC provider.

那么,有一个SQL Server的官方文档Using Azure Active Directory with the ODBC Driver介绍了如何操作以及一些限制(如果不在 Windows 上)。

The Microsoft ODBC Driver for SQL Server with version 13.1 or above allows ODBC applications to connect to an instance of SQL Azure using a federated identity in Azure Active Directory with a username/password, an Azure Active Directory access token, or Windows Integrated Authentication (Windows driver only). For the ODBC Driver version 13.1, the Azure Active Directory access token authentication is Windows only. The ODBC Driver version 17 and above support this authentication across all platforms (Windows, Linux and Mac).

还有它

accomplished through the use of new DSN and connection string keywords, and connection attributes.

您需要按照文档配置一些设置以启用新的 DSN,然后使用连接字符串连接 SQL Azure。

Driver={ODBC Driver 13 for SQL Server};Server={server};UID=myuser;PWD=myPass;Authentication=ActiveDirectoryPassword

并注意这一点。

To connect using an Azure Active Directory account username and password, specify Authentication=ActiveDirectoryPassword in the connection string and the UID and PWD keywords with the username and password, respectively.

同时,与其他资源一样,这些似乎很旧,但是 blog详细介绍了 SQL Azure 中的配置多重身份验证,并介绍了 Connecting to SQL Database By Using Azure Active Directory Authentication 的 Markdown 文档.

此外,对于 Linux 和 Mac,还有 blog申请其他可能有助于建立SQL Server连接的解决方案。

希望有帮助。

关于python - 在Python中,是否可以使用Active Directory密码身份验证连接Azure SQL Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53965056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com