21,893
社区成员




thinkphp6+think-swoole偶尔出现 AUTH failed while reconnecting Redis server went away
有大佬知道咋解决吗
有秘密认证,非防火墙原因。
项目正常运行几天后就会报错,然后重启php think swoole就又好了。
不知道那里出了问题
明白了,既然是有密码认证且运行一段时间后出现的问题,这很可能是因为Redis连接池或长连接管理的问题。以下是几个针对性的解决方案:
config/swoole.php
中添加或修改 Redis 连接池配置:return [
'pool' => [
'redis' => [
'enabled' => true,
'max_active' => 100, // 最大活动连接数
'max_wait_time' => 5, // 等待连接的超时时间(秒)
'max_idle_time' => 30, // 连接最大闲置时间(秒)
'min_idle' => 5, // 最小空闲连接数
'heartbeat' => 60, // 心跳检测间隔(秒)
],
],
];
class Redis
{
protected $maxRetries = 3;
public function command($name, ...$arguments)
{
$attempts = 0;
while ($attempts < $this->maxRetries) {
try {
return $this->connection->$name(...$arguments);
} catch (\RedisException $e) {
$attempts++;
if (strpos($e->getMessage(), 'went away') !== false ||
strpos($e->getMessage(), 'AUTH failed') !== false) {
// 重新连接
$this->reconnect();
continue;
}
throw $e;
}
}
}
}
redis.conf
中调整以下参数:timeout 0 # 客户端超时时间,0为永不超时
tcp-keepalive 300 # TCP keepalive 心跳检测间隔
// 在服务启动时添加定时任务
public function onWorkerStart($server, $workerId)
{
if ($workerId == 0) {
\Swoole\Timer::tick(30000, function () {
try {
$redis = cache()->store('redis')->handler();
$redis->ping();
} catch (\Exception $e) {
// 记录日志
\think\facade\Log::error('Redis connection lost: ' . $e->getMessage());
// 尝试重新连接
$this->reconnectRedis();
}
});
}
}
// 在配置文件中开启日志
'redis' => [
'type' => 'redis',
// ... 其他配置 ...
'log_level' => ['error'], // 记录错误日志
],
public function onShutdown($server)
{
try {
$redis = cache()->store('redis')->handler();
$redis->close();
} catch (\Exception $e) {
\think\facade\Log::error('Redis shutdown error: ' . $e->getMessage());
}
}
建议实施步骤:
另外,建议监控:
如果实施这些措施后问题仍然存在,建议收集以下信息进行进一步分析:
这些措施应该能够显著提高 Redis 连接的稳定性,减少 AUTH failed 错误的出现。
遇到非常相似的问题,请问群主解决了吗?