話不多說,直接開始。
一、安裝gcc依賴
redis 是用 C 語言開發,安裝之前必先確認是否安裝 gcc 環境(gcc -v
),如果沒有安裝,執行以下命令進行安裝。
$ yum install -y gcc
二、下載並解壓安裝包
$ wget http://download.redis.io/releases/redis-5.0.7.tar.gz
$ tar -zxvf redis-5.0.7.tar.gz
三、cd切換到redis解壓目錄下,執行編譯
$ cd redis-5.0.7 && make
四、安裝並指定安裝目錄
$ make install PREFIX=/usr/local/redis
五、啓動服務
5.1 前臺啓動
$ cd /usr/local/redis/bin/
$ ./redis-server
5.2後臺啓動
從 redis 的源碼目錄中複製 redis.conf 到 redis 的安裝目錄
$ cp /usr/local/redis-5.0.7/redis.conf /usr/local/redis/bin/
修改 redis.conf 文件,把 daemonize no 改爲 daemonize yes
$ vim redis.conf
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
後臺啓動
$ ./redis-server redis.conf
六、設置開機啓動
添加開機啓動服務
$ vim /etc/systemd/system/redis.service
添加下面內容
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意:ExecStart
配置成自己的路徑
設置開機啓動
$ systemctl daemon-reload
$ systemctl start redis.service
$ systemctl enable redis.service
創建 redis 命令軟鏈接
$ ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
# 測試
$ redis
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
最後,貼一下常用命令~
# 啓動redis服務
systemctl start redis.service
# 停止redis服務
systemctl stop redis.service
# 重新啓動服務
systemctl restart redis.service
# 查看服務當前狀態
systemctl status redis.service
# 設置開機自啓動
systemctl enable redis.service
# 停止開機自啓動
systemctl disable redis.service
OK~完事~