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:
- 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.
- 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,