通常情况下,我们将Redis是安装局域网内,且不能允许通过联网访问。因此,通常情况下我们是不需要设置密码的,但是也有例外。下面将介绍怎样去设置Redis的登录密码。
打开 redis的 redis.conf 文件,搜索 requirepass 关键字,如下:
################################## SECURITY ################################### # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared
大概在370行左右。如果要设置密码,则将 “requirepass foobared” 去掉注释,开始密码保护。如果要修改密码,则替换 “foobared” 字符串为你自己的密码。我在这里设置密码为 “hxstrive”,如下:
################################## SECURITY ################################### # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # requirepass hxstrive
修改完密码后,需要重启 redis 服务器。启动完成后通过 redis 客户端 redis-cli 连接,然后获取所有的key,当然也可以是其他语句。执行 "keys *" 语句将抛出 “NOAUTH Authentication required” 错误信息。如下:
[root@localhost redis-3.0.6]# ./redis-cli 127.0.0.1:6379> keys * (error) NOAUTH Authentication required. 127.0.0.1:6379>
如果看见了上面的错误信息,则说明 redis 密码设置成功。此时,可以通过 “redis-cli -a 你的密码” 语句连接到 redis 服务器。如下:
[root@localhost redis-3.0.6]# ./redis-cli -a hxstrive 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379>
当我们用Java客户端连接redis时会遇到同样的问题,下面看一段简单的jedis连接redis的测试代码:
package com.huangx.jedis; import org.junit.Test; import redis.clients.jedis.Jedis; public class Test { @Test public void testTwo() { Jedis jedis = new Jedis("127.0.0.1"); System.out.println("Connection to server sucessfully"); // 查看服务是否运行 System.out.println("Server is running: " + jedis.ping()); } }
运行上面程序将抛出下面信息,如下:
redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required. at redis.clients.jedis.Protocol.processError(Protocol.java:117) at redis.clients.jedis.Protocol.process(Protocol.java:142) at redis.clients.jedis.Protocol.read(Protocol.java:196) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:187) at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:109) at com.firstelite.test.Test4Jedis.testTwo(Test4Jedis.java:15) ...
该错误是因为redis设置了密码,但在jedis中又没有指定密码,所以报了和刚才相同的错误,那么如何指定密码呢?很简单,Jedis的父类BinaryJedis提供了这样一样方法:
public String auth(final String password) { checkIsInMulti(); client.auth(password); return client.getStatusCodeReply(); }
修改测试程序,添加密码。代码如下:
package com.huangx.jedis; import org.junit.Test; import redis.clients.jedis.Jedis; public class Test { @Test public void testTwo() { Jedis jedis = new Jedis("127.0.0.1"); jedis.auth("hxstrive"); System.out.println("Connection to server sucessfully"); // 查看服务是否运行 System.out.println("Server is running: " + jedis.ping()); } }