Home > Database > Mysql Tutorial > MySQL server has gone away_MySQL

MySQL server has gone away_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:35:30
Original
899 people have browsed it

bitsCN.com

今天遇到一个Mysql的问题:执行update时报错:MySQL server has gone away

由于是Linux后台服务程序,需要7*24运行,但程序在初始化时进行了DB的open操作之后,就进入了for(;;),遇到某个异常导致数据库操作句柄断开,却又没有相应的出错处理,导致程序一直报错。

下面是关于使用mysql_ping函数之后的改进,

这是我的的数据库操作类中间的一个函数
function ping()

{
  if(!mysql_ping($this->link))

   {
    mysql_close($this->link); //注意:一定要先执行数据库关闭,这是关键
    $this->connect($this->t_dbhost,$this->t_dbuser,$this->t_dbpw,$this->t_dbname,$this->t_pconnect);
    }
}


我需要调用这个函数的代码可能是这样子的
for($i=0;$i$str=file_get_contents(’xxx.xxx,xxx,xxxx’);
}
$db->ping(); //经过前面的网页抓取后,或者会导致数据库连接关闭,检查并重新连接
$db->query(’select * from table’);
ping()这个函数先检测数据连接是否正常,如果被关闭,整个把当前脚本的MYSQL实例关闭,再重新连接。
经过这样处理后,可以非常有效的解决MySQL server has gone away这样的问题,而且不会对系统造成额外的开销。

bitsCN.com
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