Redis的安裝與使用

來源:互聯網
上載者:User

標籤:服務指令碼   /proc   提升   編譯   make   href   dubbo   exe   http   

Redis使用的Demo範例建議可以參考下這個課程《基於Dubbo的分布式系統架構實戰》

IP:192.168.4.111

環境:CentOS 6.6

Redis版本:redis-3.0 (考慮到Redis3.0在叢集和效能提升方面的特性,rc版為正式版的候選版,而且很快就出正式版)

安裝目錄:/usr/local/redis

使用者:root

 

編譯和安裝所需的包:

# yum install gcc tcl

 

下載3.0版Redis(當前最新版redis-3.0.0-rc5.tar.gz,請學員們在安裝時自行選用最新版)

# cd /usr/local/src

# wget https://github.com/antirez/redis/archive/3.0.0-rc5.tar.gz

 

建立安裝目錄:

# mkdir /usr/local/redis

 

解壓:

# tar -zxvf 3.0.0-rc5.tar.gz

# mv redis-3.0.0-rc5 redis3.0

# cd redis3.0

 

安裝(使用PREFIX指定安裝目錄):

# make PREFIX=/usr/local/redis install

安裝完成後,可以看到/usr/local/redis目錄下有一個bin目錄,bin目錄裡就是redis的命令指令碼:

redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-server

 

將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.

 

REDISPORT=6379

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

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

 

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

CONF="/etc/redis/${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服務指令碼,關註標為橙色的幾個屬性,做如下幾個修改的準備:

(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指定一個驗證密碼。

Redis的安裝與使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.