Search Our Database

How to fix the error “Mysql Server has gone away”

Last updated on |

Introduction

The “MySQL Server Has Gone Away” error occurs when a MySQL connection unexpectedly terminates, often due to incorrect timeout settings, packet size issues, or resource limitations. It is important to address this error to maintain the stability and performance of database-driven applications. This guide explains how to fix the “MySQL Server Has Gone Away” error, helping administrators troubleshoot and resolve common causes such as timeouts and large queries in MySQL environments.

 

Prerequisite

  • SSH access to the server with root privileges
  • Basic knowledge of MySQL configuration files and commands

 

Step-by-step guide

Step 1: Access to your server via SSH

  • Access to your server and gain root privileges by command below.
    sudo su -

 

Step 2: Verify server storage usage

  • One of the common causes that you are getting this error was because your server was running out of space.
  • To verify, you may use df -h command and look for the line that is end with /.

 

Step 3: Locate and navigate to MySQL configuration file

  • If your server isn’t running out of space, you might need to adjust your MySQL settings for better performance.
  • Locate your MySQL configuration file (my.cnf) path.
    It can be either:

    cd /etc/

    or

    cd /etc/mysql/

 

Step 4: Verify the content of MySQL configuration file

  • Once you have navigated to the directories, you may check the MySQL configuration file by running the following command
    cat my.cnf
  • Verify the size of current max_allowed_packet was configured, you should see a line similar as below.

    max_allowed_packet=64M

    wait_timeout = 28800

    innodb_log_file_size = 128MB

    Or you may see line below as the configuration was stored in different location.
    !includedir /etc/my.cnf.d

    Navigate to the file and you will see the similar line will appear in the configuration file. For example:

Step 5: Edit MySQL configuration file

  • If you didn’t see either of the lines, you may enable the log by running the command below.
    vi my.cnf

    Then press i to enable edit mode and add the line below under [mysqld].

    max_allowed_packet= <value>M
    
    wait_timeout = <vaule>
    
    innodb_log_file_size = <value>MB

    Press ESC to escape edit mode and enter :wq to save.

  • Restart your MySQL service to apply the changes.
    service mysqld restart

 

Conclusion

By going through this guidance, you will be able to troubleshoot and resolve the “MySQL Server Has Gone Away” error, ensuring a more stable and reliable MySQL database connection.

 

For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.