Thinkphp appearance General error:2006 MySQL server have gone away workaround

Source: Internet
Author: User
Tags prepare


#13 {main}sqlstate[hy000]: General error:2006 MySQL server have gone away
FILE: \thinkphp\library\think\db\driver.class.php (169)

Cause Analysis:

This error message is run in CLI mode, will appear after a period of time, query data found that MySQL default is not 8 hours (2.88 million seconds) will be disconnected

Solution, Solution found three

Method 1

Configure MYSQL.CNF (the Windows system is My.ini), specify Wait_timeout and Interactive_timeout, and set a larger value, such as one year (86400*365).

Method 2

After the link by executing a command to specify the Wait_timeout and interactive_timeout of the link, the principle is the same as "1", but this method only affects the link, the way "1" will affect all links.

However, the thinkphp has encapsulated the database driver, so it is not good to specify a single time. My practice is to judge Php_sapi, if it is the CLI set wait_timeout and Interactive_timeout

// thinkphp\library\think\db\driver.class.php Line 105th if (Php_sapi = = ' CLI ')     {$query$this->linkid[$linkNum]->prepare ("set Session wait_timeout=31536000,interactive_timeout=31536000,net_read_timeout=10000 ");      $query, execute ();}

Method 3

Now that the timeout is broken, we can take a wire break to connect the way

Thinkphp\library\think\db\driver.class.php Line 105th
$this->linkid[$linkNum]->setattribute (Pdo::attr_errmode, PDO::errmode_exception); $this->linkid[$linkNumfalse); $this->linkid[$linkNumfalse);
//thinkphp\library\think\db\driver.class.php Line 159th (Query method) and 220 lines (Execute method)Try {            $this->pdostatement =$this->_linkid->prepare ($str); } Catch(\pdoexception$e) {            //Wire Break re-connect            if($e->ERRORINFO[1] = = 2006 | |$e->ERRORINFO[1] = = 2013) {                Echo"---> DB reconnecting...\n"; $this->linkid =Array(); $this-_linkid =NULL; $this->initconnect (false); $this->pdostatement =$this->_linkid->prepare ($str); }        }

Thinkphp appearance General error:2006 MySQL server have gone away workaround

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: 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.