How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication

前端 未结 1 1818
傲寒
傲寒 2021-01-21 09:59

I am trying to connect to an Azure SQL Database using SQL Alchemy in python. The database was recently moved to Azure from on-prem and as I understand that azure does not suppor

1条回答
  •  伪装坚强ぢ
    2021-01-21 10:37

    As I known, all of your need are in the offical document Using Azure Active Directory with the ODBC Driver.

    First, only the odbc driver 17 version for MS SQL Server supports Active Directory Integrated Authentication if you want to connect to Azure SQL Database by pyodbc. So please make sure you have installed the lastest odbc driver for SQL Server, or you can download from https://docs.microsoft.com/en-us/sql/connect/odbc/microsoft-odbc-driver-for-sql-server?view=sql-server-2017.

    Second, please follow the section UI Additions for Azure Active Directory (Windows driver only) to configure the DSN for Azure Active Directory Integrated authentication to SQL Azure.

    Then, you can follow the code below to connect to SQL Azure by SQL Alchemy with pyodbc.

    from urllib import parse
    from sqlalchemy import create_engine
    
    connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:.database.windows.net,1433;Database=;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()
    

    Note: The value of connection string above, you can copy it from the `` tab on Azure portal, but please notice to change the odbc driver version and remove the UID and PWD options.

    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.

    Or you can consider to use Authentication=ActiveDirectoryPassword which be easier than Authentication=ActiveDirectoryIntegrated and the code as below.

    from urllib import parse
    from sqlalchemy import create_engine
    
    your_user_name = ''
    your_password_here = ''
    #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()
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题