1.安裝Redis之前,我們先介紹下Redis:
(1)Redis是什么?都有哪些特點?
概念:Redis (REmote DIctionary Server) 是用 C 語言開發的一個開源的高性能鍵值對(key-value)數據庫。是基於內存運行並支持持久化的、高性能的NoSQL數據庫,它可以用作數據庫、緩存和消息中間件。
特點:
- 支持數據持久化:Redis支持數據的持久化,可以將內存中的數據保持在磁盤中,重啟的時候可以再次加載進行使用。
- 支持多種數據結構:Redis不僅僅支持簡單的key-value類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
- 支持數據備份:Redis支持數據的備份,即master-slave模式的數據備份
(2)Redis應用場景:
- 熱點數據加速查詢(主要場景),如熱點商品、熱點新聞、熱點資訊、推廣類等高訪問量信息等
- 任務隊列,如秒殺、搶購、購票排隊等
- 即時信息查詢,如各位排行榜、各類網站訪問統計、公交到站信息、在線人數信息(聊天室、網站)、設備信號等
- 時效性信息控制,如驗證碼控制、投票控制等
- 分布式數據共享,如分布式集群架構中的 session 分離
- 消息隊列
- 分布式鎖
2.linux系統安裝Redis:
(1)因為Redis是用C語言來編寫的,所以安裝之前需要設置好gcc環境
安裝升級gcc:
yum -y install gcc automake autoconf libtool make //安裝make
yum -y install centos-release-scl //安裝SCL源
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils //安裝gcc
(1)下載redis(網址:http://download.redis.io):
wget -P /tmp http://download.redis.io/releases/redis-6.0.12.tar.gz //下載安裝包到/tmp目錄下
(2)創建一個單獨的Redis文件夾,對安裝包進行解壓:
sudo su - mkdir /opt/redis tar -C /opt/redis/ -xf /tmp/redis-6.0.12.tar.gz cd /opt/redis/redis-6.0.12 //進入解壓后的目錄 scl enable devtoolset-9 bash //切換最新的gcc版本 make //編譯 make install //安裝
如圖所示就代表安裝成功了!

3.Redis的啟動方式:
(1)直接啟動
redis-server
(2)根據自己想要的配置文件啟動
配置文件參考:
port 6379 protected-mode no tcp-backlog 4096 timeout 300 tcp-keepalive 60 daemonize yes supervised systemd logfile "/redis/redis.log" dir "/redis" maxclients 1000 maxmemory-policy volatile-lru appendonly no slowlog-max-len 512 client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 0 0 0 save 900 1 repl-backlog-size 100mb stop-writes-on-bgsave-error no
redis-server /opt/redis/redis-6.0.12/redis.conf (redis.conf可以復制到其他目錄進行修改屬性配置再啟動)
(3)根據systemctl 來啟動 :
touch redis.service //在 /usr/lib/systemd/system 目錄下創建redis.service
vim redis.service //對redis.service 文件進行編輯
添加內容:
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf //redis.conf 可以自己修改調整
ExecStop=/usr/local/bin/redis-cli -h 本機IP shutdown
User=redis
Group=redis
[Install]
WantedBy=multi-user.target
接下來可以通過systemctl 來對Redis進行操作了
systemctl start redis //啟動redis
systemctl stop redis //關閉redis