1. 程式人生 > >Linux(CentOS)中Redis介紹、安裝、使用【一篇就夠】

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


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