Like almost everyone, we use shared hosting servers for our websites and most of our clients’ websites aswell. This has advantages (monetary/outsourcing) and disadvantages (no custom configurations, sometimes performance). An error which pops up quite a lot recently, at least around here, is the famous
database error: [MySQL server has gone away]
This is a usually temporary performance problem of the MySQL server (temporary connection loss or too much load, causing delays and finally, a time-out to your queries) and something you cannot fix yourself on a shared hosting, because you usually cannot edit the MySQL servers configuration to increase the timeout manually. For most of us, writing mails to the hosting company that they should upgrade their servers will probably be useless, so “the internet” has come up with a a (not very beautiful) fix: Insert an SQL query at init-time. Most more or less advanced PHP apps are shipped with a database abstraction layer, usually an OOP class somewhere hidden deep down in the include dirs. This is where you can add this query (using the provided functions) to define a new timeout for every query after initialisation time:
set session wait_timeout=600
While this might probably reduce the responsiveness of the server, it reduces failed queries due to timeouts. One possible reason why this is happening on shared hostings is the widespread usage of huge apps like the Menalto Gallery or Web CMS apps like Drupal, WordPress, Joomla, et al. With every page hit, they create hundreds of queries and almost everyone with a shared hosting has at least 2 of those apps installed. Now if the hosting environment has like 1 central MySQL cluster which is a little bit underpowered during peak hours, you might end up with an error log full of the above error messages.
Dear shared hosting providers: I understand you cannot provide lightning fast hosting for ultracheap prices. But it would slow down the filling of error logs aswell by just configuring the server to use a larger timeframe for sending queries back and forth. This would be more realistic than to expect the community to optimise PHP/SQL code to gain milliseconds. :P
I came across this on Rob’s notebook. He has a modded version of the WordPress DB layer over there to download if you don’t feel like editing yourself. Also, keep in mind that this function might be disabled for your hosting environment. Handle with care, test, retest and make yourself ready to quickly comment the line of code if your site dies. :-)