Redis 是什麼
開源的key-value的存儲服務系統,也可以把它理解爲是一種數據庫。
Redis 的特性
No | 特點 | 說明 |
---|---|---|
1 | 速度快 | 10w OPS,數據存放在內存中達到數據訪問很快,並且爲單進程 |
2 | 持久化 | Redis 所有數據保持在內存中,對數據的更新將異步保存在磁盤上!達到 斷電不怕丟數據 |
3 | 多種數據結構 | 開發常用的數據類型 |
4 | 支持多種編輯語言 | 支持開發常用的編程語言:Java、python、php、Ruby、Lua |
5 | 功能豐富 | 發佈訂閱、Lua腳本、事物功能 |
6 | 使用簡單 | |
7 | 主從複製 | 對於數據的保護 |
8 | 高可用、分佈式 | 2.8開始提供 Redis-Sentinel高可用。3.0開始支持Redis-Cluster分佈式 |
典型應用場景
特點 | 說明 |
---|---|
1. 緩存系統 | 內存緩存數據 |
2. 計數器 | 轉發量啊, 評論數啊 |
3. 消息隊列系統 | 類似中間件(卡夫卡),提供消息隊列 |
4. 排行榜功能 | 有序集合 |
5. 社交網絡 | 粉絲數、關注數、時間軸 |
6. 實時系統 | 垃圾郵件過濾器 |
Linux 平臺下的 Redis 安裝
項目 | 配置 |
---|---|
操作系統 | RHL 7.4 |
redis版本 | 5.0.8 |
系統需要安裝gcc
[root@rh74 ~]# yum install gcc*
開始安裝
[root@rh74 ~]# tar -xvf redis-5.0.8.tar
[root@rh74 redis]# ln -s redis-5.0.8 redis
[root@rh74 redis]# make && make install --編譯 及 安裝
爲了方便未來redis軟件升級、方便管理,我們會創建軟件所在目錄的軟連接。
看到如下的提示,說明安裝完成。
CC childinfo.o
CC defrag.o
CC siphash.o
CC rax.o
CC t_stream.o
CC listpack.o
CC localtime.o
CC lolwut.o
CC lolwut5.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/root/redis-5.0.8/src'
cd src && make install
make[1]: Entering directory `/root/redis-5.0.8/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-5.0.8/src'
make[1]: Entering directory `/root/redis-5.0.8/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/root/redis-5.0.8/src'
安裝完成之後,redis的命令可以直接使用,不需要配置環境變量。
啓動 Redis
redis 有三種啓動方式:
啓動模式 | 啓動方法 |
---|---|
最簡啓動 | [root@rh74 redis]# redis-serve r 使用默認端口號啓動redis服務,一切參數配置均爲默認值,啓動時log直接屏幕輸出。 |
動態參數啓動 | [root@rh74 ~]# redis-server --port 6380 可以指定端口號啓動redis服務,一切參數配置均爲默認值,啓動時log直接屏幕輸出。 |
配置文件啓動 (推薦) | 接下來詳細介紹部署+啓動方法 |
配置文件啓動方法
我們通常會在單機上部署多個redis,需要用端口號作爲區分。
- 獲取配置文件:根據系統自帶配置文件基礎上進行修改。
[root@rh74 redis]# mkdir config
[root@rh74 redis]# cp redis.conf config/
[root@rh74 redis]# cd config/
[root@rh74 config]# ll
total 64
-rw-r--r--. 1 root root 61797 May 14 05:08 redis.conf
[root@rh74 config]# cat redis.conf | grep -v "#" | grep -v "^$" > redis-7000.conf
[root@rh74 config]# ll
total 68
-rw-r--r--. 1 root root 1425 May 14 05:11 redis-7000.conf
-rw-r--r--. 1 root root 61797 May 14 05:10 redis.conf
- 不過本次我們以最簡單配置,僅留4項。
[root@rh74 config]# vi redis-7000.conf
daemonize yes
port 7000
dir "/root/redis/data"
logfile "7000.log"
- 創建所需目錄
[root@rh74 config]# cd ..
[root@rh74 redis]# mkdir data
正式啓動
[root@rh74 redis]# redis-server config/redis-7000.conf
驗證啓動
- 通過查看系統進程
因爲我分別用三個方法啓動過redis,所以後臺進程可以看到有三條:
[root@rh74 redis]# ps -ef | grep redis
root 9648 1800 0 05:03 pts/0 00:00:04 redis-server *:6379
root 9671 9652 0 05:06 pts/2 00:00:04 redis-server *:6380
root 9719 1 0 05:31 ? 00:00:00 redis-server *:7000
- log日誌已生成
[root@rh74 redis]# cd data
[root@rh74 data]# ls
7000.log
客戶端連接
其實就是進入redis軟件中進行操作,使用redis-cli
命令
[root@rh74 ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> exit
PS: 可執行文件說明
其實在安裝配置完redis 軟件之後,可以發現有這麼幾個目錄,我這裏分別給大家介紹一下:
[root@rh74 redis]# cd src/
[root@rh74 redis]# ls
目錄 | 說明 |
---|---|
redis-server | redis 服務器,用來啓動redis服務的。 |
redis-cli | redis 命令行客戶端,用來連接客戶端的 |
redis-banchmark | 性能測試 |
redis-check-aof | 修復aof工具,用於持久化功能 |
redis-check-dump | 修復rdb工具,用於持久化功能 |
redis-sentinel | Sentinel高可用 |