故意得到“MySQL服务器已经消失”的错误

时间:2011-11-24 08:54:29

标签: mysql django

我正在尝试在django env中处理MySQL的错误MySQL server has gone away

快速解决方法是将全局wait_timeout MySQL variable设置为一个巨大的值,但从长远来看,这将累积到许多开放连接。

我想我会得到wait_timeout变量并以较小的间隔轮询服务器。执行此操作后,我尝试测试它但未能得到错误。

set global wait_timeout=15甚至set global interactive_timeout=15但连接拒绝消失。我确定我在15秒内以更大的间隔轮询数据库。

无法重新创建此错误的原因是什么?

2 个答案:

答案 0 :(得分:4)

运行以下脏和快速脚本并测试您的django项目或其他任何内容。我认为这不是一种优雅的方法,但效果很好。

<?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" );
        }   
    }   
}
?>

这是结果。

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> 

答案 1 :(得分:0)

我对与MySQLi一起使用的@lqez代码进行了一些修改:

<?php
$mysql = new mysqli("127.0.0.1", "user", "password", "database"); //should be changed to yours

while (1) {
    $r = $mysql->query("SHOW PROCESSLIST");
    while ($d = $r->fetch_row()) {
        if ($d[7] != "SHOW PROCESSLIST") {
            $mysql->query("KILL " . $d[0]);
            echo($d[0] . " was killed.\n");
        }
    }
}
?>