While trying to connect to MSSQL Server 2012 using pymssql, I get the following error.
My server name in Windows Authentication is SARATH,User Name is Sarath\SarathShanker and I did not set a password.
Code:
mssql_conn=pymssql.connect(host='SARATH',user='Sarath\SarathShanker',password='',database='matrix')
Error:
Traceback (most recent call last):
File "", line 1, in
File "pymssql.pyx", line 556, in pymssql.connect (pymssql.c:7990)
pymssql.OperationalError: (18452, 'Login failed. The login is from an untrusted
domain and cannot be used with Windows authentication.DB-Lib error message 18452
, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB
-Lib error message 20002, severity 9:\nAdaptive Server connection failed\nDB-Lib
error message 18452, severity 14:\nGeneral SQL Server error: Check messages fro
m the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server conne
ction failed\n')
How should I modify my script in order to connect to MSSQL Server using pymssql.
P.S I have already imported pymssql as well. (Not Shown in Code above)
Try this:
conn = pymssql.connect(host='myhost', database='mydb')
This is with Python Version 3.4 and for Windows Authentication.
If on RHEL, try
os.environ["FREETDSCONF"] = "/etc/freetds.conf"
If you are using the latest version of pymssql (I use 2.1.3),
pymssql.connect(server='<TEST_SERVER>', database='<TEST_DB>')
Simply replace the '' and '' with your server and DB name.
Related
I need to build a model using a Python script in SQL Server. But it needs to have multiple input data sets. And I gather you can only have one. I read that a way to do it is to use pyodbc inside sp_execute_external_script.
But it won't connect. The exact same code does work in Spyder however so it's not a general configuration issue (I can also connect from Tableau etc.). As it's the local instance I tried using a dot (.) for the server name but that didn't work so I also tried just putting the server and instance name in full (hence the two different connection strings below).
The code:
EXEC sp_execute_external_script
#language =N'Python',
#script=N'
import pyodbc
sql_conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};SERVER=XXX\YYY;DATABASE=MY_PYTHON;trusted_connection=Yes")
sql_conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=.;Database=MY_PYTHON;Trusted_Connection=Yes;")
'
The error message:
Msg 39004, Level 16, State 20, Line 52
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 52
An external script error occurred:
Error in execution. Check the output for more information.
Traceback (most recent call last):
File "", line 5, in
File "C:\ProgramData\GTMDEV\Temp-PY\Appcontainer1\A0F0D72C-4B70-46BD-8307-D51C36E9A8F1\sqlindb_0.py", line 39, in transform
sql_conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=.;Database=MY_PYTHON;Trusted_Connection=Yes;")
Msg 39019, Level 16, State 2, Line 52
An external script error occurred:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')
SqlSatelliteCall error: Error in execution. Check the output for more information.
STDOUT message(s) from external script:
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
File "C:\Program Files\Microsoft SQL Server\MSSQL15.GTMDEV\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 605, in rx_sql_satellite_call
rx_native_call("SqlSatelliteCall", params)
File "C:\Program Files\Microsoft SQL Server\MSSQL15.GTMDEV\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 375, in rx_native_call
ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
Internet research has led me to create a login for SQLRUserGroupYYY in SQL Server and add it to "Allow log on locally" in Local Security Policy. But that has not changed anything. And I can't find anything else that works.
In case it is relevant the server is on my PC and it's name is MYPCNAME\MYNAMEDINSTANCE. (And the name is correct and it is running (suggested in the error message) because the connection works in Spyder, Tableau etc.).
Any ideas what else I can try?
Thanks.
For reference, this is one of the articles that suggested the method:
https://medium.com/wandering-in-advanced-analytics/passing-multiple-t-sql-queries-to-sp-execute-external-script-and-loop-back-requests-d153ee582235
The example given is for R but the principle should be the same for Python. I did try it in R and got the same error message.
This is another post with the same sort of issue (that had a link for the SQLRUserGroup login suggestion):
pyodbc.OperationalError when used inside sp_execute_external_script
I have Anaconda installed on one of our servers, and some code which successfully opens a connection to SQL (2016) on another server:
import pyodbc
conn_string = 'DRIVER={SQL Server Native Client 11.0};SERVER=wpic-smir;Trusted_Connection=yes'
conn = pyodbc.connect(conn_string)
cursor=conn.cursor()
qstring = 'select UserID from Diler_BW.Beckwith.V_Thermovals'
cursor.execute(qstring)
row=cursor.fetchone()
print row[0]
del cursor
del conn
Now, on the same server as Anaconda, we have SQL Server (2017) installed, and Machine Learning Service for Python is also installed. I'm trying to do run essentially the same code above, but from within SQL:
exec sp_execute_external_script
#LANGUAGE = N'Python',
#script = N'
import pyodbc
conn_string = ''DRIVER={SQL Server Native Client 11.0};SERVER=wpic-smir;Trusted_Connection=yes''
conn = pyodbc.connect(conn_string)
cursor=conn.cursor()
qstring = ''select UserID from Diler_BW.Beckwith.V_Thermovals''
cursor.execute(qstring)
row=cursor.fetchone()
print row[0]
del cursor
del conn
'
The code runs successfully from standalone Anaconda, but, from a SQL query connected to the 2017 server, it does not:
Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 0
An external script error occurred:
Error in execution. Check the output for more information.
Traceback (most recent call last):
File "<string>", line 5, in <module>
File "C:\ProgramData\MSSQLSERVER\Temp-PY\Appcontainer1\41EF254D-1B14-4D9D-99AF-8AD356A84BDC\sqlindb_0.py", line 39, in transform
conn = pyodbc.connect(conn_string)
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [65]. (65) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (65)')
Msg 39019, Level 16, State 2, Line 0
An external script error occurred:
SqlSatelliteCall error: Error in execution. Check the output for more information.
STDOUT message(s) from external script:
Express Edition will continue to be enforced.
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 605, in rx_sql_satellite_call
rx_native_call("SqlSatelliteCall", params)
File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 375, in rx_native_call
ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
The account I'm using for development is in the sysadmin role on both servers. We use Windows authentication only on both servers. I assume that something significant is different about the instance of python that SQL/MLS uses than the standalone installation.
Thank you.
Using SQLAlchemy to connect to SQL Server on Azure.
I can access it via JDBC using SQL Workbench/J from same IP.(opened the IP in Azure)
Using the string:
mssql+pymssql://username:password#SQLservername.database.windows.net
Getting:
(pymssql.OperationalError) (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (nibisql1.database.windows.net:1433)\n') (Background on this error at: http://sqlalche.me/e/e3q8)
I tried all variations, but none of them work.
any idea why?
How do I pass parameters such as databasename?
Try to change
mssql+pymssql://username#SQLservername:password#SQLservername.database.windows.net
I am trying to use pymssql to connect to a SQL Server 2012 database running on Windows Server 2008.
db = pymssql.connect(host='xxx',user='xxx',password='xxx',database='xxx')
However, I am getting the following error:
OperationalError: (20017, 'DB-Lib error message 20017, severity 9:\nUnexpected EOF from the server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
The full error is:
Traceback (most recent call last):
File "XXX.py", line 2, in <module>
conn = pymssql.connect(host = 'XXX.com', user = 'xxx', password = 'xxx', database = 'xxx')
File "pymssql.pyx", line 556, in pymssql.connect (pymssql.c:7990)
raise OperationalError(e[0])
OperationalError: (20017, '\xa0\xff\xaf\x02\x90_\x8d\x02L\xd6\xa7\x02\xda\xad;DB-Lib error message 20017, severity 9:\nUnexpected EOF from the server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
Could someone help me figure out what's going on?
I would guess that it might be a total connection fail. I know it says 'EOF' but sometimes error messages like that are misleading.
I'd see if you can ping the host firstly from where you are running the script.
Secondly I'd see if you can get a tcp connection on the port you're using.
I've heard of EOF errors on http connections, when you connect with https to an http port. I'm not sure if that applies here.
This came up on google. It might help:
http://sourceforge.net/projects/pymssql/forums/forum/124111/topic/1672338
If all of the above is working, I'd double check the username and password are correct; perhaps even create a new user.
Finally, I'd check to see if the client library and the server library versions are compatible.
I hope this helps.
pymssql needs freetds to work properly. On macOS, open /usr/local/etc/freetds.conf in your preferred text editor, and update the following configuration:
[global]
# TDS protocol version
# tds version = auto
tds version = 7.0
You may try again and see whether the error is gone.
Reference here.
For testing purposes, I am attempting to setup a SQL Server database on my local machine and connect to it using adodbapi in Python. I used SQL Server Management Studio 2008 r2 to create a database and table. I enabled the SQL Server Browser service, but I am still unable to connect using adodbapi. Using this connections string:
adodbapi.connect(r'Provider=SQLOLEDB;Data Source=COMPUTERNAME.\SQLEXPRESS;Initial Catalog=Test;User ID=userName; Password=password;')
I get the following error:
Traceback (most recent call last): File "", line
1, in File
"C:\Python26\ArcGIS10.0\lib\site-packages\adodbapi\adodbapi.py", line
307, in connect
raise OperationalError(e, "Error opening connection: " + connection_string) OperationalError: (com_error(-2147352567,
'Exception occurred.', (0, u'Microsoft OLE DB Provider for SQL
Server', u'[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not
exist or access denied.', None, 0, -2147467259), None), 'Error opening
connection: Provider=SQLOLEDB;Data Source=COMPUTERNAME.\SQLEXPRESS;Initial
Catalog=Test;User ID=userName; Password=password;')
Any insight into what I'm missing?
I did a little more digging around and I ended up haphazardly stumbling on the answer. I was missing "Integrated Security=SSPI" in my connection string and it turns out I didn't need the dot before "\SQLEXPRESS" in my data source. Here's the connection string that worked for me:
adodbapi.connect(r'Provider=SQLOLEDB;Data Source=COMPUTERNAME\SQLEXPRESS;Initial Catalog=Test;User ID=COMPUTERNAME\USERNAME; Password=PASSWORD;Integrated Security=SSPI')