在PhpRedisConnector.php第161行连接拒绝redis laravel

问题描述 投票:0回答:1

我需要帮助在 Laravel 中正确设置 Redis。我已经尝试过在互联网上找到的内容,但我不确定我是否错过了一步。我收到以下错误:

In PhpRedisConnector.php line 161:

  Connection refused

我安装了 PHP 的 Redis 扩展,我想我可以用这个来确认:

php-extension

然后我安装了Docker和Redis,并且我收到了来自PING的PONG。

pong

另外,我已经重新启动了 FPM

sudo systemctl restart php8.1-fpm.service

之后,我清除了 Laravel 缓存。

clear-cache

我使用以下内容配置了 .env。

env content

我尝试执行这个 artisan 命令,但收到错误:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class TestRedisCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'custom:test_redis';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $redis = Redis::connection();
        if ($redis->ping()) {
            echo "Conexión Exitosa";
        }

        return Command::SUCCESS;
    }
}

请问我有什么遗漏的吗?我不明白错误可能出在哪里。

php laravel docker ubuntu redis
1个回答
0
投票

您遇到的错误,

Connection refused
,是在此类情况下发生的情况的最清晰指示之一。除了防火墙和其他东西之外,唯一真正的选择是该端口中没有运行 Redis

您对

docker exec -it local-redis redis-cli
->
ping
的测试实际上没有任何作用来确认您的问题,因为您使用
docker exec
所做的是连接到容器内部并在那里进行测试,而您的应用程序正在本地运行。

如果您从主机连接到在容器中运行的 Redis,如环境变量所示:

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

然后需要在容器外部发布端口。默认情况下和设计上它们是不打开的。

你说你是这样连接的:

docker run --name local-redis -d redis

您需要将其更改为:

docker run --name local-redis -p 127.0.0.1:6379:6379 -d redis
© www.soinside.com 2019 - 2024. All rights reserved.