Redis客户端登录

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.conf配置文件中修改protected-mode

protected-mode yes -> protected-mode no

二、启动时添加 –protected-mode no

bin/redis-server conf/redis.conf --protected-mode no

三、密码授权登录

  1. 在redis.conf配置文件中修改requirepass
    主节点
requirepass 111111

从节点

requirepass 111111
masterauth 111111
  1. 无密码情况下登录客户端
[root@redis redis]# bin/redis-cli -h IP -p 6379
IP:6379> keys *
(error) NOAUTH Authentication required.
  1. 有密码情况下登录客户端
[root@redis redis]# bin/redis-cli -h IP -p 6379 -a 111111
IP:6379> keys *
 1) "c"
 2) "hello"
 3) "mylist"
  1. 无密码情况下登录客户端,之后再授权密码
[root@redis redis]# bin/redis-cli -h IP -p 6379
IP:6379> auth 111111
OK
IP:6379> keys *
 1) "c"
 2) "hello"
 3) "mylist"
  1. Java Api登录
JedisShardInfo jInfo = new JedisShardInfo("IP", 6379);
jInfo.setPassword("111111");
Jedis jedis = new Jedis(jInfo);
String value = jedis.get("hello");
System.out.println(value);  
//word

针对sentinel配置

sentinel.conf

sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 111111
String masterName = "mymaster";
HashSet<String> set = new HashSet<String>();
set.add("IP:26379");
set.add("IP:26380");
JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName,set,"111111");
Jedis jedis = sentinelPool.getResource();
System.out.println(jedis.get("hello"));

猜你喜欢

转载自blog.csdn.net/xcf111/article/details/85037728