Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The cause of mysql has gone away error reported by mysql in PHP Program and its solution

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the PHP program mysql reported mysql has gone away error reasons and solutions, the content of the article is carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the PHP program mysql reported mysql has gone away error reasons and solutions.

In the cli environment, the PHP program needs to run for a long time, and the TCP connection between the client and the MySQL CVM is unstable. The reasons for instability are as follows:

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

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

In other cases, execute kill process in the MySQL CVM to kill a connection, and restart the MySQL CVM.

Network jitter

At this point, the MySQL connection in the PHP program fails. If mysql_query is still executed, an error of MySQL server has gone away will be reported. If the program can't handle it, it directly encounters a fatal error and exits. So the PHP program needs to be disconnected and reconnected.

Solution mysql_ping

Many people have put forward the scheme of mysql_ping, which carries out connection detection or timing connection detection every time mysql_query.

This plan is not the best. The reason is that mysql_ping needs to actively detect connections, resulting in additional consumption. Regular execution of mysql_ping does not solve the problem, for example, the connection is closed just after the mysql_ping test has been performed

Capture the error code, disconnect and reconnect

Its principle is: after mysql_query execution, the return value is detected. If mysql_query returns a failure, the detection error code is found to be 2006Universe 2013 (these two errors indicate connection failure). After executing mysql_connect again, mysql_connect executes mysql_query again. If mysql_query returns successfully, then the connection is valid. This is a normal call.

Many well-known PHP resident process frameworks

The query method in swoole_framework.

$res = mysql_query ($sql, $this- > conn); if ($res = false) {if ($this- > conn) = = 2006 or mysql_errno ($this- > conn) = = 2013) {$r = $this- > checkConnection (); if ($r = true) {continue;}}

The database connection class execute method in workerman.

Protected function execute ($query, $parameters = "") {try {...} catch (PDOException $e) {/ / reconnect if ($e-> errorInfo [1] = = 2006 | | $e-> errorInfo [1] = 2013) {$this- > closeConnection (); $this- > connect () ...}}

It is obvious that capturing error codes and reconnecting disconnected is a highly reliable and low-cost solution, which is recommended for use in resident environments.

Thinkphp + phpworkman use

A similar situation will occur in TP+workman. Starting from version 5.0.6 +, TP supports the disconnection and reconnection mechanism of Mysql. It is turned off by default. If necessary, add it to the application/databases.php database configuration file.

/ / enable disconnection and reconnection 'break_reconnect' = > true

After reading the above about the causes and solutions of PHP program mysql reporting mysql has gone away errors, many readers must have some understanding. If you need to get more industry knowledge and information, you can continue to follow our industry information column.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report