MySQL server has gone away

June 11, 2017 25.5k views
MySQL PHP Ubuntu 16.04 Apache

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!

4 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.

where put this
mysql.connecttimeout = 300
socket_timeout = 300
lines ?

  • My MySQL configuration file is /etc/my.cnf
    I would put it somewhere below the line: [mysqld]
    and restart mysql service (service mysql restart). If it do not restart, restore the my.cnf as it was before.

Hi, what about if the error is in this line?

$link->ssl_set(‘client-key.pem’, 'client-cert.pem’, 'ca.pem’, NULL, NULL);

Have another answer? Share your knowledge.