Jedis是Redis官方推荐的java连接开发工具。

1,添加依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>

2,编码测试

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.254.129",6379);
        System.out.println(jedis.ping());
        jedis.close();
    }

运行报错

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.

redis是在保护模式下运行,但没有指定地址和授权密码给客户端。而这种模式只有本地回环接口才可以。如果你要从外表机器连接redis,可以采用以下方式解决。

1)禁用保护模式,通过发送指令 config set protected-mode no

2)更改redis配置文件,将protected mode 选项改为 no ,然后重启服务器。

3)如果是测试,再启动服务的时候,加上 --protected-mode no 选项。

4)指定地址或者授权密码。但是需要执行上面的其中一个步骤。

 

实际:禁用保护模式,redis配置文件中注释掉bind 127.0.0.1

 

 posted on 2020-11-20 21:12  会飞的金鱼  阅读(219)  评论(0)    收藏  举报