如何使用 Active Directory 集成身份验证通过 python SQL alchemy 连接到 Azure sql 数据库
How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication
我正在尝试使用 python 中的 SQL Alchemy 连接到 Azure SQL 数据库。数据库最近从本地迁移到 Azure,据我所知,azure 不支持 Windows Auth.
我可以使用 Active Directory Integrated Auth 从 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 默认情况下的身份验证进行连接,无论如何我可以解决这个问题吗?
(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)
据我所知,您的所有需求都在官方文档中Using Azure Active Directory with the ODBC Driver
。
首先,如果您想通过 pyodbc
连接到 Azure SQL 数据库,只有适用于 MS SQL 服务器的 odbc 驱动程序 17 版本支持 Active Directory 集成身份验证。所以请确保您已经为 SQL 服务器安装了最新的 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)
部分将 Azure Active Directory 集成身份验证的 DSN 配置到 SQL Azure。
然后,您可以按照下面的代码通过 SQL Alchemy
和 pyodbc
连接到 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 portal的``选项卡中复制,但请注意更改odbc驱动程序版本并删除UID
和PWD
选项。
To connect using Windows Integrated or Active Directory Integrated
(Windows driver only) authentication, specify
Authentication=ActiveDirectoryIntegrated in the connection string. The
driver will choose the correct authentication mode automatically. UID
and PWD must not be specified.
或者你可以考虑使用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()
希望对您有所帮助。
我正在尝试使用 python 中的 SQL Alchemy 连接到 Azure SQL 数据库。数据库最近从本地迁移到 Azure,据我所知,azure 不支持 Windows Auth.
我可以使用 Active Directory Integrated Auth 从 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 默认情况下的身份验证进行连接,无论如何我可以解决这个问题吗?
(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)
据我所知,您的所有需求都在官方文档中Using Azure Active Directory with the ODBC Driver
。
首先,如果您想通过 pyodbc
连接到 Azure SQL 数据库,只有适用于 MS SQL 服务器的 odbc 驱动程序 17 版本支持 Active Directory 集成身份验证。所以请确保您已经为 SQL 服务器安装了最新的 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)
部分将 Azure Active Directory 集成身份验证的 DSN 配置到 SQL Azure。
然后,您可以按照下面的代码通过 SQL Alchemy
和 pyodbc
连接到 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 portal的``选项卡中复制,但请注意更改odbc驱动程序版本并删除UID
和PWD
选项。
To connect using Windows Integrated or Active Directory Integrated (Windows driver only) authentication, specify Authentication=ActiveDirectoryIntegrated in the connection string. The driver will choose the correct authentication mode automatically. UID and PWD must not be specified.
或者你可以考虑使用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()
希望对您有所帮助。