如何使用Active Directory集成身份验证通过python SQL炼金术连接到Azure sql数据库

时间:2019-07-05 10:56:49

标签: python sql azure sqlalchemy pyodbc

我正在尝试使用python中的SQL Alchemy连接到Azure SQL数据库。该数据库最近已从本地迁移到Azure,据我了解,azure不支持Windows Auth。
我可以使用Active Directory集成身份验证从SSMS连接到数据库。

当Db处于Prem状态时,我将使用以下连接字符串,并且该字符串有效:

"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server"

我尝试了其他一些连接字符串,但无法使其正常工作。

"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server?Integrated Security=true"
"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server?Trusted_Connection=true"

我不断收到以下错误,似乎sql alchemy默认情况下尝试通过Windows auth进行连接,无论如何我可以解决此问题吗?

(pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server. (40607) (SQLDriverConnect); [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server. (40607)')
(Background on this error at: http://sqlalche.me/e/dbapi)

1 个答案:

答案 0 :(得分:1)

众所周知,您的所有需求都在官方文档Using Azure Active Directory with the ODBC Driver中。

首先,如果要通过pyodbc连接到Azure SQL数据库,则仅MS SQL Server的odbc驱动程序17版本支持Active Directory集成身份验证。因此,请确保已为SQL Server安装了最新的odbc驱动程序,或者可以从https://docs.microsoft.com/en-us/sql/connect/odbc/microsoft-odbc-driver-for-sql-server?view=sql-server-2017下载。

第二,请遵循UI Additions for Azure Active Directory (Windows driver only)部分,为SQL Azure配置用于Azure Active Directory集成身份验证的DSN。

然后,您可以按照以下代码通过SQL Alchemypyodbc连接到SQL Azure。

from urllib import parse
from sqlalchemy import create_engine

connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:<your sql azure server name>.database.windows.net,1433;Database=<your database name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryIntegrated'
params = parse.quote_plus(connecting_string)

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
result = connection.execute("select 1+1 as res")
for row in result:
    print("res:", row['res'])
connection.close()

注意:上面连接字符串的值可以从Azure门户的``选项卡中复制,但请注意要更改odbc驱动程序版本并删除UIDPWD选项。

enter image description here

  

使用Windows Integrated或Active Directory Integrated连接   (仅Windows驱动程序)身份验证,指定   连接字符串中的 Authentication = ActiveDirectoryIntegrated 。的   驱动程序将自动选择正确的身份验证模式。 UID   和 PWD 不能指定。

或者您可以考虑使用Authentication=ActiveDirectoryPassword,它比Authentication=ActiveDirectoryIntegrated和下面的代码更容易。

from urllib import parse
from sqlalchemy import create_engine

your_user_name = '<your AAD user or configured in SQL Azure Server as the figure below>'
your_password_here = '<your AAD account password>'
#connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:sqlserverleon.database.windows.net,1433;Database=dbleon;Uid='+your_user_name+';Pwd='+your_password_here+';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword'
connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:sqlserverleon.database.windows.net,1433;Database=dbleon;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryIntegrated'
params = parse.quote_plus(connecting_string)

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
result = connection.execute("select 1+1 as res")
for row in result:
    print("res:", row['res'])
connection.close()

enter image description here

希望有帮助。