1. 程式人生 > >Centos 7.5安裝 Redis 5.0.0

Centos 7.5安裝 Redis 5.0.0

1 我的環境

 1.1 linux(騰訊雲)

CentOS Linux release 7.5.1804 (Core)

 1.2 Redis

Redis 5.0.0

2 下載

官網

官網下載地址

3 上傳

 3.1 使用xftp上傳到指定目錄,我的目錄為

/app/tool

4 解壓到安裝目錄

mkdir /usr/local/redis

tar zxf redis-5.0.0.tar.gz -C /usr/local/redis/

5 安裝 gcc 依賴

yum -y install gcc

6 編譯

 6.1 進入redis解壓目錄

cd /usr/local/redis/redis-5.0.0

 6.2 編譯

make MALLOC=libc

7 安裝

 7.1 進入redis/src下

cd /usr/local/redis/redis-5.0.0/src

 7.2 執行安裝

make install

8 配置(後臺啟動,設定密碼,外部訪問)

 8.1 修改配置檔案

vim /usr/local/redis/redis-5.0.0/redis.conf

進入vim 輸入 / 再按火車用來搜尋關鍵字

比如如下命令會定位到檔案中出現 bind 的位置並會將關鍵詞用顏色區分。

/bind

 8.2 後臺啟動

搜尋 daemonize no 改為 daemonize yes

daemonize yes

 8.3 設定密碼

搜尋 requirepass foobared 新增 requirepass 你設定的密碼

requirepass foobared

 8.4 外部訪問

搜尋 bind 127.0.0.1 然後註釋掉

# bind 127.0.0.1

9 啟動,停止和重啟

 9.1 啟動

  • 在任意目錄執行

    redis-server /usr/local/redis/redis-5.0.0/redis.conf

  • 啟動成功介面和程序

    ps aux | grep redis

  • 連線redis
    在任意目錄下執行 redis-cli 即可連線

    redis -cli

auth 密碼

 9.2 停止

  • 未設定密碼時

    redis-cli shutdown

  • 設定密碼後,執行以下命令

    redis-cli -a 密碼

  • 再執行停止和退出連線

    shutdown
    not connected> exit

 9.3 重啟

一般重啟都是殺掉程序,再啟動redis,這裡就介紹一下步驟,當然也可以執行上面的停止再啟動

ps aux | grep redis

[[email protected]_0_9_centos /]# ps aux | grep redis
root     29228  0.0  0.1 144004  2016 ?        Ssl  14:22   0:00 redis-server *:6379
root     29259  0.0  0.0 112704   972 pts/2    S+   14:22   0:00 grep --color=auto redis

kill -9 29228

ps aux | grep redis

[[email protected]_0_9_centos /]# ps aux | grep redis
root     29538  0.0  0.0 112704   968 pts/2    S+   14:25   0:00 grep --color=auto redis

10 開機啟動(可以忽略)

 10.1 複製redis服務檔案 redis_init_script

cp /usr/local/redis/redis-5.0.0/utils/redis_init_script /etc/init.d/redis

 10.2 在 etc 下建立 redis 資料夾

mkdir /etc/redis

 10.3 複製redis配置檔案 redis.conf 並重命名為6379.conf

cp /usr/local/redis/redis-5.0.0/redis.conf /etc/redis/

mv /etc/redis/redis.conf 6379.conf

 10.3 修改redis服務檔案

vim /etc/init.d/redis

  • 在/etc/init.d/redis檔案的頭部新增下面兩行註釋程式碼,也就是在檔案中#!/bin/sh的下方新增
# chkconfig: 2345 10 90  
# description: Start and Stop redis
  • 指定redis的安裝路徑和pid檔案路徑
REDISPORT=6379
EXEC=/usr/local/redis/redis-5.0.0/src/redis-server  # 指定的執行路徑
CLIEXEC=/usr/local/redis/redis-5.0.0/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf" # 這裡就是上面重新命名的配置檔案
  • 若設定了密碼,則需要修改stop指令碼
stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a 你的密碼 -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
  • 停止輸出結果
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped

 10.4 現在的redis命令

  • 開啟redis命令

    service redis start

  • 關閉redis命令

    service redis stop

  • 設為開機啟動

    chkconfig redis on

  • 設為開機關閉

    chkconfig redis off