Ubuntu 16.04 安裝 Redis

劉校發表於2017-09-20

簡介

Redis是一個開源(BSD許可),記憶體儲存的資料結構伺服器,是一個高效能的key-value資料庫,快取記憶體和訊息佇列代理。它支援字串、雜湊表、列表、集合、有序集合,點陣圖,hyperloglogs等資料型別。內建複製、Lua指令碼、LRU收回、事務以及不同級別磁碟持久化功能,同時通過Redis Sentinel提供高可用,通過Redis Cluster提供自動分割槽。

下載與安裝

wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make
make isntall

服務管理

通過指定配置檔案啟動

sudo redis-server /opt/redis-4.0.1/redis.conf

22090:C 20 Sep 09:52:39.951 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22090:C 20 Sep 09:52:39.951 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=22090, just started
22090:C 20 Sep 09:52:39.951 # Configuration loaded
如果輸出如上資訊,證明Redis服務已經啟動成功。

通過命令redis-server 啟動

redis-server

關閉Redis服務

redis-cli shutdown

客戶端登陸並驗證

redis-cli

127.0.0.1:6379> set a b
OK
127.0.0.1:6379> get a
“b”
127.0.0.1:6379> exit

設定redis開機啟動

建立Redis配置目錄

sudo mkdir /etc/redis

拷貝配置檔案

sudo cp /opt/redis-4.0.1/redis.conf /etc/redis/

修改目錄/etc/redis/下面的redis.conf檔案,將以下內容

> daemonize no
> logfile ""

修改為:

#開啟後臺執行選項
daemonize yes
#設定日誌檔案路徑
logfile “/var/log/redis/redis.log”

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

PATH=/usr/local/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed."
        else
            echo "Starting Redis server..."
            $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
            echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is not running."
        else
            PID=$(cat $PIDFILE)
            echo "Stopping..."
            $REDIS_CLI -p $REDISPORT SHUTDOWN
            while [ -x $PIDFILE ]
            do
                echo "Waiting for Redis to shutdown..."
                sleep 1
            done
            echo "Redis stopped"
        fi
        ;;
    restart|force-reload)  
        ${0} stop  
        ${0} start
        ;;
    *)
        echo "Usage: /etc/init.d/redis {start|stop|restart|fore-reload}"
        exit 1
esac

問題一

insserv: warning: script `K01redis` missing LSB tags and overrides

解決方法
安裝一箇中文語言,系統就知道zh_CN.UTF-8了,這個時候用perl就不會報錯了

sudo apt-get install language-pack-zh-hans

問題二

insserv: warning: script `redis` missing LSB tags and overrides

解決方法
修改目錄/etc/init.d/下面的redis檔案,在檔案頭部#!/bin/sh下面新增如下內容:

### BEGIN INIT INFO
# Provides: OSSEC HIDS
# Required-Start: $network $remote_fs $syslog $time
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OSSEC HIDS
### END INIT INFO

使用指令碼啟動服務

  • 開啟redis:service redis start
  • 停止redis: service redis stop
  • 重啟redis: service redis restart
  • 檢視服務狀態:service redis status

相關文章