一、簡介
Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件。 它支持多種類型的數據結構,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與範圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內置了 複製(replication),LUA腳本(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不同級別的 磁盤持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)。[簡介來源Redis中文官網]
二、Windows下安裝
-
Redis下載
由於Redis還沒正式支持Windows,但由於其是開源的,微軟開放技術小組開發並維護了針對Win64的Windows接口程序,提供了一個windows版本的redis分支,目前已經不進行更新維護了。
-
創建目錄(目錄隨意,按照習慣,我放到了D盤:
D:\Redis
)用於存放解壓後的文件
解壓後的內容如下:
-
打開cmd窗口,進入
D:\Redis
目錄(剛纔的安裝目錄)下C:\Users>D: D:\>cd Redis D:\Redis>
或者直接在剛纔的
D:\Redis
目錄下直接進入cmd窗口
-
運行Redis
輸入命令redis-server.exe redis.windows.conf
D:\Redis>redis-server.exe redis.windows.conf [9380] 21 Dec 20:12:21.137 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server.exe /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 9380 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [9380] 21 Dec 20:12:21.143 # Server started, Redis version 3.2.100 [9380] 21 Dec 20:12:21.143 * DB loaded from disk: 0.000 seconds [9380] 21 Dec 20:12:21.143 * The server is now ready to accept connections on port 6379
1.可以把 redis 的路徑(安裝路徑:
D:\Redis
)加到系統的環境變量裏,這樣就可以在任意目錄下啓動了
2.後面的那個redis.windows.conf
可以省略,會啓用默認的
-
簡單使用
(1)另啓一個 cmd 窗口,原來的不要關閉,不然就無法訪問服務端了
(2)進入redis目錄下(添加過環境變量的不需要進入redis目錄)
(3)連接Redis服務器redis-cli.exe -h 127.0.0.1 -p 6379
(4)設置鍵值對
set myKey myValue
(5)取出鍵值對
get myKey
三、Linux(Centos7)下安裝
-
Redis下載
下載地址:https://redis.io/download,最好下載最新穩定版本,我下載的時候最新版本爲
5.0
(1)獲取對應版本的下載鏈接
(2)使用wget下載Redis安裝包[root@localhost Redis]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz [root@localhost Redis]# tar xzf redis-5.0.7.tar.gz [root@localhost Redis]# cd redis-5.0.7/ [root@localhost redis-5.0.7]# make
-
啓動redis的三種方式
第一種方式:直接啓動Redis[root@localhost redis-5.0.7]# ./src/redis-server
這種方式啓動Redis 使用的是默認配置。也可以通過啓動參數告訴Redis使用指定配置文件使用下面命令啓動:
./src/redis-server ./redis.conf
第二種方式:以後臺進程方式啓動Redis
修改redis.conf文件
將daemonize no
修改爲daemonize yes
[root@localhost redis-5.0.7]# ./src/redis-server ./redis.conf
查看進程:
ps -aux | grep redis
關閉進程:kill 進程id
第三種方式:設置Redis開機自啓動
首先必須確保將daemonize no
修改爲daemonize yes
,修改方法請看第二種方式
在/etc目錄下新建redis目錄[root@localhost redis-5.0.7]# cd /etc/ [root@localhost etc]# mkdir redis
將
安裝目錄
下的redis.conf 文件複製一份到/etc/redis目錄下,並命名爲6379.conf[root@localhost etc]# cd redis/ [root@localhost redis]# cp /home/Redis/redis-5.0.7/redis.conf /etc/redis/6379.conf
將Redis的啓動腳本複製一份放到/etc/init.d目錄下,並重命名爲
redisd
[root@localhost redis]# cd /etc/init.d/ [root@localhost init.d]# cp /home/Redis/redis-5.0.7/utils/redis_init_script /etc/init.d/redisd
修改
redisd
文件裏的兩個路徑[root@localhost init.d]# vim redisd
我的Redis安裝路徑爲
/home/Redis/redis-5.0.7
,所以我修改爲:
此處如果不進行修改,在啓動的時候會報錯:/etc/init.d/redisd: line 28: /usr/local/bin/redis-server: No such file or directory,詳細報錯內容請參考:報錯(/etc/init.d/redisd: line 28: /usr/local/bin/redis-server: No such file or directory)的解決辦法
設置redis開機自啓動
[root@localhost init.d]# chkconfig redisd on
現在可以直接以服務的形式啓動和關閉redis了
啓動:service redisd start
[root@localhost init.d]# service redisd start Starting Redis server... 14200:C 09 Dec 2019 21:17:50.133 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 14200:C 09 Dec 2019 21:17:50.133 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=14200, just started 14200:C 09 Dec 2019 21:17:50.133 # Configuration loaded [root@localhost init.d]#
關閉:
service redisd stop
[root@localhost init.d]# service redisd stop Stopping ... Redis stopped [root@localhost init.d]#
-
簡單使用
[root@localhost init.d]# cd /home/Redis/redis-5.0.7/ [root@localhost redis-5.0.7]# src/redis-cli 127.0.0.1:6379> set myKey myValue OK 127.0.0.1:6379> get myKey "myValue" 127.0.0.1:6379>
常用操作命令:
啓動客戶端:src/redis-cli
設置鍵值對:set 鍵 值
通過鍵獲取值:get 鍵