redis 入门

Submitted by Lizhe on Mon, 07/03/2017 - 11:03

因为redis不在centos的标准库中, 需要先安装epel-release库

yum install epel-release 

yum install redis

如果不使用systemctl启动的话,需要使用下面的命令

/usr/bin/redis-server /etc/redis.conf

从局域网访问需要先去掉redis.conf文件中的

 bind 127.0.0.1

否则只能本机登录

然后我们尝试使用java客户端连接

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>name.lizhe</groupId>
    <artifactId>redissample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>

</project>

java代码

package redissample;

import redis.clients.jedis.Jedis;

public class App {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("172.28.128.4");
        System.out.println("ping...");
        System.out.println("result: "+jedis.ping());
    }
}

低版本(3.0)的redis此时可以正常工作

高版本(3.2.3-1.el7)会抛出如下错误,需要特别设定权限

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设置一个密码吧

/usr/bin/redis-cli                                                                                                                                                               
127.0.0.1:6379> config set requirepass test123
OK
 

java客户端添加验证

package redissample;

import redis.clients.jedis.Jedis;

public class App {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("172.28.128.4");
        jedis.auth("test123");
        System.out.println("ping...");
        System.out.println("result: "+jedis.ping());
    }
}

这种方式方便快捷,不需要重启redis, 但是每次重启之后设置的密码都会丢失

把配置文件/etc/redis.conf文件中

#requirepass foobared 的注释去掉, 把foobared替换成自己的密码

使用 /usr/bin/redis-server /etc/redis.conf 启动redis

有了密码之后再使用cli需要加-a参数

[root@32a6cc8ef5ed redis]# /usr/bin/redis-cli -a test12345
127.0.0.1:6379> config get requirepass  
1) "requirepass"
2) "test12345"
127.0.0.1:6379>