CentOS6.5安裝redis3.2.1

1、安裝redisredis

一、下載redis包bash

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

二、解壓redis包到/opt下tcp

tar -zxvf /home/redis-3.2.1.tar.gz -C /opt

三、安裝並測試redis測試

cd /opt/redis-3.2.1/src
make && make install
make test

備註:若make test報錯,提示安裝tcl,則進行tcl的安裝;ui

2、配置redis加密

一、複製並修改配置文檔命令行

cp /opt/redis-3.2.1/redis.conf /etc/
vi /etc/redis.conf
#開啓redis遠程訪問
#將 bind 127.0.0.1改成 bind 0.0.0.0
#開啓redis守護進程
#根據需求,可配置redis是否進行持久化:修改save策略
#添加密碼訪問 requirepass password

二、將redis配置進系統服務rest

vi /etc/init.d/redis

/etc/init.d/redis內容以下code

#chkconfig:   2345 90 10  
  
# description:  Redis is a persistent key-value database  
  
###########################  
PATH=/usr/local/bin:/sbin:/usr/bin:/bin  
     
REDISPORT=6379  
EXEC=/usr/local/bin/redis-server  
REDIS_CLI=/usr/local/bin/redis-cli  
     
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis.conf"
PASSWORD=password
     
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 does not exist, process is not running"  
        else  
                PID=$(cat $PIDFILE)  
                echo "Stopping ..."  
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                #如有密碼則加上密碼
                #$REDIS_CLI -a $PASSWORD -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|force-reload}" >&2  
        exit 1  
esac  
##############################
#給redis文件加上執行權限
chmod +x /etc/init.d/redis

三、設置redis服務開機啓動server

chkconfig redis on

四、在防火牆中開放redis端口

vi /etc/sysconfig/iptables
#加入下面的端口配置
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
#重啓防火牆
service iptables restart

五、啓動、關閉和重啓redis服務

service redis start
service redis stop
service redis restart

3、使用redis

#進入redis命令行
redis-cli -a password
相關文章
相關標籤/搜索