Home > Database > Mysql Tutorial > body text

PHP program mysql error mysql has gone away

小云云
Release: 2018-03-14 15:49:03
Original
3663 people have browsed it


This article mainly shares with you the memory-resident PHP program mysql error mysql has gone away. In the cli environment, the PHP program needs to run for a long time. The communication between the client and the MySQL server TCP connections are unstable.

The reasons for instability may be as follows:

  • MySQL-Server will automatically cut off the connection within a certain period of time

  • PHP When the program encounters an idle period and there is no MySQL query for a long time, MySQL-Server will also cut off the connection and recycle resources

  • In other cases, execute the kill process in the MySQL server to kill a certain connection, and the MySQL server Restart

  • Network jitter

At this time, the MySQL connection in the PHP program fails. If mysql_query is still executed, an error MySQL server has gone away will be reported. If the program cannot handle it, it directly encounters a fatal error and exits. Therefore, the PHP program needs to disconnect and reconnect.

Solution

mysql_ping

Many people have proposed the mysql_ping solution, which performs connection detection or timing every time mysql_query Connection detection.

This solution is not the best. The reason is: mysql_ping needs to actively detect the connection, which brings additional consumption. Executing mysql_ping regularly cannot solve the problem. For example, after just executing the mysql_ping test, the connection is closed;

Capture the error code and disconnect and reconnect

Its principle is: mysql_query detects the return value after execution. If mysql_query fails to return, the detection error code is found to be 2006/2013 (these 2 errors indicate connection Failure), execute mysql_connectAfter executing mysql_connect, re-execute mysql_queryIf mysql_query returns success, then the connection is valid Yes, this is a normal call.

Many well-known PHP resident process frameworks

  • query method in swoole_framework.

$res = mysql_query($sql, $this->conn);if ($res === false)
{    if (mysql_errno($this->conn) == 2006 or mysql_errno($this->conn) == 2013)
    {        $r = $this->checkConnection();        if ($r === true)
        {
            continue;
        }
    }
Copy after login
  • execute method of database connection class in workerman.

  protected function execute($query, $parameters = "")
    {        try {            ...
        } catch (PDOException $e) {
            // 服务端断开时重连一次            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
                $this->closeConnection();
                $this->connect();            ...
        }
    }
Copy after login

Obviously, capturing the error code and disconnecting and reconnecting is a highly reliable and low-cost solution. It is recommended for everyone to use it in a permanent environment.

thinkphp + phpworkman A similar situation will occur when using

TP+workman. Starting from version V5.0.6+, TP supports Mysql’s disconnection and reconnection mechanism. It is turned off by default. If necessary, add

// 开启断线重连'break_reconnect' => true,
Copy after login
to the application/databases.php

database configuration file and it will be OK.

Related recommendations:

The memory-resident PHP program mysql reports an error

The above is the detailed content of PHP program mysql error mysql has gone away. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template