mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.92k stars 561 forks source link

MSSQL - SSL Provider: The certificate chain was issued by an authority that is not trusted. #1309

Closed msharshath closed 10 months ago

msharshath commented 10 months ago

Hello all,

could some one help me on this:

import pyodbc

server = 'sername' database = 'abc' connection_str = f'DRIVER=ODBC Driver 17 for SQL Server;SERVER={server};DATABASE={database};Trusted_Connection=yes;' connection = pyodbc.connect(connection_str)

query = "SELECT * FROM calendar_dates" df = pd.read_sql(query, connection) print(df)

Error as:

Traceback (most recent call last): File "c:\Users\Desktop\All_Other_Project_Docs\connection.py", line 6, in connection = pyodbc.connect(connection_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') i tried changing the DRIVER=SQL SERVER.

import pyodbc

server = 'sername' database = 'abc' connection_str = f'DRIVER=SQL Server;SERVER={server};DATABASE={database};Trusted_Connection=yes;' connection = pyodbc.connect(connection_str)

query = "SELECT * FROM calendar_dates" df = pd.read_sql(query, connection) print(df)

error as:

Traceback (most recent call last): File "c:\Users\z004tu0x\Desktop\All_Other_Project_Docs\connection.py", line 6, in connection = pyodbc.connect(connection_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (5)')

From ODBC Data sources, i got the information as: errors

So, Even i tried changing it to

import pyodbc

server = 'sername' database = 'abc' connection_str = f'DRIVER=ODBC Driver 18 for SQL Server;SERVER={server};DATABASE={database};Trusted_Connection=yes;' connection = pyodbc.connect(connection_str)

query = "SELECT * FROM calendar_dates" df = pd.read_sql(query, connection) print(df)

Error as:

Traceback (most recent call last): File "c:\Users\z004tu0x\Desktop\All_Other_Project_Docs\connection.py", line 6, in connection = pyodbc.connect(connection_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted.\r\n (-2146893019) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722 (-2146893019)')

Kindly help me to resolve the issue.

v-chojas commented 10 months ago

Did you read https://go.microsoft.com/fwlink/?linkid=2226722 ?

gordthompson commented 10 months ago

For "ODBC Driver 18 for SQL Server" add ;TrustServerCertificate=yes to your connection string.