Workaround for WordPress database error: [MySQL server has gone away] for different versions of Wordpress

Rob July 16th, 2007

Updated on July 18, 2008 - TESTED and WORKING update for Wordpress 2.6.

For those of you who have received the Wordpress error message “WordPress database error: [MySQL server has gone away]”, I made a change to a Wordpress PHP file that makes it so you will probably never get this error again.

There are several possible causes to getting this error. Further down in this article, I will describe what I think is happening and some things that you can do (other than this workaround) to diagnose and fix the problem.

Installing the Workaround

There are 2 workarounds, you can use either one of them, although one of them has better performance than the other.

  1. wp-db-x.y.z-workaround.phptxt
    • The latest version of the workaround with the best performance. This latest workaround is available for all versions of Wordpress that are listed in this article.
  2. wp-db-x.y.z-workaround-old.phptxt
    • The original version of the workaround with less than optimal performance. Since I am no longer maintaining that version, I only have it posted for the versions of Wordpress that I had originally posted it for. This workaround is based on the work of openadvent.

To install the workaround, download the “workaround” or “workaround-old” version of the wp-db…phptxt file, rename it to wp-db.php, and copy it to wp-includes/wp-db.php (overwriting the old file). You may want to backup the old file instead of overwriting it. You’ll need to do this each time you upgrade Wordpress (a pain, I know), with the appropriate version of wp-db.php for that version of Wordpress.

Wordpress 2.6

Workaround wp-db.php
Original wp-db.php

Wordpress 2.5

Workaround wp-db.php
Original wp-db.php

Wordpress 2.3.3

There was no change in wp-db.php from 2.3.2 to 2.3.3, so use the 2.3.2 file.

Wordpress 2.3.2

Workaround wp-db.php
Workaround-old wp-db.php
Original wp-db.php

Wordpress 2.3.1

There was no change in wp-db.php from 2.3 to 2.3.1, so use the 2.3 file.

Wordpress 2.3

Workaround wp-db.php
Workaround-old wp-db.php
Original wp-db.php

Wordpress 2.2.3

There was no change in wp-db.php from 2.2.1 to 2.2.3, so use the 2.2.1 file.

Wordpress 2.2.2

There was no change in wp-db.php from 2.2.1 to 2.2.2, so use the 2.2.1 file.

Wordpress 2.2.1

Workaround wp-db.php
Workaround-old wp-db.php
Original wp-db.php

Wordpress 2.1.2

Workaround wp-db.php
Workaround-old wp-db.php
Original wp-db.php

Why This Happens

When someone tries to view one of your Wordpress web pages, the PHP code first opens a connection to the MySQL server. Subsequent queries to the MySQL server rely on the connection remaining open, and do not re-open the connection. If the connection to the server happened to have shut down before a query is made, then Wordpress will display this error.

One reason that the connection may shut down is if it has been idle for “wait_timeout” seconds since the last access to the connection. “wait_timeout” is a system variable in your MySQL server configuration. I have created a tool to allow you to check the value of “wait_timeout”.

Tool for checking “wait_timeout”

Changing the “wait_timeout”

If your “wait_timeout” variable is small, and your CPU is heavily loaded, it may make it more likely that your connection to the MySQL server is shut down prematurely.

If your “wait_timeout” is too small, and you have admin access to your MySQL server, it is possible to change the default value of “wait_timeout” by editing a file such as /etc/my.cnf, and restarting your MySQL server.

If you don’t have admin access (you are on a shared host), there is a way to change the “wait_timeout” variable for the MySQL session, but you need to modify the wp-db.php file (either the workaround version or the original version).

To change the “wait_timeout” to 600 in the wp-db-x.y.z-workaround.phptxt, uncomment (remove the “//”) the line:

// $this->query("set session wait_timeout=600");

You can also change the wait_timeout in the original wp-db.php file (if you choose not to use the workaround) by adding the above query line for changing the wait_timeout variable into the wp-db.php source. The place to insert the line is inside the “__construct(…)” function after the closing brace ‘}’ of the “if (!$this->dbh) {” statement.

What the Workaround Does

The original workaround posted in this article (labeled “workaround-old” now) was originally conceived by openadvent. In that workaround, PHP code was added that would try to “ping” the mysql server before each query. If this ping was unsuccessful, then the workaround would try to reopen a connection to the MySQL server and then repeat the query. The connection would try to be reopened and queried 4 times before declaring a failure.

The newer workaround that I have on here now does not do this ping, giving better performance. Instead, it just tries to perform the query. If the query is successful, the PHP code continues on, and no new connection to the MySQL server is opened. If the query fails, the PHP code will try to reopen the connection and do another query. Just like the old workaround, the connection would try to be reopened and queried 4 times before declaring a failure.

Another Possible Fix

One person posted in the comments that by switching from PHP 4 to PHP 5, he was able to get rid of this error. Perhaps his PHP 4 implementation was running just slow enough that it was exceeding the timeout, and PHP 5 was running fast enough to avoid the timeout. When I originally encountered this error, I was using PHP 5, so switching from PHP 4 to PHP 5 may not solve the problem.

See Also:

Open Advent Blog with fix for WP 2.1.2

Wordpress Support Site

Vbulletin Forum

67 Comments »

  1. Cmbon 05 Aug 2007 at 4:26 am

    Hey, Rob,
    Thank you! It has fixed the error in my blog.

    Weki.cn

  2. Kevin Makiceon 07 Aug 2007 at 4:30 pm

    I think it worked for me, too. The admin site is still painfully slow, but I can get back into it for the first time in six hours.

  3. Robon 11 Aug 2007 at 12:28 pm

    I checked out Wordpress version 2.2.2, and there is no change in wp-db.php from 2.2.1 to 2.2.2, so I added a note to use the 2.2.1 file for 2.2.2.

  4. Neriakon 22 Aug 2007 at 7:45 am

    Heya Rob, thanks a ton for that fix. You really saved me from going through a lot of trouble! :)

  5. kenon 04 Oct 2007 at 7:13 am

    hey, how about Wordpress 2.3?
    TQ

  6. Robon 05 Oct 2007 at 12:19 am

    Fix for Wordpress 2.3 was just added.

  7. kenon 05 Oct 2007 at 10:49 am

    Thanks Rob! great fix for WP 2.3
    & it seems fix for WP 2.2.1 also works fine for WP 2.3
    Great job!

  8. Robon 05 Oct 2007 at 11:06 am

    I wouldn’t recommend using the WP 2.2.1 fix for WP 2.3, since when I did the update, I noticed that they added some things into wp-db.php for WP 2.3.

  9. bakkouzon 08 Oct 2007 at 8:35 pm

    heya Rob,
    Thank you for the fix, i just applied it and it works just fine.
    the strange thing in my case though that the error wasn’t sitewide, but was appearing on a single post only, my most recent post, and only with my comments and not other people’s, any idea why? :)

  10. Jay Ton 17 Oct 2007 at 3:10 pm

    Rob - THANKS! for this. Worked like a charm on my 2.3 blog. You da man!

  11. […] file in version 2.1. Because I recently upgraded to WP 2.3, I applied the file fix again. It is at this site if anyone else needs […]

  12. Willon 25 Oct 2007 at 6:23 pm

    Thanks! I had applied the fix for version 2.1 and forgotten about it since it worked so well. Now that I am on Version 2.3 I have been getting some timeouts and the “Lost Connection to the MySQL Database” error, followed, of course, with the “gone away” error.
    Hopefully I have fixed it by downloading your file to replace the one that came with 2.3.

    Really appreciate your effort on this and have posted and linked to your fix.

    -Will

  13. kentoon 28 Oct 2007 at 11:40 am

    Thanks mate! I’m using WP 2.3.1

  14. […] Investigando un poco he encontrado la solución. […]

  15. Robon 30 Oct 2007 at 9:12 am

    Please don’t link to the fixed php files directly on your blog, since I do release new versions when there are new releases of Wordpress (and I would also like to get the ad traffic). If you do want to link, use the one to this page: http://robsnotebook.com/wordpress-mysql-gone-away. Thank you.

  16. […] Download the fix here […]

  17. Jan Dembowskion 31 Oct 2007 at 7:16 am

    Much thanks! I diffed the 2.3 v 2.3.1 wp-db.php file and except for a trailing line feed there was no difference. Your fix worked so far :)

  18. Kanokon 08 Nov 2007 at 8:14 am

    The Flick photo page on my blog gone error when I upgrade the plugin to v0.93.1 which use database to keep the image cache in the DB. Your mod fixes the problem..

    Thanks a lot
    Kanok

  19. […] Yes, this error happened to me too. It seems there are plenty of hosters who just don’t bother configuring and tuning mySQL and this is definitely not quite a WordPress issue (at least I can’t believe it runs heavy queries when adding a new post), but there is a workaround Problem explanation and download link […]

  20. […] pingback […]

  21. More Blog Moneyon 02 Dec 2007 at 3:54 am

    I used the 2.1.2 file for WP2.1.3 and it’s working so far.

    Thanks!
    mbm

  22. […] Origional complete article link with more details […]

  23. WordPress Mysql Has Goneon 11 Dec 2007 at 1:41 pm

    […] information get from : fix for wordpress database error […]

  24. shadowX_19on 11 Dec 2007 at 11:42 pm

    thanks 4 the fix.. now my blog can run smoothly back..

  25. […] created to the public for this issue. You may try visiting the following link for the resolution. http://robsnotebook.com/wordpress-mysql-gone-away Kindly let us know if the above link has solved your issue after you have replaced your […]

  26. Patrick Wongon 18 Dec 2007 at 11:04 pm

    Thanks. I have been pulling my hair when this error shows up. I even phoned my hosting company but they say it’s not their end. Anyway, I took your suggestion and see if this fixes the problem!

  27. […] Fix for WordPress database error: [MySQL server has gone away] for different versions of Wordpress - MySQL server has gone away wordpress error […]

  28. ECon 28 Dec 2007 at 7:21 am

    I got exactly the same problem, but I have been running a very old version of WordPress - 2.1. I copied the 2.1.2 fix and replaced my wp-db.php with it, but i get an error message “Fatal error: Call to undefined function: hide_errors() in /home/lasermk/public_html/wp-includes/functions.php on line 913″

    Any idea how to get round this? Many thanks in advance.

  29. Robon 03 Jan 2008 at 7:32 pm

    It seems that wp-db.php did change from WP version 2.3.1 to 2.3.2. I have posted the fixed 2.3.2 file in the article.

  30. […] ???????????????? Fix for WordPress database error: [MySQL server has gone away] for different versions of Wordpress […]

  31. […] created to the public for this issue. You may try visiting the following link for the resolution. http://robsnotebook.com/wordpress-mysql-gone-away Kindly let us know if the above link has solved your issue after you have replaced your […]

  32. […] I upgraded to WordPress 2.3.2, I have noticed a really strange error.  I am trying a fix found here, and this post is partly a test to see if it […]

  33. bayuon 01 Feb 2008 at 1:14 am

    Thanks mate… you’re my savior

  34. Erickon 19 Feb 2008 at 10:37 pm

    Hi. Please CC me if you respond to this (you don’t have comment subscription).

    I did this. And it works — thanks for this hack!

    But I would additionally like to use “mysqli_” instead of “mysql_” functions for better MySQL 5 performance. Any way we can change that in just one file and be done with it, or are queries scattered all over?

  35. Martin Cleaveron 24 Feb 2008 at 8:10 pm

    http://trac.mu.wordpress.org/ticket/376 says:

    07/24/07 16:07:35 changed by donncha

    “Check your MySQL wait_timeout variable. What is it set to. It’s likely that it’s way too low. Try setting it to 600 or higher. The solution offered by the link above suggests not reusing an existing connection but that doesn’t make sense and would severely reduce performance on your server.”

  36. Robon 24 Feb 2008 at 9:23 pm

    Thanks Martin for that tip. Based on your suggestion, I just tried to find a way to find out what my MySQL wait_timeout variable is, but I haven’t figured it out yet. I’m sure if I kept looking, I’ll find it, but if you know it and can say how to do that, it would be great.

  37. Martin Cleaveron 24 Feb 2008 at 10:04 pm

    I altered the variable to value 600 in /etc/my.cnf - but you may be unable to change this on machines where you don’t have root access.

    http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#option_mysqld_wait_timeout has the description for this variable. You may find this useful in discovering how to set the value interactively.

  38. Robon 24 Feb 2008 at 11:40 pm

    Thanks Martin. I don’t have root access, but I think I found a way to discover the value of my wait_timeout variable. I sshed into the server and executed “mysqladmin -u username -p variables”. It shows that the wait_timeout variable is 60. Do you think that this is too small?

  39. Robon 24 Feb 2008 at 11:43 pm

    I’ve also been meaning to change this “fix” (Hack really is the appropriate word). I originally just used openadvent’s idea to ping the connection to see if it is still open (and only reopening the connection if the ping is unsuccessful). I think a better way would be to just perform the query, and only open a new connection if the query fails. This way, the only penalty in performance is when the query fails. If the query is successful, there is no penalty. Someday soon I will make this change and do another post with it.

  40. […] was just looking at RobsNotebook.com and came across a really good solution to a common problem with […]

  41. skarldon 13 Mar 2008 at 8:26 am

    In a recent upgrade, 1and1 hosting has broken my site. This looks like the fix. However, I am at version 2.0.11. Has this been tested at that level, or will I need to upgrade to a later release of WP? Are there plans for 2.5?

  42. Robon 13 Mar 2008 at 1:33 pm

    skarld: The earliest version that I have done this for is 2.1.2 (I never tested any earlier version). One option for you if you do not want to upgrade is to make the changes yourself in your version of the wp-db.php file. You can look at what I have here as the original for 2.1.2 and the fix, and you can insert the changes yourself. I’m a little busy these days, otherwise I would look into making the change for you myself.

  43. skarldon 13 Mar 2008 at 11:39 pm

    Thanks for the suggestion. It did not work.
    My host suggested PHP5. Stating the following:

    Try switching to PHP5. Scripts parsed in PHP4
    use more memory than PHP5 in our systems. You can switch to PHP5 by
    adding line below in your .htaccess file:

    AddType x-mapp-php5 .php

    It worked.

  44. Robon 14 Mar 2008 at 12:02 pm

    When I first got this error, I was using PHP5. I think it is a very sporadic error and only really occurs when your server is highly loaded (especially shared hosts). I would say that the error maybe only occurs 1% of the time, but when the condition that causes the error occurs, the workaround in this script will try to reconnect with the MySQL server.

  45. » It’s Resolved!! » naziebiyahon 15 Mar 2008 at 11:30 am

    […] server has gone away]. Dan, oh thanks God for creating the creator of google, saya pun menemukan ini. And it shows the problem and the solution too.. Dan akhirnya bisa!! […]

  46. […] goodness the solution is not very hard. My hastyhost found this solution and helped me with the upload the first time round. Today, I had to do it myself, using WIFI in a […]

  47. Robon 26 Mar 2008 at 10:00 pm

    I rewrote the article and added a new workaround that does not do the ping - increasing the performance. The article also gives more information for diagnosing the problem, such as looking up the value of the “wait_timeout” MySQL variable, and how to change this variable.

  48. Robon 01 Apr 2008 at 8:25 am

    I added the workaround for Wordpress 2.5.

  49. […] must be something in WordPress itself that causes this error and I found a workaround for it here: Workaround for WordPress database error: [MySQL server has gone away] for different versions of Word… I did what it said to do and now my blog is back! *whew* __________________ Army Mom: New […]

  50. […] if you ever get an error like that then you need to get the workaround from Rob’s Notebook. It saved my sanity… it will save yours. It was very, very easy to […]

  51. Charlotteon 04 Apr 2008 at 8:21 pm

    Rob.. thank you so much for this. I was about to pull my hair out. This saved my hair, sanity and my blog!

  52. WordPress????????? | ????on 10 Apr 2008 at 10:26 pm

    […] UPDATE 2008.04.11????????????????wp-db.php????????????????????????????????????????????????? […]

  53. Surpasshosting ????(2) - Shawn's Blogon 20 Apr 2008 at 4:13 am

    […] WP-DB ???????? MySQL ??? Ping MySQL ?????????? error-log […]

  54. ?ukasz Sobekon 26 Apr 2008 at 3:50 pm

    The db file of 2.5.1 is identical to that of 2.5

  55. LaurenMarie - Creative Curioon 02 May 2008 at 9:39 am

    Hi Rob, it looks like this could help me, but uploading the file you provided for 2.5 isn’t working for me. I would like to try the modification you listed for the wait_timeout variable, but I can’t find that line in order to uncomment it. Which line number should it be on?

    If you have time, maybe you can check out my problem and solutions I’ve tried.

  56. Widgett Wallson 16 May 2008 at 2:14 pm

    Echoing LaurenMarie’s comment–couldn’t find the line either for the 2.5 flavored file.

  57. Robon 16 May 2008 at 2:15 pm

    Yes, it is missing from the 2.5 file. I will add it. In the meantime, you can look at one of the other versions as it is there in the other versions, and put it in the same place in the 2.5 version.

  58. QuicksWebon 18 May 2008 at 6:29 pm

    I wonder why if this works and default wordpress does not, why did they not adopt this into their original releases.

    It’s been going on a long time now.

  59. Robon 18 May 2008 at 9:58 pm

    I just updated the 2.5 version so that the commented line that allows you to change the wait_timeout is now there.

  60. Simonon 20 Jun 2008 at 3:54 am

    Thanks so much, I was getting another error,
    “Fatal error:Call to undefined method:wpdb->set_prefix()”

    and this sorted it, much appreciated, Simon

  61. rahulon 26 Jun 2008 at 9:37 pm

    my site database give problem any one know how to fix this plz help me site is http://www.znvj.com

  62. Kerryon 29 Jun 2008 at 1:48 pm

    Dude, you are a lifesaver! Thanks so much!

  63. Joseon 18 Jul 2008 at 8:21 am

    I’ve tested the 2.6 fix and it says it can’t connect to the database :?

  64. Joseon 18 Jul 2008 at 8:54 am

    Sorry, the problem is that it can’t SELECT the database :oops:

  65. Robon 18 Jul 2008 at 10:10 am

    OK, I’ll see if I have some time to test 2.6 this weekend and see what the problem is.

  66. Dana Huffon 18 Jul 2008 at 5:14 pm

    Just FYI, I tested and had the same problem as Jose.

  67. Robon 18 Jul 2008 at 10:05 pm

    I found the problem with my untested version for 2.6 and I fixed it, tested it, and the 2.6 version on this site is now updated with the fix. Please let me know if you have a problem.

RSS feed for comments on this post. TrackBack URI

Leave a comment

If you want to leave a feedback to this post or to some other user´s comment, simply fill out the form below.

(required)

(required)