MySQL Server Has Gone Away problem solved

I was just looking at RobsNotebook.com and came across a really good solution to a common problem with WordPress.

Lately, I have been getting a lot of error messages on my blog that said “MySQL server has gone away.” I looked through the Godaddy support forums hoping to find a solution. Unfortunately, I did not.

But then I found RobsNotebook.com and they had a page dedicated to this problem. It turns out that it is caused by the SQL connection being open for too long. Essentially, WordPress does not close the connection every time.

Godaddy’s (or insert your own host here) configuration of MySQL tells it to time out after a certain period, and that can happen in the middle of your call to the database.

RobsNotebook.com has provided a php file that you use in wordpress and just replace your existing file. He made it so easy that it takes only about 5 minutes to put in the solution.

P.S. I verified that the php page was correctly coded and had no “funny business” on it, but as the file could change at any time, it is always good to look through it for yourself whenever you download and use PHP from the net.

10 thoughts on “MySQL Server Has Gone Away problem solved

  1. At least you didn’t have my mySQL problem (that sounds strange!) I had a silly plugin that got spammed and then decided to query all the spams, 50k rows at a time! 🙁

    You can guess how my host reacted!

  2. yo,im also on godaddy
    and i must say that error (sever has gone) when running longer sql queries really pissed me off.
    though,the solution seems to be to mysql_close() the connection before it exceeds the time limit what actually probably causes godaddy service to close the connection itself.
    but what i just recently noticed is,
    check out Mysql Proccesslist on Godaddy MYSQL Servers,
    you will see the port changes,that could be one reason why it says “Server gone”.
    so each time you close the connection and estabilish a new one it catches the new port(which could have changed in an interval of max time).
    So there are 2 theories:

    1.Godaddy simply set a quite short maximum of connection time. : Workarround => mysql_close();
    2.Godaddy changes port in an unknown interval(would need some research to find it out). : Workarround => mysql_close();

    Maybe mysql_free_ressources also can help a bit,but i dont think so(just give it a try 🙂

    so the final solution seems to be to close the godaddy mysql connection as fast as possible.

    close,connect … probably wont cause your server to crash(not really similiar to DOS) so no worries!

  3. I have the same problem hosting on godaddy, though using druapl install. So I can’t use your total fix. But I’m hoping that I can get some tips from what you’ve done.

    Are you essentially saying that I need to call mysql_close after every database request that my site makes? Or do I need to call it explicitly when my page has finished generating?

  4. Imran, check out the comment by “Yo” on this post. I think his theory about random port changes is correct.

    I would say you could probably close the connection at the end of the page load. The most important thing is that you do not want to have an open connection through a port that has been changed randomly by GoDaddy. So if “Yo’s” theory is correct, you should be fine with closing the SQL connection at the end of a page load.

  5. Actually the problem with GoDaddy servers and most others having this problem is quite simple to fix if you have root access to the server. Clear everything out of etc/my.cnf and replace with

    [mysqld]
    set-variable = max_connections=500
    safe-show-database
    query-cache-type = 1
    query-cache-size = 20M

    Then restart sql.

    The “MySQL Server Has Gone Away” issue will… well… go away.

  6. Good point Sandcarving. If you have a dedicated VPS or Server, you can do just that. You can also adjust the timeout settings at that point.

    However, if you are on a shared host, the problem is probably not a failure of the MySQl server, it is just a timeout issue, of which you have no control in a shared environment.

Leave a Reply