centos 安装redis自启动要点

时间:2022-04-06 20:38:05

1.redis.conf

  a.daemonize yes

  b.pidfile /var/run/xxx.pid

2./etc/init.d/redis

//加了下面三个注释部分,才支持设置开机自启动

#!/bin/sh
# 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-server所在目录 
REDIS_CLI=/usr/local/bin/redis-cli  //redis-cli所在目录
     
PIDFILE=/var/run/redis_6379.pid  //redis.conf中间定义的pidfile
CONF="/software/redis-stable/redis.conf"   //redis.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  
        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  
                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  
##############################

下载

  1. wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz
  2. tar -zxf redis-2.2.13.tar.gz
  3. cd redis-2.2.13
  4. make
  5. sudo make install
  6. cp redis.conf /etc

新建用户。数据和日志文件夹

  

  1. sudo useradd redis
  2. sudo mkdir -p /var/lib/redis
  3. sudo mkdir -p /var/log/redis
  4. sudo chown redis.redis /var/lib/redis #db文件放在这里,要修改redis.conf
  5. sudo chown redis.redis /var/log/redis

脚本设置

见上面

设置权限

chmod +x /etc/init.d/redis

设置自动启动:

  1. sudo chkconfig redis on

启动或者关闭服务

  1. service redis start   #或者 /etc/init.d/redis start
  2. service redis stop   #或者 /etc/init.d/redis stop

测试是否成功

     1。reboot

     2. redis-cli

     3.get xxxx