'"Protocol error in TDS stream" and "Client unable to establish connection due to prelogin failure" while connecting to SQL Server with Python
Enviornment
At Server Side -- OS: Windows NT 6.3
- DB: Microsoft SQL Server Standard 2014
- Python: 3.7.3
- pyodbc: 4.0.27
- pymysql: 0.9.3
- OS: Windows 10
- driver: SQL Server Native Client 11.0 / ODBC Driver 13 for SQL Server
Issue
I am trying two different codes, which I know is correct, because they are running in a different system without any issue. The code is as follows:
Code 1:
import pymysql.cursors
conn = pymysql.connect(host='SomeRemoteServer',
user='UserIDForRemoteServer',
password='PassForRemoteServer',
db='DBNAME',
port=1433)
cursor = conn.cursor()
Error 1:
---------------------------------------------------------------------------
ConnectionResetError Traceback (most recent call last)
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in _read_bytes(self, num_bytes)
690 try:
--> 691 data = self._rfile.read(num_bytes)
692 break
c:\users\jains\appdata\local\programs\python\python37\lib\socket.py in readinto(self, b)
588 try:
--> 589 return self._sock.recv_into(b)
590 except timeout:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
OperationalError Traceback (most recent call last)
<ipython-input-3-396d851bab6d> in <module>
4 password='PassForRemoteServer',
5 db='DBNAME',
----> 6 port=1433)
7 cursor = conn.cursor()
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\__init__.py in Connect(*args, **kwargs)
92 """
93 from .connections import Connection
---> 94 return Connection(*args, **kwargs)
95
96 from . import connections as _orig_conn
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address, binary_prefix, program_name, server_public_key)
323 self._sock = None
324 else:
--> 325 self.connect()
326
327 def _create_ssl_ctx(self, sslp):
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in connect(self, sock)
596 self._next_seq_id = 0
597
--> 598 self._get_server_information()
599 self._request_authentication()
600
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in _get_server_information(self)
973 def _get_server_information(self):
974 i = 0
--> 975 packet = self._read_packet()
976 data = packet.get_all_data()
977
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in _read_packet(self, packet_type)
655 buff = b''
656 while True:
--> 657 packet_header = self._read_bytes(4)
658 #if DEBUG: dump_packet(packet_header)
659
c:\users\jains\appdata\local\programs\python\python37\lib\site-packages\pymysql\connections.py in _read_bytes(self, num_bytes)
697 raise err.OperationalError(
698 CR.CR_SERVER_LOST,
--> 699 "Lost connection to MySQL server during query (%s)" % (e,))
700 except BaseException:
701 # Don't convert unknown exception to MySQLError.
OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
Code 2:
import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=RemoteServerName;DATABASE=SomeDB;UID=UserNameToReadDB;PWD=PassForUser')
Error 2:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-2-cc9028930f12> in <module>
1 import pyodbc
----> 2 conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=RemoteServerName;DATABASE=SomeDB;UID=UserNameToReadDB;PWD=PassForUser')
Error: ('HY000', '[HY000] [Microsoft][SQL Server Native Client 11.0]Protocol error in TDS stream (0) (SQLDriverConnect); [HY000] [Microsoft][SQL Server Native Client 11.0]Client unable to establish connection due to prelogin failure (0)')
When I try to connect the same Server with same credentials with Microsoft SQL Server Management Studio 2017, it connects without any problem.
I have tried setting it up on ODBC Data Source Administrator, over there also it connected successfully. Initially same code was working on other laptop. Now I have changed the laptop, I am not sure what could be the issue...?
Please Suggest what I can do to access the Database.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
