1. 程式人生 > 其它 >redis系列一:下載與安裝

redis系列一:下載與安裝

redis是什麼?

Redis(官網 https://redis.io/) 是一個開源(BSD 許可)的記憶體資料結構儲存,用作資料庫、快取和訊息代理。 Redis 提供資料結構,例如字串、雜湊、列表、集合、具有範圍查詢的排序集合、點陣圖、超日誌、地理空間索引和流。 Redis 具有內建複製、Lua 指令碼、LRU 驅逐、事務和不同級別的磁碟永續性,並通過 Redis Sentinel 和 Redis Cluster 自動分割槽提供高可用性。

下載指引

下載地址:https://redis.io/download
每次進入都會顯示當前最新的版本,例如當前最新的版本是7.0-rc1,如果想下載5.0歷史版本,需要在頁面找到 【Other versions】,選擇download5.0.14則可以下載

6.0.16下載地址:https://download.redis.io/releases/redis-6.0.16.tar.gz
5.0.14下載地址:https://download.redis.io/releases/redis-5.0.14.tar.gz
所有版本地址:https://download.redis.io/releases/

centos下載和安裝

在centos版本是7.6 和 8.2都測試過,其他版本慎用本教程
下面使用centos7.6版本安裝redis5.0.14

1. 下載

在 /usr/local下執行

wget https://download.redis.io/releases/redis-5.0.14.tar.gz

如果顯示:

-bash: wget: command not found,

表示當前環境還沒有安裝wget,執行

yum -y install wget

進行安裝
下載完後

-rw-r--r--. 1 root root 2000179 Oct  4 19:08 redis-5.0.14.tar.gz

2. 解壓

執行指令

tar -zxvf redis-5.0.14.tar.gz 

3. 檢查gcc版本

redis的安裝需要依賴gcc編譯器,安裝gcc或者將版本升級到最新,執行指令

yum -y install gcc tcl

如果安裝redis的gcc版本需要大於5.x版本,需要按順序執行

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

4. 編譯

因為我的redis解壓後目錄就在 /urs/local下,所以我就進入redis後執行編譯命令

make

如果解壓目錄不是在/usr/local下,又想安裝在/usr/local/redis下,就進入redis目錄後執行編譯命令

make PREFIX=/usr/local/redis install

編譯過程需要1-2分鐘時間,成功後的結果有一行日誌:

make[1]: Leaving directory /usr/local/redis-5.0.14/src

說明redis已經編譯好了
編譯後我的redis目錄如下

[root@localhost redis-5.0.14]# ll
total 352
-rw-rw-r--.  1 root root 127554 Oct  4 18:58 00-RELEASENOTES
-rw-rw-r--.  1 root root     53 Oct  4 18:58 BUGS
-rw-rw-r--.  1 root root   2381 Oct  4 18:58 CONTRIBUTING
-rw-rw-r--.  1 root root   1487 Oct  4 18:58 COPYING
drwxrwxr-x.  6 root root    192 Feb 27 23:06 deps
-rw-rw-r--.  1 root root     11 Oct  4 18:58 INSTALL
-rw-rw-r--.  1 root root    151 Oct  4 18:58 Makefile
-rw-rw-r--.  1 root root   6888 Oct  4 18:58 MANIFESTO
-rw-rw-r--.  1 root root  20555 Oct  4 18:58 README.md
-rw-rw-r--.  1 root root  63086 Feb 27 23:15 redis.conf
-rw-r--r--.  1 root root  63088 Feb 27 23:08 redis.conf.bak
-rwxrwxr-x.  1 root root    275 Oct  4 18:58 runtest
-rwxrwxr-x.  1 root root    280 Oct  4 18:58 runtest-cluster
-rwxrwxr-x.  1 root root    373 Oct  4 18:58 runtest-moduleapi
-rwxrwxr-x.  1 root root    281 Oct  4 18:58 runtest-sentinel
-rw-rw-r--.  1 root root   9710 Oct  4 18:58 sentinel.conf
drwxrwxr-x.  3 root root   8192 Feb 27 23:07 src
drwxrwxr-x. 11 root root    182 Oct  4 18:58 tests
drwxrwxr-x.  8 root root   4096 Oct  4 18:58 utils

5. 修改配置檔案

在redis目錄下找到redis.conf檔案,這個檔案配置著啟動的資訊

# 哪個ip可以訪問,預設是127.0.0.1修改為0.0.0.0沒有限制
bind 0.0.0.0
#預設是設定成yes的, 防止了遠端訪問,修改為no
protected-mode no
#守護執行緒啟動,即後臺執行,預設是no,修改為yes
daemonize yes
#埠,不修改預設是6379
port 6379

6. 執行

在redsi根目錄下執行

./src/redis-server redis.conf

執行成功後顯示

[root@localhost redis-5.0.14]# ./src/redis-server redis.conf
19718:C 27 Feb 2022 23:17:06.393 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19718:C 27 Feb 2022 23:17:06.393 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=19718, just started
19718:C 27 Feb 2022 23:17:06.393 # Configuration loaded

7. 檢查是否已經啟動

在redis根目錄下執行

./src/redis-cli

顯示如下則說明已經後臺執行redis,如需退出當前客戶端,執行quit回車即可

[root@localhost redis-5.0.14]# ./src/redis-cli 
127.0.0.1:6379> 

開機啟動

1. 前置條件

redis服務的啟動檔案redis-server在/usr/local/redis-5.0.14/src目錄下
啟動的配置檔案redis.conf在/usr/local/redis-5.0.14目錄下

2. 建立配置檔案

/usr/lib/systemd/system下執行

vi redisd.service

輸入內容

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis-5.0.14/src/redis-server /usr/local/redis-5.0.14/redis.conf
ExecReload=/usr/local/redis-5.0.14/src/redis-server -s reload
ExecStop=/usr/local/redis-5.0.14/src/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

儲存退出
執行開機啟動

systemctl enable redisd

3. 常用的命令

#啟動redis服務
systemctl start redisd

#重啟redis服務
systemctl restart redisd

#停止redis服務
systemctl stop redisd

#禁止開機自啟
systemctl disable redisd

#檢視狀態
systemctl status redisd