I'm trying to connect to SQL Server remotely from my Linux (Debian) machine using Python, I tried everything but nothing has worked
import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};'
'SERVER=192.168.108.186;'
'PORT=1433;DATABASE=teste;'
'UID=username;'
'PWD=my_password;'
'Trusted_Connection=no;'
'TrustServerCertificate=yes')
cursor = cnxn.cursor()
This gives me:
pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC Driver 18 for SQL Server]SSPI
Provider: No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)
(851968) (SQLDriverConnect)')
The same thing if I use pymssql
import pymssql
pymssql.connect(server=r'192.168.108.186', user=r'username', password=r'my_password', database=r'teste')
and this gives me
pymssql.OperationalError: (20002, b'DB-Lib error message 20002,
severity 9:\nAdaptive Server connection failed (192.168.108.186)\n
DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.108.186)\n')
for pyodbc.Error:
One of the reasons is an incorrect password. In that case, a Kerberos ticket is not created.
Also, as #AlwaysLearning said Trusted_Connection=yes; means to use the Windows Authentication, you have to go with either one of them.
You can see this link as well
https://techjogging.com/access-sqlserver-windows-trusted-connection-linux-python.html
Related
Need help using a trusted connection to connect to SQL Server using SQLALCHEMY
So far I have this code to establish the connection
import sqlalchemy as sa
engine = sa.create_engine("mssql+pyodbc://REPORTING/REPORTING_System?driver={SQL Server}?TrustedConnection=yes")
view = f"SELECT Date From Table"
df = pd.read_sql_query(view, engine)
However I got this error message - DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I tried to change the driver to {ODBC Driver 17 for SQL Server}
It did not help.
I'm trying to connect to a SQL server database using pyodbc in Python 3. But I get an error when I'm trying to establish the connection.
I do something like this:
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
And I get this:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol][error:140B40C7:SSL routines:SSL_do_handshake:peer did not return a certificate] (-1) (SQLDriverConnect)')
Does anybody know how to solve this? The database is not my own, so I hope there is a solution that doesn't require changing any settings there.
I'm running Ubuntu within the Windows Subsystem for Linux.
There is a breaking change in ODBC Driver 18 for SQL Server
Similar to the HTTP to HTTPS default changes made in web browsers a few years back (and the security reasons for them), we are changing the default value of the Encrypt connection option from no to yes/mandatory.
ODBC Driver 18.0 for SQL Server Released
So this
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
is the same as
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=yes')
If you don't want an encrypted connection you must opt out:
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no')
We also changed the behavior of TrustServerCertificate to not be tied to the Encrypt setting
So if your server is using a self-signed certificate, you also must opt out of certificate validation. so
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no;TrustServerCertificate=yes')
I ended up taking my script out of WSL. Running the same command (with David's additions or ODBC Driver 17 for SQL Server instead of 18) under Windows works without issues in my case.
I am having trouble with a MS SQL connection when using pyinstaller. When run in interactive mode, everything works as expected. After compiling to an exe, the MS SQL database connection times out on the first query with the following error:
(pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expire (0); ...
My connection string is similar to the following:
create_engine(
"mssql+pyodbc://USER:PASSWORD#SERVERIP/DB_NAME?driver=ODBC+Driver+17+for+SQL+Server"
)
In attempting to diagnose the issue, I am printing out the drivers available to pyodbc with pyodbc.drivers() (which shows a large disparity between available drivers in compiled vs interactive) as well as the driver in use using
print(session.bind.dialect.name)
> pyodbc
print(session.bind.dialect.driver)
> mssql
It returns the upper level python modules which are being used but not the .dll that is handling it at a lower level. Is there any way to find which exact driver is being used? Any tips on what could be causing the error in the compiled version in the firstplace would be appreciated as well.
The issue may be in your connection string.
To create a proper connection string to connect to MSSQL Server ODBC driver with sqlAlchemy use the following:
import urllib
from sqlalchemy import create_engine
server = 'serverName\instanceName,port' # to specify an alternate port
database = 'mydb'
username = 'myusername'
password = 'mypassword'
params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Also, you can check the following article Connecting to Microsoft SQL Server using SQLAlchemy and PyODBC
Is there any way to find which exact driver [.dll] is being used?
import pyodbc
cnxn = engine.raw_connection()
print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)) # msodbcsql17.dll
print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER)) # 17.08.0001
I am trying to connect SQL Server using pymssql package with active directory authentication. Below is the code.
db = pymssql.connect(server=url, user=r'Domain\user', password=password, database=database)
cursor = db.cursor()
But it fails with below error:
Error (18452, b'Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed ()\n')
Python version: Python 3.8.6
PYMSSQL version: pymssql 2.1.5
For most client libraries you can't specify the username/password with Windows Integrated Auth. It just connects as the identity of the current process.
To do otherwise would require the driver to implement (or us a library that implements) the NTLM or Kerberos authentication protocols, and AFAIK only the Microsoft JDBC Driver for SQL Server does that.
Not sure that will work. Try it like this.
# connect to SQL Server
import pyodbc
from sqlalchemy import create_engine
driver= '{SQL Server Native Client 11.0}'
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=your_server_name;'
r'DATABASE=your_db_name;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Employee')
# print contents of table
for row in cursor:
print('row = %r' % (row,))
My OS is Scientific Linux 7.5 and wants to connect to MS SQL server using python 2.7.5 i have configured unixODBC 2.3.1 and Microsoft ODBC Driver 13 for SQL Server
But when i use the following code:
import pyodbc
cnxn = pyodbc.connect(
'Driver={/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2};'
'Server=localhost;'
'User=sa;'
'Password=xxx;'
'Database=yyy;'
'Trusted_Connection=yes;')
i get the following error
pyodbc.Error: ('HY000', u'[HY000] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]SSPI Provider: No Kerberos credentials available (default cache: KEYRING:persistent:1000) (851968) (SQLDriverConnect)')
and got the same error when used 'Driver={ODBC Driver 13 for SQL Server};'
if i use 'Driver={SQL Server};' i got stuck on following error
pyodbc.Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
You should remove 'Trusted_Connection=yes;' from your connection string. It is used for Windows authentication, while you are trying to connect with SQL authentication (providing user name and password).
An alternate cause and solution, in case anyone ends up here for this reason:
If you're relying on an env file to populate the Username and Password components of the pyodbc connection string, and something goes wrong with your execution such that those values are not accessible, then you will get that No Kerberos credentials available error message.
Check the value of the connection string; in my case, the username and password were missing from the SQLAlchemy engine string when I printed it. Running my docker container with the proper --env-file= flag addressed this case.