一、错误如下
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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.

翻译如下

线程“main”中的异常 redis.clients.jedis.exceptions.JedisDataException:DENIED Redis 正在保护模式下运行,因为已启用保护模式,未指定绑定地址,未向客户端请求身份验证密码。在此模式下,仅接受来自环回接口的连接。
如果您想从外部计算机连接到 Redis,您可以采用以下解决方案之一:
1) 只需禁用保护模式,通过从与服务器相同的主机连接到 Redis,从环回接口发送命令“CONFIG SET protected-mode no”正在运行,但是如果这样做,请确保 Redis 不能从 Internet 公开访问。使用 CONFIG REWRITE 使此更改永久化。
2) 或者,您可以通过编辑 Redis 配置文件并将保护模式选项设置为“no”,然后重新启动服务器来禁用保护模式。 
3) 如果您手动启动服务器只是为了测试,请使用“--protected-mode no”选项重新启动它。 
4) 设置绑定地址或认证密码。
注意:您只需要执行上述操作之一,服务器就可以开始接受来自外部的连接。
二、解决方法
1、 禁用保护模式

①、编辑 Redis 配置文件 redis.conf 并将保护模式选项设置为“protected-mode no“
在这里插入图片描述
②、如果是服务器,需要屏蔽 127.0.0.1,或者改为 0.0.0.0 都可以,本地测试可以不设置
在这里插入图片描述
③、指定配置文件启动
不同的 redis 版本,redis 安装路径可能不同,我的 redis.conf 在/home/ling/software/redis-6.2.4路径下,启动文件都在 /home/ling/software/redis-6.2.4/src 下

ps -ef|grep redis  # 查询已启动 redis 服务器
kill -9 1048265  # 关闭 redis 服务器
./redis-server ../redis.conf # 当前路径为 /home/ling/software/redis-6.2.4/src 使用修改的配置文件启动 redis 服务器

在这里插入图片描述
④、测试

连接成功

public class TestPing {
    public static void main(String[] args) {
        // 1、 new 一个 redis 对象
        Jedis jedis = new Jedis("1.117.165.107",6379);
        jedis.set("name","tom");
        jedis.set("age","18");
        System.out.println(jedis.ping());      // pong
        System.out.println(jedis.get("name")); // tom
        System.out.println(jedis.get("age")); // 18
    }
}
2、设置绑定地址或认证密码
`第一种方法:redis.conf 配置文件修改 requirepass "123456"`
`第二种方法:命令修改`
config set requirepass 123456 # 添加密码
config get requirepass		# 获取密码
config set requirepass ""   # 清空(取消)密码

测试成功

public class TestPing {
    public static void main(String[] args) {
        // 1、 new 一个 redis 对象
        Jedis jedis = new Jedis("1.117.165.107",6379);
        jedis.auth("123456");
        jedis.set("name","tom");
        jedis.set("age","18");
        System.out.println(jedis.ping());      // pong
        System.out.println(jedis.get("name")); // tom
        System.out.println(jedis.get("age")); // 18
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐