【问题标题】:How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication如何使用 Active Directory 集成身份验证通过 python SQL alchemy 连接到 Azure sql 数据库
【发布时间】:2019-11-16 00:13:41
【问题描述】:

我正在尝试使用 Python 中的 SQL Alchemy 连接到 Azure SQL 数据库。该数据库最近从本地迁移到 Azure,据我了解,Azure 不支持 Windows 身份验证。
我可以使用 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)

【问题讨论】:

    标签: python sql azure sqlalchemy pyodbc


    【解决方案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) 部分配置 Azure Active Directory 集成身份验证到 SQL Azure 的 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选项。

    使用 Windows Integrated 或 Active Directory Integrated 进行连接 (仅限 Windows 驱动程序)身份验证,指定 Authentication=ActiveDirectoryIntegrated 在连接字符串中。这 驱动程序将自动选择正确的身份验证模式。 UIDPWD 不得指定。

    或者你可以考虑使用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()
    

    希望对你有帮助。

    【讨论】:

    • 感谢您清晰的回答。简单地切换到 ODBC Driver 17 对我有用。这是有效的连接字符串:“mssql+pyodbc://@*Server*/*Database*?driver=ODBC Driver 17 for SQL Server;Encrypt=yes;TrustServerCertificate=no”由于某种原因它在添加时不起作用Authentication=ActiveDirectoryIntegrated
    猜你喜欢
    • 2018-09-21
    • 2021-07-13
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多