Ubuntu 16.04 安裝 Redis

簡介

Redis是一個開源(BSD許可),內存存儲的數據結構服務器,是一個高性能的key-value數據庫,高速緩存和消息隊列代理。它支持字符串、哈希表、列表、集合、有序集合,位圖,hyperloglogs等數據類型。內置複製、Lua腳本、LRU收回、事務以及不一樣級別磁盤持久化功能,同時經過Redis Sentinel提供高可用,經過Redis Cluster提供自動分區。redis

下載與安裝

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

服務管理

經過指定配置文件啓動shell

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服務bash

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

拷貝配置文件ide

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

修改目錄/etc/redis/下面的redis.conf文件,將如下內容性能

> daemonize no
> logfile ""

修改成:ui

#打開後臺運行選項
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
相關文章
相關標籤/搜索