Linux Redis安裝部署

安裝 Redis:

wget http://download.redis.io/releases/redis-2.8.24.tar.gz

tar xf redis-2.8.24.tar.gz

ln -s redis-2.6.14 redis #建立一個鏈接

cd redis

make PREFIX=/usr/local/redis install #安裝到指定目錄中

注意上面的最後一行,我們通過PREFIX指定了安裝的目錄。如果make失敗,一般是你們系統中還未安裝gcc,那麼可以通過yum安裝: 

yum -y install gcc*


將redis做成一個服務 

cp utils/redis_init_script /etc/rc.d/init.d/redis

vim /etc/rc.d/init.d/redis

在#!/bin/bash 下面加入以下代碼:

#chkconfig: 2345 80 90 

修改

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli 


在start)函數中修改:


$EXEC $CONF 

爲:

$EXEC $CONF &

保存退出

創建Redis的配置文件目錄:

mkdir /etc/redis

find / -name redis.conf 

grep "REDISPORT=" /etc/rc.d/init.d/redis

cp /soft/redis-2.8.24/redis.conf /etc/redis/6379.conf

chkconfig --add redis


將Redis的命令所在目錄添加到系統參數PATH中


修改profile文件:

vi /etc/profile

在最後加入:

export PATH="$PATH:/usr/local/redis/bin"


啓動Redis:

/usr/local/redis/bin/redis-server /etc/redis/6379.conf &



這樣就可以直接調用redis-cli的命令了,如下所示: 


$ redis-cli   

redis 127.0.0.1:6379> auth superman   

OK   

redis 127.0.0.1:6379> ping   

PONG   

redis 127.0.0.1:6379>


至此,redis 就成功安裝了。


如果在執行redis-cli中報錯:

[root@Redis redis]# redis-cli

127.0.0.1:6379> auth superman

(error) ERR Client sent AUTH, but no password is set


原因是redis沒有設置驗證口令!

解決方法:

設置Redis密碼:

    vim /etc/redis/redis.conf

# requirepass foobared

修改爲:

requirepass auth密碼


將redis寫成服務腳本

vim /etc/init.d/redis

#!/bin/sh

#

# Author: Zlyang

# Date  : 2016-6-14

#

# 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/bin/redis-server

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

PID=`ps -ef|grep 6379|grep -Ev "grep" |awk '{print $2}'`

PID_NUM=`ps -ef|grep 6379|grep -Ev "grep" |awk '{print $2}'|wc -l`

#PIDFILE=/var/run/redis_${REDISPORT}.pid

#CONF="/etc/redis/${REDISPORT}.conf"

CONF="/etc/redis/redis.conf"

function start()

 {

    if [ "$PID_NUM" != 0 ]

        then

                echo "Redis service is already running ..."

        else

                echo "Starting Redis server..."

                $EXEC $CONF 2>&1 > /dev/null &        

        sleep 1

        if [ `ps -ef|grep 6379|grep -Ev "grep" |awk '{print $2}'|wc -l` != 0 ]

                then

                        echo -e "Start Redis service.............................. [\E[1;32m OK \E[0m]"  

                fi

    fi

 }

function stop()

{

    if [ $PID_NUM == 0 ]

        then

                echo "Redis service is not running !"

        else

                echo "Waiting for Redis to stop ..."

                kill -9 $PID

                sleep 1

                echo -e "Stop Redis service............................... [\E[1;32m OK \E[0m]"

        fi

}

case "$1" in

    start)

    start

        ;;

    stop)

    stop

        ;;

    status)

    if [ "$PID_NUM" != 0 ]

    then

        echo "Redis service is already running ..."

    else

        echo "Redis service is stoped !" 

    fi

    ;;

    restart)

    if [ "$PID_NUM" != 0 ]

        then

        stop

        sleep 1

        echo "Starting Redis server..."

                $EXEC $CONF 2>&1 > /dev/null &

                sleep 1

                if [ `ps -ef|grep 6379|grep -Ev "grep" |awk '{print $2}'|wc -l` != 0 ]

                then

                        echo -e "Start Redis service.............................. [\E[1;32m OK \E[0m]"  

                fi

    else

        start

    fi

    ;;

    *)

        echo -e "\E[1;35m Usage: /etc/init.d/redos {start|stop|status|restart|}\E[0m"

        ;;

esac


保存退出:

將redis添加爲服務:

chkconfig --add redis

chkconfig redis on


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章