php 多程序下mysql連線 gone away
阿新 • • 發佈:2018-12-27
php 在命令列模式下啟動多程序如果父程序有sql查詢,可能會導致子程序裡面的sql查詢 報錯 General error: 2006 MySQL server has gone away
當fork的子程序都共用相同的mysql連線的時候,會出現該錯誤,每個子程序單獨一個mysql連線即可解決。
解決辦法:在子程序執行之前,先把mysql的連線close即可。
$db = \Yii::$app->getDb(); $bizs = $db->createCommand("select xx from xxx where status = 0")->queryAll(); foreach($bizs as $biz){$biz = $biz['biz']; // 建立子程序 $pid = pcntl_fork(); if ($pid == -1){// 返回值為-1,建立失敗 die('could not fork'); }elseif ($pid){// 返回值大於0,是父程序 echo "parent \t", date("H:i:s", time()), "\n"; }else {// 返回值等於0,是子程序 echo "child \t", date("H:i:s", time()), "\n"; try{ \Yii::$app->db->close();// solve 子程序 MySQL server has gone $this->run($biz); }catch (Exception $e){ var_dump($e); }}}\Yii::$app->db->close();// solve 主程序 MySQL server has gone