MySQL server has gone away

June 11, 2017 140 views
MySQL PHP Apache Ubuntu 16.04

Hello! While running a PHP script I've encountered an error that says the MySQL server has gone away and it's really getting annoying.

I've tried the following things with no success.
Add these to /etc/mysql/my.cnf

wait_timeout = 28800
interactive_timeout = 28800
max_allowed_packet  = 256M

I've tried adding this to php.ini as well.

mysql.connect_timeout = 240

Nothing seems to work.

Thank you!

2 Answers

Hi @matthew6d

You shouldn't need to change anything in MySQL - it's by default set to some ridiculous high timeout.
But PHP has normally a timeout on 30 or 60 seconds for most things. Can you set the following two PHP configurations (300 seconds = 5 minutes):

mysql.connect_timeout = 300
default_socket_timeout = 300
  • Ohh, and you have to restart PHP for it to take effect - in your case, that would be Apache you need to restart, since PHP is probably running as mod_php.

  • This didn't work. I put it to 1000, and that didn't work either.

Create a file that contains the following and call it phpinfo.php and place it in your web root and open your browser to that page.

<?php phpinfo();

Then you will see all parameters used by PHP and you can search for each configuration to see what setting it's using.

Have another answer? Share your knowledge.