thinkingbullet 发表于 2013-6-18 23:17:42

pcntl_fork后提示MySQL server has gone away

下午发了一帖,没人回复我,这心里这个着急啊,没办法,没人鸟你你就需要自己解决,经过代码层层调试,发现出错的原因是因为我在pcntl_fork后在子进程中仍然用原来的连接资源调用mysql,导致出现MySQL server has gone away,经过google之后给出的解决办法是在pcntl_fork之后强制重新链接一下mysql,下面是官方贴出的例子

<?php
// Create the MySQL connection
$db = mysql_connect($server, $username, $password);

$pid = pcntl_fork();
            
if ( $pid == -1 ) {      
    // Fork failed            
    exit(1);
} else if ( $pid ) {
    // We are the parent
    // Can no longer use $db because it will be closed by the child
    // Instead, make a new MySQL connection for ourselves to work with
    $db = mysql_connect($server, $username, $password, true);
} else {
    // We are the child
    // Do something with the inherited connection here
    // It will get closed upon exit
    exit(0);
?>

在下面是我的代码
在controler的__constroct中已经加载model了


<?php

class Cron extends CI_Controller {

    public function __construct() {
      parent::__construct();
      $this->load->model('m_monitor', 'model');
    }

    public function test() {
      //      从model中取出的数据
      //      $data = $this->model->getweb();
      $data = array(
            0 => array(
                'm_name' => 'baidu',
                'mid' => '7',
                'm_url' => 'http://www.baidu.com'
            ),
            1 => array(
                'm_name' => 'sina',
                'mid' => '8',
                'm_url' => 'http://www.sina.com.cn'
            )
      );
      foreach ($data as $key => $value) {
            $pid = pcntl_fork();
            if ($pid == -1) {
                exit('子进程创建失败');
            } elseif ($pid) {
                //pcntl_wait($status);

//                model中的db方法返回的是$this->db;
//                $this->model->db()->reconnect(true);
                $dsn = 'dbdriver://root:@localhost/webmon';
                $this->load->database($dsn);
            } else {
                //当前时间戳
                $time = time();
//                //上次任务运行结束时间[从model中查询数据库得到的数据]
                $lasttime = $this->model->getruntime(1, $value['mid']);
//                //用户设置的任务运行周期[从model中查询数据库得到的数据]
                $cron_time = $this->model->gettasktime(1, $value['m_url']);
            }
      }
    }

}

?>

运行后发现仍然提示MySQL server has gone away,难道CI 强制重新链接mysql不是我写的那样,求大大们给出解决方法(目前猜测是CI 强制重新链接mysql有问题,或者是我写的不对)

thinkingbullet 发表于 2013-6-18 23:20:29

官方例子乱码了,重新补上

<?php
// Create the MySQL connection
$db = mysql_connect($server, $username, $password);

$pid = pcntl_fork();
            
if ( $pid == -1 ) {      
    // Fork failed            
    exit(1);
} else if ( $pid ) {
    // We are the parent
    // Can no longer use $db because it will be closed by the child
    // Instead, make a new MySQL connection for ourselves to work with
    $db = mysql_connect($server, $username, $password, true);
} else {
    // We are the child
    // Do something with the inherited connection here
    // It will get closed upon exit
    exit(0);
?>

thinkingbullet 发表于 2013-6-19 09:53:42

为什么没人解答呢,焦躁不安啊

Raphael 发表于 2013-6-19 11:15:41

這位大哥,建議您去看一下官方文件.
数据库类

thinkingbullet 发表于 2013-6-19 16:41:28

貌似是CI的一个bug吧,总之fork后强制重连mysql会提示那个错误,百撕不得骑姐啊,我自己写一个mysql的链接解决了.

13579443 发表于 2013-10-13 00:08:23

$this->db->reconnect();

瓦信 发表于 2013-11-1 11:00:42

重连前一定要先close
mysql_close($connid);
mysql_connect(..........)
页: [1]
查看完整版本: pcntl_fork后提示MySQL server has gone away