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,))
Related
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
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 have configured a new SQL Server 2017.XXX instance in my Windows 10. Then, created a new database called CUSTOMER_DEVICES. I tried to connect to database using pyodbc. I could not and got below error:
Error - pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
When I use the same code in existing production server it works. Code below
import pyodbc
conn = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=CUSTOMER_DEVICES;Trusted_Connection=yes;')
cursor = conn.cursor()
print(cursor)
Check if SQL server Configuration Management is configured. Enable TCP, IP. Check below link Check here
I am using pymssql=2.1.1 to connect Azure database from python. Due to idle connection for few minutes, i am getting error (Write to the server failed) and not able to fetch the data.
I am using connect method of pymssql to create connection to Azure DB.
conn = pymssql.connect(server=v_host, user=v_user, password=v_passwd, database=v_db)
cursor = self.conn.cursor(as_dict=True)
cursor.execute(query)
The error looks like
(20006, b'DB-Lib error message 20006, severity 9:\nWrite to the server failed\nNet-Lib error during Connection reset by peer (104)\n')
If you want to us pymssql connect to Azure SQL database, make sure the following requirements:
Examples:
import pymssql
conn=pymssql.connect("xxx.database.windows.net", "username#xxx", "password", "db_name")
cursor = conn.cursor()
cursor.execute(query)
For more details, please see: Connecting to Azure SQL Database. Starting with version 2.1.1 pymssql can be used to connect to Microsoft Azure SQL Database. And you can troubleshoot the error by pymssql Frequently asked questions.
Another way, you also can try the pyodbc example:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute(query)
Here is the Azure document: Quickstart: Use Python to query an Azure SQL database.
Hope this helps.
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.