Mysql server has gone away – Fix

The MySQL server has gone away error can occur when you have an established connection to the MySQL server and for one reason or another, the connection times out or otherwise goes down. In the context of a Linux system, the error can sometimes be seen from the MySQL client. In this tutorial, we will show several solutions for dealing with and troubleshooting the MySQL error.

In this tutorial you will learn:

  • How to resolve the Mysql server has gone away error on Linux
Mysql server has gone away - Fix
Mysql server has gone away – Fix
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Ubuntu Linux based system
Software MySQL server and client
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Mysql server has gone away – List of possible fixes




The MySQL server has gone away error is somewhat generic and could be caused from a variety of different issues. Therefore we will look at some of the most common culprits below, as well as the fixes which must be applied in order to keep the error from occuring again.

  1. The most common cause of this problem is that your connection has simply timed out. This will happen in your MySQL client if you have not sent any data to or from the server within a certain amount of time. The default time out is quite long – 8 hours. But the server you are dealing with could have a much shorter time out. To adjust the time out setting, edit the /etc/mysql/my.cnf file with the following lines.
    [mysqld]
    wait_timeout = 31536000
    interactive_timeout = 31536000
    

    Note that these numbers are in seconds. Adjust to your desired time out as necessary.

  2. Another cause of this error is that the server was terminated unexpectedly. This could be a sudden sysem shutdown or perhaps the MySQL server was exited ungracefully with the kill -9 command. In either case, you will receive the MySQL server has gone away error in your client. You must then investigate why the server is shutting down unexpectedly.
  3. The error will also occur if you are trying to execute a command on a server that has already timed out. In that case, you must first reestablish your connection to the MySQL server before trying to execute another command to retrieve or send data.
  4. If your MySQL client does not have permission to connect to the MySQL server, you may see this error pop up. Ensure that your username has permissions to connect from your host’s IP address, or from all IP addresses (wildcard).
  5. In some cases the error will be present when trying to send data that is too large. To prevent this, you must adjust the max packet size in the /etc/mysql/my.cnf file. By default, the max packet size is 64 MB. Adjust the following setting according to your needs.


    [mysqld]
    max_allowed_packet = 100M
    

    Note that in this example we have set our max packet size to 100 MB.

Closing Thoughts

In this tutorial, we saw how to resolve the MySQL server has gone away error on a Linux system. The most common issue to cause this error is that your connection has simply timed out, but unexpected termination of the MySQL server, or a large payload can also make you get this error on your MySQL client system.



Comments and Discussions
Linux Forum