php,mysql服务器已经消失了

时间:2011-09-02 11:24:31

标签: php mysql

有什么问题,当你在QUERY之前连接到LINE上的数据库时,你仍然得到“MySQL服务器已经消失了”?

检查此示例代码:

mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sql = mysql_query("SELECT id FROM db");
while ($data = mysql_fetch_assoc($sql)) {
$ids[] = $data[id];
}

foreach ($ids as $id) {
$content = file_get_contents("http://www.id.com/?id=$id");
if (stristr($content, "the id is ok man")) {
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
mysql_query("UPDATE db SET status = 'OK' WHERE id = '$id'");
}
}

mysql服务器消失了,我在foreach循环中得到了它。 BTW我需要在foreachloop中连接,因为它可能需要一段时间才能找到更新内容(比如1-2分钟),然后肯定会让mysql服务器消失了。

2 个答案:

答案 0 :(得分:7)

我认为您的问题是脚本可能会在发送第一个UPDATE查询之前执行很长时间。

您应该检查my.cnf中的wait_timeout值。您可以通过运行查询“SHOW VARIABLES;”

来检查您的wait_timeout

您还可以尝试使用一段代码重新连接:

if (!mysql_ping ($conn)) {
   //here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
   mysql_close($conn);
   $conn = mysql_connect('localhost','user','pass');
   mysql_select_db('db',$conn);
}

结合您的代码,它将是:

mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sql = mysql_query("SELECT id FROM db");
while ($data = mysql_fetch_assoc($sql)) {
    if (!mysql_ping ()) {
       //here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
       mysql_close();
       mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
       mysql_select_db("xxx") or die(mysql_error());
    }

    $ids[] = $data['id'];
    $content = file_get_contents("http://www.id.com/?id=$id");
    if (stristr($content, "the id is ok man")) {
        mysql_query("UPDATE db SET status = 'OK' WHERE id = '$id'");
    }
}

答案 1 :(得分:0)

我们本周在工作中遇到的错误是来自MySQL的官方文档:http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

  

本节还介绍了在查询错误期间与服务器的相关丢失连接。

     

MySQL服务器消失的最常见原因是服务器超时并关闭了连接。