MySQL server has gone away

Category:support request
Related pages:#50: MySQL server has gone away

I have a php CLI script which takes more than 8 hours, which is longer than the default timeout for the mysql.

As a result, I have the following error by the end of my script:

MySQL server has gone away

I'm finding information about everywhere in the internet. I'll compile what I find in the wiki.



20.8.11. Controlling Automatic Reconnection Behavior

In MySQL 5.0, auto-reconnect was enabled by default until MySQL 5.0.3, and disabled by default thereafter. The MYSQL_OPT_RECONNECT option is available as of MySQL 5.0.13.

The problem is that the solution offered implies being able to change the code. I could do that but I'd need to patch Drupal core, which I'd rather not do.

I'm looking for a solution that I could set in my.cnf or in php.ini.


a php.ini setting is available with mysqli.
See #52: Mysql and Mysqli php extensions for server setting questions.


This may also help some people:
"So, one problem I was getting using mysqli OO was "Error: MySQL server has gone away". I fixed this by enabling the option in the PHP-CLI php.ini to mysqli.reconnect = On. I've also adjusted my my.cnf file to handle bigger datasets. So the file runs, but takes upto 53-54 seconds to run. I think this is due to a timeout and then reconnection. I can't see why PEAR::DB would be faster than OO mysqli."
I wonder if switching from mysqli to mysql in settings.php would help.


Apparently on Ubuntu, the mysqli extensions are installed together with php5-mysql.

So, in my case, I only needed to adapt the settings.php for my Drupal site to use the mysqli instead of mysql.

Also, I had to change the default php.ini:

; Allow or prevent reconnect
mysqli.reconnect = On


Status:active» fixed

The problem is fixed for me, and I put in the wiki the information I found.


Status:fixed» closed
Related pages:-50: MySQL server has gone away

Automatically closed -- issue fixed for 2 weeks with no activity.