Home    Articles


Php - MySql error: "Mysql Server has gone away" best FIX.



The reason for this MySql error: "Mysql Server has gone away" is the fact that you are running a script that takes too long to be executed, for example 20 or 30 seconds...or even more than a minute.

So you have two ways in fixing this: 

1. Close and open a new DB connection

In my opinion this is the best practice and you should do it in this way:

Before executing a long time running script that doesn't involve MySql requests, close MySql connection. After the script has ended create a new DB connection and then you are able to use MySql without any issues.

2. Increase waiting time

Increaseing waiting time for MySql is not the best way to deal with this kind of issue...maybe becouse you often have no ideea how long the script will run.

These are the settings that should be increased in case you will choose this way:

wait_timeout, and max_allowed_packet 

Max Allowed Paket - this is a setting that allowes you to send a maximum packet size to MySql. I gues.. :D

ps: max_allowed_packet can be the reason for your issue, please investigate this too.

I hope it helped. ;)