1 Java 连接redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/faicm/article/details/52453022





public class RedisMain {
	public static void main(String[] args) {
		Jedis jedis = new Jedis("", 6379);
		jedis.set("test", "abc");
		String str = jedis.get("test");


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注释掉,这个时候,代码不需要auth,也可以。


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

           >>set  name wang  >>get  name