4

我一直在摸索,试图找出导致脚本间歇性错误的原因。错误是:SQLSTATE[HY000]: General error: 2006 MySQL server has gone away。

我下面的脚本是执行 curl 的函数的一部分,从 JSON 响应中获取一些值,然后将它们写入表。我会说 80% 的时间它工作正常,然后其他 20% 我得到服务器消失错误。我无法确定导致它出错的任何趋势,它似乎只是随机的。任何想法为什么我可能会收到此错误?谢谢你检查这个

    ...
    //post via cURL 
    $ch = curl_init( $url );
    $timeout = 500;
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    $this->response = curl_exec( $ch );
    $this->json_decoded = json_decode($this->response);
    $this->full = print_r($this->json_decoded, true);
    $client_leadid = $this->json_decoded->Parameters->lead_id;
    $client_status = $this->json_decoded->Status;   
    $length = curl_getinfo($ch);
    curl_close($ch);

    //record in DB
    $insert = $this->full.' | '.$url.' | '.$myvars.' | '.$this->date . ' | Time Taken: '.$length['total_time'];
    $db->exec("UPDATE table SET client_resp = '$insert' WHERE global_id = '$this->leadid' LIMIT 1");
    $db->exec("UPDATE table SET client_leadid = '$client_leadid' WHERE global_id = '$this->leadid' LIMIT 1");
4

1 回答 1

4

这可能是因为您的 CURL 请求花费的时间比 mysql 连接超时时间长

要么 1) 为 CURL 设置请求超时,以便它在错误时更快地死掉(CURLOPT_CONNECTTIMEOUT 仅用于连接 - CURLOPT_TIMEOUT 用于请求的总长度,如果服务器没有及时响应,它将停止)2)出现mysql 空闲超时以防止服务器因未发送查询而断开连接
3) 检测错误并自动重新连接到 mysql

mysql> show variables like "%timeout%";
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| connect_timeout          | 5     |
| delayed_insert_timeout   | 300   |
| innodb_lock_wait_timeout | 50    |
| interactive_timeout      | 28800 |
| net_read_timeout         | 30    |
| net_write_timeout        | 60    |
| slave_net_timeout        | 3600  |
| table_lock_wait_timeout  | 50    |
| wait_timeout             | 28800 |
+--------------------------+-------+
9 rows in set (0.00 sec)

wait_timeout 和 interactive_timeout 是你关心的两个

于 2012-08-27T22:44:53.727 回答