Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intentionally get a "MySQL server has gone away" error

Tags:

mysql

django

I'm trying to cope with MySQL's error MySQL server has gone away in a django env.

The quick workaround was to set the global wait_timeout MySQL variable to a huge value, but in the long run this would accumulate to many open connections.

I figured I'll get the wait_timeout variable and poll the server in smaller intervals. After implementing this I tried to test it but am failing to get the error.

I set global wait_timeout=15 and even set global interactive_timeout=15 but the connection refuses to disappear. I'm sure I'm polling the database in larger intervals than 15sec.

What could be the cause for not being able to recreate this error?

like image 590
Jonathan Livni Avatar asked Nov 24 '11 08:11

Jonathan Livni


1 Answers

Run below dirty-and-quick script and test your django project or whatever. I think this is not an elegant approach, but it works well.

<?php
mysql_connect( "127.0.0.1", "id", "pw" ); // should be changed to yours
while(1)
{
    $r = mysql_query( "SHOW PROCESSLIST" );
    while( $d = mysql_fetch_row( $r ) ) 
    {   
        if( $d[7] != "SHOW PROCESSLIST" )
        {   
            mysql_query( "KILL ". $d[0] );
            echo( $d[0]." was killed.\n" );
        }   
    }   
}
?>

And here is the result.

mysql> select * from foo;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    337
Current database: test

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    338
Current database: test

ERROR 2006 (HY000): MySQL server has gone away
mysql> 
like image 169
lqez Avatar answered Nov 15 '22 06:11

lqez