Linux(CentOS)中Redis介绍、安装、使用【一篇就够】

一、介绍

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. 

二、下载 redis版本包到 CentOS上

curl -O http://download.redis.io/releases/redis-4.0.9.tar.gz


三、安装

mkdir redis 创建redis存放目录

mv redis-4.0.9.tar.gz reids 将安装包移动到redis存放目录下

cd redis 进入目录

tar -xvf redis-4.0.9.tar.gz 解压安装包

ls -lrt

扫描二维码关注公众号,回复: 1019819 查看本文章


ls -lrt redis-4.0.9


cd redis-4.0.9 进入redis-4.0.9

make 运行make命令

如果运行出现 make:gcc:Commond not found错误,一般出现这种错误是因为安装系统的时候使用的是最小化mini安装,系统没有安装make、vim等常用命令,直接yum安装下即可。

运行 yum -y install gcc automake autoconf libtool make 即可

make命令正常运行完毕后,在安装目录的src目录下可以看到服务程序redis-server和用于测试的客户端程序redis-cli等


四、使用

cd src 进入src目录

./redis-server 启动redis服务(这种方式使用的是默认配置,也可以通过启动参数告诉redis使用指定的配置文件)

如: ./redis-server redis.conf

启动后的界面如下:



通过另一台机器,登录客户端,使用客户端和redis服务交互

cd src

./redis-cli


set key value

get key


参考:http://www.runoob.com/redis/redis-install.html

五、windows端通过java程序访问虚拟机linux上的redis

1)修改redis.conf配置文件

vi redis.conf

注释掉 bind 127.0.0.1

bind的意思是绑定哪个ip地址能够访问服务 ,简单说bind指定的ip才可以访问redis server。
ps:
bind 127.0.0.1 //指定只有本机才能访问redis服务器
bind 0.0.0.0    // 所有的机子都可以访问到redis server
bind  192.168.1.253  //只有这个ip的机子才可以访问redis server


2)关闭linux防火墙

systemctl stop firewalld.service

3)java程序(虚拟机linux ip地址为:192.168.0.104)

import redis.clients.jedis.Jedis;

public class RedisJava {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.0.104");
        System.out.println("连接成功");

        //查看服务是否运行
        System.out.println("服务正在运行:" + jedis.ping());
    }
}
执行时报下面错误:

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.conf,去掉requirepass foobared前面的#,启用密码验证(密码为 foobared)


相应的在java程序中增加密码设置

import redis.clients.jedis.Jedis;

public class RedisJava {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.0.104");
        jedis.auth("foobared"); //输入redis密码
        System.out.println("连接成功");

        //查看服务是否运行
        System.out.println("服务正在运行:" + jedis.ping());
    }
}

重新启动redis,再次执行java程序,执行成功!

连接成功
服务正在运行:PONG

Redis Java String(字符串)实例

import redis.clients.jedis.Jedis;

public class RedisJava {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.0.104");
        jedis.auth("foobared"); //输入redis密码
        System.out.println("连接成功");

        //查看服务是否运行
        System.out.println("服务正在运行:" + jedis.ping());

        //设置redis字符串数据
        jedis.set("testK", "first redis value");
        //获取存储的数据并输出
        System.out.println("redis存储的字符串为:" + jedis.get("testK"));
    }
}

执行结果:

连接成功
服务正在运行:PONG

redis存储的字符串为:first redis value


六、最后推荐一个Redis在线测试网站,适合初学者使用

猜你喜欢

转载自blog.csdn.net/sjmz30071360/article/details/80287524