Lost connection to MySQL server during query - Pyt

2019-08-26 18:34发布

I'm trying to connect to a server to fetch details from one of the databases using pymysql library in python. But I keep getting this same error and I've no clue on why. I'm pretty new to Python and hoping someone could help me here.

I tried checking the post here but not very helpful.

Here is my code.

import pymysql
print("Connecting mysql")
connection = pymysql.connect(host=A.B.C.D,user='admin',password='admin',db='MY_DB',port=ABCD)

and here is the error I get

       Connecting mysql
   Traceback (most recent call last):
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 690, in _read_bytes
    data = self._rfile.read(num_bytes)
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\socket.py", line 571, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/anon/PycharmProjects/labelFetch/getlabel.py", line 3, in <module>
    connection = pymysql.connect(host='172.16.137.50',user='qaadmin',password='qaadmin',db='QA_INTRANET',port=1433)
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 327, in __init__
    self.connect()
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 597, in connect
    self._get_server_information()
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 966, in _get_server_information
    packet = self._read_packet()
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 656, in _read_packet
    packet_header = self._read_bytes(4)
  File "C:\Users\anon\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\connections.py", line 698, in _read_bytes
    "Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')

1条回答
冷血范
2楼-- · 2019-08-26 18:56

You may want to try using pymssql instead of pymysql.

I had the same errors but the reason in my case was that my Database was not a MySQL but a Microsoft SQL database. Using the adequate library (pymssql) then solved the problem.

查看更多
登录 后发表回答