Home >Database >Mysql Tutorial >PHP program mysql error mysql has gone away
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.
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;
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_connect
After executing mysql_connect
, re-execute mysql_query
If mysql_query
returns success, then the connection is valid Yes, this is a normal call.
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; } }
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(); ... } }
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.
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 <pre class="brush:php;toolbar:false;">// 开启断线重连&#39;break_reconnect&#39; => true,</pre>
to the
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!