SSS*_*SSS 3 python sql sqlalchemy azure pyodbc
我正在尝试在 python 中使用 SQL Alchemy 连接到 Azure SQL 数据库。该数据库最近从本地迁移到 Azure,据我所知,azure 不支持 Windows 身份验证。
我可以使用 Active Directory 集成身份验证从 SSMS 连接到数据库。
当 Db 在本地时,我将使用以下连接字符串并且它工作正常:
"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server"
Run Code Online (Sandbox Code Playgroud)
我尝试了其他一些连接字符串,但无法使其正常工作。
"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server?Integrated Security=true"
"mssql+pyodbc://@*Server*/*DB*?driver=SQL Server?Trusted_Connection=true"
Run Code Online (Sandbox Code Playgroud)
我不断收到以下错误,似乎 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)
Run Code Online (Sandbox Code Playgroud)
据我所知,您的所有需求都在官方文件中Using Azure Active Directory with the ODBC Driver。
首先,如果您想通过pyodbc. 因此,请确保您已为 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)配置用于 Azure Active Directory 集成身份验证到 SQL Azure 的 DSN。
然后,您可以按照以下代码SQL Alchemy使用pyodbc.
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()
Run Code Online (Sandbox Code Playgroud)
注意:以上连接字符串的值,可以从Azure门户的``选项卡中复制,但请注意更改odbc驱动程序版本并删除UID和PWD选项。
要使用 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()
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
5384 次 |
| 最近记录: |