Access SQL Server with Active Directory DomainUsername credentials from Linux using Python

I have the following scenario.

I received access to a Windows SQL server with Domain/Username authentication attached to my personal Windows Active Directory and also our team’s Service Account’s Active Directory. When I test with my Windows work station, I can confirm that, I am able to programmatically fetch data from the SQL server. I tested this within my Windows machine with two Python scripts. One used pyodbc, and other used pymssql.

pyodbc example (local)

import pyodbc
conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};"
                      "SERVER=<server><instance>,<port>;"
                      "DATABASE=<database>;"
                      "Trusted_Connection=yes;"
                      )

cursor = conn.cursor()
cursor.execute("SELECT * FROM database.table")

for row in cursor.fetchall():
    print (row) 
cursor.close()

print("Completed!")

pymssql example (local)

import pymssql
conn = pymssql.connect(host='<server>', server='<instance>', port='<port>', database='<database>') 
cursor = conn.cursor()
cursor.execute("SELECT * FROM database.table")

for row in cursor.fetchall():
    print (row)
cursor.close()

The two scripts worked well in my personal Windows laptop. As you can see, in both scenarios, I did not have to specify my username or password. For example, when using pyodbc, I used ‘Trusted_Connection=yes‘. As, I was executing this script from my personal laptop which received the permission to the external Windows SQL server, this script automatically authenticate my request to the targeted SQL server and enable me to query the data from it. Similarly, when using pymssql, I was able to retrieve data directly from my local laptop.

However, my end objective is to retrieve data when I execute the script in a Linux Kubernets environment. Therefore, I extend the above two scripts while including my Active Directory <Domain><Username> and <password>. However, in both scenarios, the code does not get executed as per my expectation.i.e. the use of Active Directory <Domin><Username> and <password> combination with in Linux environment, throw me errors. The extended pyodbc and pymssql scripts are provided below. The received error messages are also stated.

pyodbc example

import pyodbc
conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};"
                      "SERVER=<server><instance>,<port>;"
                      "DATABASE=<database>;"
                      "Trusted_Connection=yes;"
                      "UID=<domain><username>;"
                      "PWD=<password>;"
                      "Integrated_Security=SSPI"
                      )

cursor = conn.cursor()
cursor.execute("SELECT * FROM database.table")

for row in cursor.fetchall():
    print (row) 
cursor.close()

print("Completed!")

pyodbc error encountered

    conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};"
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

pymssql example

import pymssql
conn = pymssql.connect(host='<server>', server='<instance>', port='<port>', database='<database>', user='<domain\username>', password='<password>', tds_version='7.0') 
cursor = conn.cursor()
cursor.execute("SELECT * FROM database.table")

for row in cursor.fetchall():
    print (row)
cursor.close()

pymssql error encountered:

    conn = pymssql.connect(host='<server>', server='<instance>', port='<port>', database='<db>', user='<domain>\<username>', password='<password>', tds_version='7.0')  
  File "src/pymssql/_pymssql.pyx", line 659, in pymssql._pymssql.connect
pymssql.exceptions.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:nUnable to connect: Adaptive Server is unavailable or does not exist (<server>)nNet-Lib error during Connection timed out (110)n')

I primarily followed Stack Overflow article, titled, Authenticate from Linux to Windows SQL Server with pyodbc, and SQL Server windows authentication linux python to accomplish my task. But, the steps I followed did not help me yet.

What steps am I missing? Do you see an issue in my code, or do you have alternate suggestions?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật