Error:
#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