MySQL server has gone away

Posted June 11, 2017 37k views
MySQLApachePHPUbuntu 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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

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 @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

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.

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

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