报错信息

redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
Watching for file changes with StatReloader

错误分析:
Redis 报错 DENIED Redis is running in protected mode 表示 Redis 服务器处于保护模式,这是 Redis 的默认安全机制。以下是对问题的详细说明和解决方案:


问题原因

Redis 默认开启保护模式(protected-mode yes),当以下条件同时满足时会触发此模式:

  1. 未显式设置 bind 地址(即 Redis 监听所有网络接口);
  2. 未配置密码认证(requirepass);
  3. 尝试从非本地(非 127.0.0.1 或 ::1)的 IP 地址连接 Redis。

此时,Redis 仅允许来自本地回环接口(localhost)的连接,拒绝外部访问,以防止未授权访问和数据泄露。


解决方案

根据实际需求选择以下方法之一:


1. 临时禁用保护模式(仅测试环境)

通过本地连接 Redis 后执行命令(重启后失效):

redis-cli
CONFIG SET protected-mode no

适用场景:快速测试,但需确保 Redis 不暴露在公网。


2. 永久禁用保护模式

修改 Redis 配置文件(redis.conf):

  1. 找到并修改配置项:
    protected-mode no
    
  2. 重启 Redis 服务:
    sudo systemctl restart redis
    

注意:禁用保护模式后,需配合其他安全措施(如设置密码或限制 IP)。


3. 设置密码认证(推荐)

在 redis.conf 中添加密码:

  1. 修改配置项:
    requirepass your_strong_password
    
  2. 重启 Redis 服务:
    sudo systemctl restart redis
    
  3. 连接时使用密码:
    # Python 示例
    import redis
    r = redis.Redis(host='your_redis_ip', port=6379, password='your_strong_password')
    

4. 绑定指定 IP 地址

限制 Redis 监听的网络接口:

  1. 修改 redis.conf:
    bind 127.0.0.1 192.168.1.100  # 允许本地和指定内网 IP
    
  2. 重启 Redis 服务。

5. 启动时禁用保护模式(临时测试)

通过命令行启动 Redis 时添加参数:

redis-server --protected-mode no

安全建议

• 生产环境必须配置密码(requirepass)或结合防火墙限制访问 IP。
• 避免直接禁用保护模式且不设密码,否则 Redis 可能被恶意攻击。
• 配置文件路径通常为 /etc/redis/redis.conf(Linux)或 redis.windows.conf(Windows)。


验证配置

检查配置是否生效:

redis-cli -h your_redis_ip -p 6379
AUTH your_strong_password  # 若设置了密码
PING  # 应返回 "PONG"

选择最适合场景的方案,优先推荐设置密码认证以平衡安全性和可用性。

经历以上修改依旧不行

终结者

最终查看当前的conf 是正常的,但是为什么不能正常使用这个conf呢
于是

/usr/local/bin/redis-server  /usr/local/jtmr/redis-5.0.10/redis.conf 

终于可以了 可以指定了conf

更多推荐