InterfaceError:2013:查询期间丢失与 MySQL 服务器的连接

2023-12-20

当我尝试连接到MySql数据库服务器位于Python on 谷歌云平台,我收到错误。以下是实际代码。

import mysql.connector
import pandas as pd

cnx = mysql.connector.connect(user = 'xxxxxx', password='xxxxxx',
                              host='xxx.xxx.xxx.xx',
                              database='xxxxxxxxx')
cursor = cnx.cursor()
cnx.close()
if cur and con:                        
    cur.close() 
    con.close()
sql1 = "SELECT * FROM ms_trackevaluation_15_16.ms_skill"
cursor.execute(sql1)
rows = cursor.fetchall()
df1 = pd.read_sql(sql1, cnx)

以下是错误:

InterfaceError                            Traceback (most recent call last)
<ipython-input-2-30094be976fb> in <module>()
      4 cnx = mysql.connector.connect(user = 'xxxxx', password='xxxxx',
      5                               host='173.194.104.33',
----> 6                               database='xxxxx')
      7 cursor = cnx.cursor()
      8 cnx.close()

    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/__init__.py in connect(*args, **kwargs)
        177         return CMySQLConnection(*args, **kwargs)
        178     else:
    --> 179         return MySQLConnection(*args, **kwargs)
        180 Connect = connect  # pylint: disable=C0103
        181 
    
    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in __init__(self, *args, **kwargs)
         93 
         94         if len(kwargs) > 0:
    ---> 95             self.connect(**kwargs)
         96 
         97     def _do_handshake(self):
    
    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/abstracts.py in connect(self, **kwargs)
        714 
        715         self.disconnect()
    --> 716         self._open_connection()
        717         self._post_connection()
        718 
    
    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in _open_connection(self)
        205         self._socket = self._get_connection()
        206         self._socket.open_connection()
    --> 207         self._do_handshake()
        208         self._do_auth(self._user, self._password,
        209                       self._database, self._client_flags, self._charset_id,
    
    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/connection.py in _do_handshake(self)
         97     def _do_handshake(self):
         98         """Get the handshake from the MySQL server"""
    ---> 99         packet = self._socket.recv()
        100         if packet[4] == 255:
        101             raise errors.get_exception(packet)
    
    /home/scrollstech_shravankumar/anaconda3/lib/python3.5/site-packages/mysql/connector/network.py in recv_plain(self)
        241                 chunk = self.sock.recv(4 - packet_len)
        242                 if not chunk:
    --> 243                     raise errors.InterfaceError(errno=2013)
        244                 packet += chunk
        245                 packet_len = len(packet)
    
    InterfaceError: 2013: Lost connection to MySQL server during query

谁能解释一下这段代码有什么问题以及为什么我会收到此错误。请解释我该如何解决这个问题?


From MySQL 文档 https://dev.mysql.com/doc/refman/8.0/en/error-lost-connection.html,这是该问题的原因。

通常这表明网络连接有问题,您应该检查 如果此错误频繁发生,请检查您的网络状况。如果 错误消息包括“查询期间”,这可能是您的情况 正在经历。

正如他们所说,解决方法是:

增加net_read_timeout从默认的 30 秒到 60 秒 秒或更长时间

如何在 python 中做到这一点:

如果您使用 SQLAlchemy,请将超时参数添加到连接引擎函数中,然后就可以开始使用了。

这是一个例子

from sqlalchemy import create_engine

engine = create_engine(self.writer_url, 
                       connect_args={'connect_timeout': 120},
                       pool_pre_ping=True)

The pool_pre_ping被抢自这个答案 https://stackoverflow.com/a/61870014/4683950 .

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

InterfaceError:2013:查询期间丢失与 MySQL 服务器的连接 的相关文章