|
下午发了一帖,没人回复我,这心里这个着急啊,没办法,没人鸟你你就需要自己解决,经过代码层层调试,发现出错的原因是因为我在pcntl_fork后在子进程中仍然用原来的连接资源调用mysql,导致出现MySQL server has gone away,经过google之后给出的解决办法是在pcntl_fork之后强制重新链接一下mysql,下面是官方贴出的例子
PHP复制代码 <?php [/font][/backcolor][/color][color=#ff800]// Create the MySQL connection [/font][/backcolor][/color][color=#00bb]$db = mysql_connect($server, $username, $password); [/font][/backcolor][/color][color=#00bb]$pid = pcntl_fork(); if ( [/font][/backcolor][/color][color=#00bb]$pid == -1 ) { [/font][/backcolor][/color][color=#ff800]// Fork failed [/font][/backcolor][/color][color=#0770]exit(1); } else if ( [/font][/backcolor][/color][color=#00bb]$pid ) { [/font][/backcolor][/color][color=#ff800]// 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 [/font][/backcolor][/color][color=#00bb]$db = mysql_connect($server, $username, $password, true); } else { [/font][/backcolor][/color][color=#ff800]// We are the child // Do something with the inherited connection here // It will get closed upon exit [/font][/backcolor][/color][color=#0770]exit(0); [/font][/backcolor][/color][color=#00bb]?> 在下面是我的代码 在controler的__constroct中已经加载model了 PHP复制代码 <?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://rootlocalhost/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有问题,或者是我写的不对) 评分 | |
相关帖子
|
|
|
官方例子乱码了,重新补上
PHP复制代码 <?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); ?> | |
|
为什么没人解答呢,焦躁不安啊
| |
|
這位大哥,建議您去看一下官方文件.
数据库类 | |
|
貌似是CI的一个bug吧,总之fork后强制重连mysql会提示那个错误,百撕不得骑姐啊,我自己写一个mysql的链接解决了.
| |
|
$this->db->reconnect();
| |