Home Connecting to Azure SQL Server with AAD authentication using PyODBC
Reply: 0

Connecting to Azure SQL Server with AAD authentication using PyODBC

user1737
1#
user1737 Published in April 26, 2018, 5:38 am

Environment:

Docker image: python:2.7.14-stretch (which means Debian 9 as underlying OS)
Installed SQL Server ODBC driver for Debian 9 (https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017)

using pyodbc module

Connecting to Azure SQL Server with AAD authentication.

Issue:

First connection to DB is made perfectly fine. However any next connection to same DB or other DB is failed with below error:

Exception::: ('08001', u'[08001] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:140A90F1:lib(20):func(169):reason(241)] (-1) (SQLDriverConnect)') Cannot connect to DB

Please note that this is issue with only when using AAD autnethcation mode. If I use simple sql server user, everything runs fine.

Below is the example code snippet.

db_list = [TEST_DB1, TEST_DB2]
sql_conn = None
for db in db_list:
    try:
        conn_string = 'DRIVER={ODBC Driver 17 for SQL Server};' \
                      'SERVER=' + <db_url> + \
                      ';DATABASE=' + <db_name> + \
                      ';UID=' + <db_username> + \
                      ';PWD=' + <db_password> + \
                      ';Authentication=ActiveDirectoryPassword'
        print conn_string
        sql_conn = pyodbc.connect(conn_string)
except Exception as e:
    print "Exception:::", e
    print 'Cannot connect to DB' + str(sys.exc_info()[0])
    return None
sql_conn.cursor().execute(<some SQL Query>)
sql_conn.close()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314337 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO