1 Java 连接redis

来源:互联网 发布:java分母最小化 编辑:程序博客网 时间:2024/06/03 01:41

在安装好redis后,从java这里连接redis。

            

                 <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.2</version></dependency>


==

public class RedisMain {public static void main(String[] args) {Jedis jedis = new Jedis("192.168.40.129", 6379);//jedis.auth("123456");  jedis.set("test", "abc");String str = jedis.get("test");System.out.println(str);}}

报错

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-cli执行   config set requirepass 123456,这个时候123456就是密码了,然后java代码需要先执行 jedis.auth("123456") 才行。

解决②,把redis主目录的redis.conf复制到src中,然后修改,把protected-mode改为 no   把bind 127.0.0.1注释掉,这个时候,代码不需要auth,也可以。

==



注意,在设置了密码后如果再需要用  ./redis-cli 使用redis,可能需要 >>auth 123456才行。

           >>set  name wang  >>get  name

0 0
原创粉丝点击