KP Solutions

Solutions for Day to Day Technical Problems

SQL Error: 2006: MySQL server has gone away


So you got an error “SQL Error: 2006: MySQL server has gone away”. Like me if you were wondering where the heck did MySql go away. It was there when the script started.

Ok, I am gonna tell you that there’s a variable in your mysql configuration that is causing the MySql connection to timeout waiting idle. This could happen in the case, when you start the php script, make a mysql connection and then the script is doing some heavy processing, while the mysql connection made initially is sitting idle doing nothing. So in such cases, the MySql will check for the ‘wait_timeout’ variable setting and if the php script didn’t use the mysql connection for the ‘wait_timeout’ interval, the MySql will kill that connection and hence your script will get the ‘MySQL server has gone away’ error.

Solution to resolve this issue, two folds:

  1. If you make the MySQL connection, then you better use it. If you don’t want it then the resource needs to be released so others can use it.
  2. Else If you think that you need the connection and it is OK for it to sit idle for that time, then increase the value of ‘wait_timeout’ during runtime at the beginning of the script by issuing the SQL query ‘SET GLOBAL wait_timeout=60‘ in case you want to set it to 60 seconds and then restore it before you exit your script.

But, as always, if you increase the ‘wait_timeout’ value, check your server performance for another few days and see if there is any negative impact. If no impact then you are OK, but if there is then I would recommend Option 1.

Hope to help,


  • Ktbio11

    this solution not working… please help me out.. coz when i m uploading a image of 2.2mb size, it came out. please give me d proper solution

  • Anonymous

    As I mentioned, use OPTION 1 as that’s the best way to handle resources. If you use Option 2, you are going into speculation assuming that you know exactly how long the process would take. In your case, uploading 2.2MB size file would take 1 min on your connnection but on other user with modem connection, it could takeĀ  well over 10 minutes, so your fine tuning of the ‘wait_timeout’ value will not guarantee success.