使用swoole加速laravel报错2006 MySQL server has gone away,
原因是laravel在swoole加速下进程是常驻内存,mysql的配置wait_timeout=86400,interactive_timeout=7200,当超过时间mysql就会自动踢掉php的连接,php连接又是在swoole下常驻内存
{"code":"HY000","msg":"SQLSTATE[HY000]: General error: 2006 MySQL server has gone away (SQL: select * from `user` where `openid` = xxxx limit 1)","errorFile":"\/home\/wwwroot\/xxxxx\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php:692"}]
修改如下vendor\laravel\framework\src\Illuminate\Database.Connetction.php,
1、找到692行判断错误符合重连,
2、新增符合重连条件方法isRecon
protected function runQueryCallback($query, $bindings, Closure $callback)
{
// To execute the statement, we'll simply call the callback, which will actually
// run the SQL against the PDO connection. Then we can calculate the time it
// took to execute and log the query SQL, bindings and time in our memory.
try {
return $callback($query, $bindings);
}
// If an exception occurs when attempting to run a query, we'll format the error
// message to include the bindings with SQL, which will make this exception a
// lot more helpful to the developer instead of just the database's errors.
catch (Exception $e) {
//符合条件重新连接,然后重新直接sql
if($this->isRecon($e->getMessage())){
$this->reconnect();
return $this->runQueryCallback($query, $bindings, $callback);
}
//符合条件重新连接
throw new QueryException(
$query, $this->prepareBindings($bindings), $e
);
}
}
//新增方法 从拷贝过来vendor\laravel\framework\src\Illuminate\Database\DetectsLostConnections.php
/**
* 符合重新连接的
* @param $message
* @return bool
*/
public function isRecon($message){
return Str::contains($message, [
'server has gone away',
'no connection to the server',
'Lost connection',
'is dead or not enabled',
'Error while sending',
'decryption failed or bad record mac',
'server closed the connection unexpectedly',
'SSL connection has been closed unexpectedly',
'Error writing data to the connection',
'Resource deadlock avoided',
'Transaction() on null',
'child connection forced to terminate due to client_idle_limit',
'query_wait_timeout',
'reset by peer',
'Physical connection is not usable',
'TCP Provider: Error code 0x68',
'ORA-03114',
'Packets out of order. Expected',
'Adaptive Server connection failed',
'Communication link failure',
'connection is no longer usable',
'Login timeout expired',
'SQLSTATE[HY000] [2002] Connection refused',
'running with the --read-only option so it cannot execute this statement',
'The connection is broken and recovery is not possible. The connection is marked by the client driver as unrecoverable. No attempt was made to restore the connection.',
'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Try again',
'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known',
'SQLSTATE[HY000]: General error: 7 SSL SYSCALL error: EOF detected',
'SQLSTATE[HY000] [2002] Connection timed out',
'SSL: Connection timed out',
'SQLSTATE[HY000]: General error: 1105 The last transaction was aborted due to Seamless Scaling. Please retry.',
'Temporary failure in name resolution',
'SSL: Broken pipe',
'SQLSTATE[08S01]: Communication link failure',
'SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host',
'SQLSTATE[HY000]: General error: 7 SSL SYSCALL error: No route to host',
]);
}
网友评论