php,mysql服务器不见了 [英] php, mysql server has gone away

查看:89
本文介绍了php,mysql服务器不见了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是错,当您在查询之前连接到LINE上的数据库时,您仍然收到"MySQL服务器已消失"的信息吗?

What is wrong, when you connect to the DB on the LINE BEFORE THE QUERY, and you still get "MySQL server has gone away"?

检查此示例代码:

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循环中得到了它. 顺便说一句,我需要在foreachloop中进行连接,因为它可能需要一段时间才能找到要更新的内容(例如1-2分钟),然后确定我将使mysql服务器消失.

mysql server is gone away, i get that in the foreach loop. BTW i need to connect in the foreachloop, because it may take along time before it finds something to update (like 1-2 minutes), and then for sure i will get mysql server has gone away.

推荐答案

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

I assume your issue is that the script may execute for a very long time before sending the first UPDATE query.

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

You should check the wait_timeout value in my.cnf. You can check your wait_timeout by running the query "SHOW VARIABLES;"

您也可以尝试编写一段代码来重新连接:

You can also try to piece of code to do a reconnect:

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

这篇关于php,mysql服务器不见了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆