MySQL error 2006: mysql server has gone away

2018-12-31 09:28发布

I'm running a server at my office to process some files and report the results to a remote MySQL server.

The files processing takes some time and the process dies halfway through with the following error:

2006, MySQL server has gone away

I've heard about the MySQL setting, wait_timeout, but do I need to change that on the server at my office or the remote MySQL server?

23条回答
忆尘夕之涩
2楼-- · 2018-12-31 10:25

This might be a problem of your .sql file size.

If you are using xampp. Go to the xampp control panel -> Click MySql config -> Open my.ini.

Increase the packet size.

max_allowed_packet = 2M -> 10M
查看更多
梦寄多情
3楼-- · 2018-12-31 10:26

If you know you're going offline for a while, you can close your connection, do your processing, reconnect and write your reports.

查看更多
姐姐魅力值爆表
4楼-- · 2018-12-31 10:28

The unlikely scenario is you have a firewall between the client and the server that forces TCP reset into the connection.

I had that issue, and I found our corporate F5 firewall was configured to terminate inactive sessions that is idle for more than 5 mins.

Once again, this is the unlikely scenario.

查看更多
ら面具成の殇う
5楼-- · 2018-12-31 10:31

This generally indicates MySQL server connectivity issues or timeouts. Can generally be solved by changing wait_timeout and max_allowed_packet in my.cnf or similar.

I would suggest these values:

wait_timeout = 28800

max_allowed_packet = 8M

查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 10:31

This error happens basically for two reasons.

  1. You have a too low RAM.
  2. The database connection is closed when you try to connect.

You can try this code below.

# Simplification to execute an SQL string of getting a data from the database
def get(self, sql_string, sql_vars=(), debug_sql=0):
    try:            
        self.cursor.execute(sql_string, sql_vars)
        return self.cursor.fetchall()
    except (AttributeError, MySQLdb.OperationalError):
        self.__init__()
        self.cursor.execute(sql_string, sql_vars)
        return self.cursor.fetchall()

It mitigates the error whatever the reason behind it, especially for the second reason.

If it's caused by low RAM, you either have to raise database connection efficiency from the code, from the database configuration, or simply raise the RAM.

查看更多
登录 后发表回答