Redis 3.2.8的安装及自启动

1. Redis 3.2.8安装

参考文档:https://redis.io/download

yum install gcc
yum install cpp
yum install binutils
yum install glibc-kernheaders
yum install glibc-common
yum install glibc-devel
yum install make
cd ~wqq
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar xzf redis-3.2.8.tar.gz
cd redis-3.2.8
make  #如果不行则把make换成MALLOC=libc;make test;make install

2. Redis 3.2.8的自启动

参考文档:http://futeng.iteye.com/blog/2071867?utm_source=tuicool&utm_medium=referral

2.1 拷贝配置文件

把redis的配置文件复制到指定目录(与自启动服务的脚本中的目录一致即可)

cd /etc
mkdir redis
cp ~wqq/redis-3.2.8/redis.conf redis/6379.conf

2.2 配置Redis作为守护进程运行

默认情况下,redis不是作为守护进行运行,而是在前台运行,需要修改配置文件

cd redis
vi 6379.conf
Paste_Image.png

修改为


Paste_Image.png

2.3 Redis的启动脚本(/etc/init.d/redis)

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO


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
        ;;
    status)
        if [ ! -f $PIDFILE ]
#       if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            PID=$(cat $PIDFILE)
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

备注:

cd /home/wqq/redis-3.2.8/utils
cp redis_init_script  /etc/init.d/redic

/home/wqq/redis-3.2.8/utils/redis_init_script是redis自带的启动脚本,但该脚本只能实现start、stop功能,还需在里面添加restart和status的功能。

2.4 设置Redis开机自启动

chkconfig --add redis
chkconfig --list |grep redis

推荐阅读更多精彩内容