Yii 数据库重连告别General error: 2006 MySQL server has gone away

General error: 2006 MySQL server has gone away

  • 错误原因
  • 制造错误
  • 解决办法
  • 最新办法

错误原因

Mysql has gone away

  1. MySQL 服务宕了
  2. mysql连接超时 show global variables like ‘%timeout’;
  3. mysql请求链接进程被主动kill show global status like ‘com_kill’;
  4. Your SQL statement was too large. show global variables like ‘max_allowed_packet’;

制造错误

造一个表

-- ----------------------------
-- Table structure for t_biubiu
-- ----------------------------
DROP TABLE IF EXISTS `t_biubiu`;
CREATE TABLE `t_biubiu` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `value` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后造一个程序

public function actionInsert(){
   
        Biubiu::model()->findAll();
        $result = Yii::app()->db->createCommand("show global variables like '%timeout'")->queryAll();
        self::show_vars($result);
        for($index = 1 ;$index< 10;$index++){
            $model_ = new Biubiu();
            $model_->value = "insert".$index;
            if($index == 8){
                sleep(31);
            }
            $model_->save();
        }
        $model = new Biubiu();
        $model->value = "insert4";
        $model->save();
     }

设置你的MYSQL的wait_timeout = 30,interactive_timeout也要设置,否则不生效

执行的时候就会抛异常:

exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away. The SQL statement executed was: INSERT INTO `t_biubiu` (`value`
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值