PHP, MySQL and error “2006: MySQL server has gone away”

As a hobby I like to do little photography.

Same time I do not like to give my photos to someone else to host, so I have my gallery.

It runs on Menalto’s Gallery 3 engine – nice, open source, PHP based with MySQL back-end.

Not my primary programming language, but allows me to host and enhance my blog, keep my gallery and small web-site.

Last night, while bringing some old photos over from my backup gallery (G3 is still under development and I do not want to recreate gallery if anything to happen, and it did – I killed the setup…), I noticed that import process start to fail. Several attempts to recover did not give much of the progress and I went digging error logs.

Well,.. I learned something interesting. I am not a PHP programmer, at least it is not that I do for living, or not at this time… but even doing simple house work forces you learn things 🙂
G3 runs on Kohana framework and there is a wrapper for DB access.

What happend was that some of the SQL requests began timing out

Database_Exception [ 44 ]: #2006: MySQL server has gone away

In case when queries are not optimal, one has to learn how to manage PHP… through php.ini…

MySQL timeout related errors are “fixed” by telling mysql to reconnect… and for that “tricks” are handy…
From browsing, reading, browsing, filtering, … while waiting for G2 Import I have found a solution which lead me here. One of interest is called mysqli.reconnect. Little magic – simply adding the following line in PHP.INI would tell MySQL reconnect.

mysqli.reconnect=1

It may be still slow, but at least connection would recover gracefully.

We can talk about code styles, code protection and error handling, but line above is a hidden gold on the end of the rainbow called PHP.

This entry was posted in Web-design and tagged , , , , , , . Bookmark the permalink.

Leave a Reply