I’m writing a console application with Yii, and encountered the MySQL Server has gone away error. This is related to using PHP’s pcntl_fork(), which makes PHP lose its MySQL connection after a while in the child processes.
The Yii solution is very easy, but I thought I might share it anyway for people who’d encounter the same issue:
Yii::app()->getDb()->setActive(false); $pid = pcntl_fork(); Yii::app()->getDb()->setActive(true);
This basically closes the connection before forking, and reinstantiates it in the child and parent after the fork. By doing that, the error disappears and you can continue to use everything you had already collected from the db