XHanch Studio Log in | Register | Cart

Forum

[Troubleshoot] Word...
 
Notifications
Clear all

[Troubleshoot] WordPress database error: "MySQL server has gone away for query…"

1 Posts
1 Users
0 Likes
1,175 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

If you find lots of MySQL server has gone away for query… warnings in your error_log file, the following trick should solve your problem.

Why this errors appear

After digging around using Google, I eventually found this brilliant article at Rob’s Notebook called Workaround for WordPress database error: [MySQL server has gone away] for different versions of WordPress that provides the workaround for this error.

Rob goes on to explain why this error appears,

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.

Solution

Rob’s solution is to change the wait_timeout variable to 600 in the wp-db.php file which is located in the wp-includes directory. You just need to modify the __construct function.

Before modification:

function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
    register_shutdown_function( array( &$this, '__destruct' ) );

    if ( WP_DEBUG )
        $this->show_errors();

    $this->init_charset();

    $this->dbuser = $dbuser;
    $this->dbpassword = $dbpassword;
    $this->dbname = $dbname;
    $this->dbhost = $dbhost;

    $this->db_connect();
}

Aftermodification:

function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
    register_shutdown_function( array( &$this, '__destruct' ) );

    if ( WP_DEBUG )
        $this->show_errors();

    $this->init_charset();

    $this->dbuser = $dbuser;
    $this->dbpassword = $dbpassword;
    $this->dbname = $dbname;
    $this->dbhost = $dbhost;

    $this->db_connect();

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

Then, save this newly modified wp-db.php file and overwrite the original file.
Done!

Since adding this, the WordPress database error MySQL server has gone away for query… should no longer appears in your error_log.

As a note, you will need to do this each time you upgrade WordPress.

 
Posted : 28/09/2011 5:39 pm
Share:

× Close Menu