redis的安裝與使用

Redis版本:redis-3.0 
安裝目錄:/usr/local/redis
用戶:rootgit

編譯和安裝所需的包:
# yum install gcc tclgithub

下載3.0版Redis
# cd /usr/local/src
# wget https://github.com/antirez/redis/archive/3.0.0-rc5.tar.gzredis

建立安裝目錄:
# mkdir /usr/local/redis安全

解壓:
# tar -zxvf 3.0.0-rc5.tar.gz
# mv redis-3.0.0-rc5 redis3.0
# cd redis3.0tcp

安裝(使用PREFIX指定安裝目錄):
# make PREFIX=/usr/local/redis install
安裝完成後,能夠看到/usr/local/redis目錄下有一個bin目錄,bin目錄裏就是redis的命令腳本:
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-serverui

將Redis配置成服務:
按上面的操做步驟,Redis的啓動腳本爲:/usr/local/src/redis3.0/utils/redis_init_script
將啓動腳本複製到/etc/rc.d/init.d/目錄下,並命名爲redis:
# cp /usr/local/src/redis3.0/utils/redis_init_script /etc/rc.d/init.d/redis
編輯/etc/rc.d/init.d/redis,修改相應配置,使之能註冊成爲服務:
# vi /etc/rc.d/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.spa

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli3d

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"rest

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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esacserver

查看以上redis服務腳本,關注標爲橙色的幾個屬性,作以下幾個修改的準備:
(1)    在腳本的第一行後面添加一行內容以下:
#chkconfig: 2345 80 90
(若是不添加上面的內容,在註冊服務時會提示:service redis does not support chkconfig)
(2)    REDISPORT端口保持6379不變;(注意,端口名將與下面的配置文件名有關)
(3)    EXEC=/usr/local/bin/redis-server改成 EXEC=/usr/local/redis/bin/redis-server
(4)    CLIEXEC=/usr/local/bin/redis-cli 改成CLIEXEC=/usr/local/redis/bin/redis-cli
(5)    配置文件設置:
建立redis配置文件目錄
# mkdir /usr/local/redis/conf
複製redis配置文件/usr/local/src/redis3.0/redis.conf到/usr/local/redis/conf目錄並按端口號重命名爲6379.conf
# cp /usr/local/src/redis3.0/redis.conf /usr/local/redis/conf/6379.conf
作了以上準備後,再對CONF屬性做以下調整:
CONF="/etc/redis/${REDISPORT}.conf" 改成 CONF="/usr/local/redis/conf/${REDISPORT}.conf"
(6)    更改redis開啓的命令,之後臺運行的方式執行:
$EXEC $CONF &    #「&」做用是將服務轉到後面運行

修改後的/etc/rc.d/init.d/redis服務腳本內容爲:
#!/bin/sh
#chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/${REDISPORT}.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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

以上配置操做完成後,即可將Redis註冊成爲服務:
# chkconfig --add redis

防火牆中打開對應的端口
# vi /etc/sysconfig/iptables
添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
重啓防火牆:
# service iptables restart

修改redis配置文件設置:
# vi /usr/local/redis/conf/6379.conf
修改以下配置
daemonize no 改成> daemonize yes
pidfile /var/run/redis.pid 改成> pidfile /var/run/redis_6379.pid

啓動Redis服務
# service redis start

將Redis添加到環境變量中:
# vi /etc/profile
在最後添加如下內容:
## Redis env
export PATH=$PATH:/usr/local/redis/bin
使配置生效:
# source /etc/profile
如今就能夠直接使用redis-cli等redis命令了:
 

關閉Redis服務
# service redis stop

默認狀況下,Redis開啓安全認證,能夠經過/usr/local/redis/conf/6379.conf的requirepass指定一個驗證密碼。

若是設置了requirepass,就須要輸入auth huangkejie 進行校驗。

相關文章
相關標籤/搜索