douzheren3349 2011-12-13 15:12
浏览 61
已采纳

mysql链接服务器丢失,无法重新连接

I get the following error:

Link to server lost, unable to reconnect

I have a mysql daemon coded, and I use mysql_pconnect to connect to mysql but after a while, I get the following error and the daemon stops functioning properly:

Link to server lost, unable to reconnect

What I do is the following:

while(true)
{
  $connect = mysql_pconnect(...);
  $db = mysql_select_db(...);
}

What can I do to prevent this? I need mysql connection to stay steady for the whole duration of the daemon - which may be forever.

  • 写回答

1条回答 默认 最新

  • donglie1994 2011-12-13 15:27
    关注

    You have a few choices.

    But just as a precursor, you should try and move away from relying on mysql_* and start using the PDO instead.

    Anyway... When you open a mysql connection, it will stay "available" until the wait timeout has expired. What you are doing is constantly creating a new connection, (also without closing the other connection). This means you will either hit the server mysql limit, or your unix box socket limit very quickly.

    Wait Timeout

    You should first check with your server to see what this timeout is set to. You might want to consider increasing it in your my.cnf if it is terribly low. The default period is 28800 seconds (if I recall correctly).

    You can check by issuing this query:

    SHOW variables like "%wait_timeout%"
    

    You can then change the value in your my.cnf to increase it or you can also set it using

    SET @@GLOBAL.wait_timeout=288000
    

    SELECT 1

    Okay, now, with a reasonable set timeout set, the "typical" way that you can make sure that you don't get the mysql has gone away message, is to just do a SELECT 1. This will do another query and make sure that the connection is held open.

    And, the benefit of using the PDO is that you can then catch PDOExceptions which might be thrown when a SELECT 1 fails.

    This means, that you can then try and connect again, if an exception is thrown, and then check again. If you can't connect after that then you should probably kill your daemon. Here is some code.... you would clearly have to make your PDO object using a valid connection string.

        // $pdo holds the connection and a PDO object.  
        try {
                $pdo->execute("SELECT 1");
            } catch (PDOException $e) {
                // Mysql has gone away. 
                $pdo = null
                $pdo = new PDO( $connectionString );
                echo "Mysql has gone away - But we attempted to reconnect
    ";
                try {
                     $pdo->execute("SELECT 1");
                } catch (PDOException $e) {
                     echo "Mysql has failed twice - Kill Daemon
    ";
                     throw($e);
                }
          }
    

    This is the solution that I would probably take.

    You can easily substitute the SELECT 1 with your own query that you actually want to use. This is just an example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器