YII2 console MySQL server has gone away

Source: Internet
Author: User
Keywords Php yii2 console mysql
Tags yii
A. Question:
There are now 1 PHP scripts executed using YII2 's console, which has been executed in the background.
Trace output Log Discovery Report:

2006 MySQL server has gone away

Two. The main code is as follows:

try{Yii::$app->db->open();    if (Yii::$app->db->getIsActive()) {       // 做insert操作    }}catach(\yii\db\Exception $e){   var_dump($e);  // 重新连接 Yii::$app->db->close(); Yii::$app->db->open();}

Three. Scene:

This PHP script has been executed in the background, and when there is data coming up, the insert operation is performed and no data is taken to perform the insert operation. Maybe yesterday 18:00 fishes data, then insert succeeds and then comes to another data 10:00 the next day, or the interval may be very small;

Four. Analysis:
My insert operation just inserts an INT field and a timestamp, so the problem is not going to take too long for this insert to execute.
Because the interval is likely to be too long, it exceeds the MySQL wait_timeout, causing this to occur:

Five. How to solve Yii2?
1. I did not modify the Mysql.ini timeout parameter permission, so this workaround is not feasible
2. The solution is also relatively simple: before each insert to determine whether the DB connection active, or reconnect, then why my code does not solve the problem? How to write? pay attention to using YII2 class to solve

    1. There's a discussion on GitHub, but there's no standard solution.

Reply content:

A. Question:
There are now 1 PHP scripts executed using YII2 's console, which has been executed in the background.
Trace output Log Discovery Report:

2006 MySQL server has gone away

Two. The main code is as follows:

try{Yii::$app->db->open();    if (Yii::$app->db->getIsActive()) {       // 做insert操作    }}catach(\yii\db\Exception $e){   var_dump($e);  // 重新连接 Yii::$app->db->close(); Yii::$app->db->open();}

Three. Scene:

This PHP script has been executed in the background, and when there is data coming up, the insert operation is performed and no data is taken to perform the insert operation. Maybe yesterday 18:00 fishes data, then insert succeeds and then comes to another data 10:00 the next day, or the interval may be very small;

Four. Analysis:
My insert operation just inserts an INT field and a timestamp, so the problem is not going to take too long for this insert to execute.
Because the interval is likely to be too long, it exceeds the MySQL wait_timeout, causing this to occur:

Five. How to solve Yii2?
1. I did not modify the Mysql.ini timeout parameter permission, so this workaround is not feasible
2. The solution is also relatively simple: before each insert to determine whether the DB connection active, or reconnect, then why my code does not solve the problem? How to write? pay attention to using YII2 class to solve

    1. There's a discussion on GitHub, but there's no standard solution.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.