Error: MySQL Server Has Gone Away / Linux / Error: MySQL Server Has Gone Away

Let’s see how to fix the “MySQL server has gone away” error that appears when accessing MySQL Server.

The most common causes of the MySQL server has gone away error are:

  • Packet size too large in MySQL query (default maximum packet size is 16 MB);
  • You ran out of free RAM on the MySQL host (you can check the free memory in Linux using the free –h command;
  • The inactive connection between your application/script and MySQL (by default, the session disconnects after 8 hours).

MySQL server has gone away - error 2006

General error: 2006 MySQL server has gone away
Error Code: 2013. Lost connection to MySQL server during query
PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

To increase the timeout for MySQL connections, add the following options to the myscnf configuration file:

sudo nano /etc/mysql/my.cnf

Find the [mysqld] section and increase the timeout to 24 hours:

wait_timeout = 86400
interactive_timeout = 86400

If you upload large files or BLOBs larger than 16 MB to MySQL, MySQL will return MySQL server has gone away error. You need to increase the maximum packet size in my.cnf.

To do this, in the [mysqld] section, increase the value of the max_allowed_packet parameter from the standard 16M to 128MB:

max_allowed_packet = 128MB

After making changes to the mysqld.cnf file, you need to restart the MySQL service:

  • In CentOS/RHEL/Fedora distros:
    sudo systemctl restart mysqld
  • In Debian/Ubuntu:
    sudo systemctl restart mysql.service

If you are connecting to MySQL from PHP, check that the timeout values in php.ini are greater than in MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *