centos安装redis3为系统服务

#无wget,请通过命令yum install wget安装
wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar xzf redis-3.0.1.tar.gz
cd redis-3.0.1
make #如果无make命令,请下载yum install gcc make
make test
make install

安装好了redis后,须注意拷贝bin文件到usr/local/bin(这步很重要,无这步会报错)

cp redis-server /usr/local/bin/
cp redis-cli  /usr/local/bin
之后 修改redis.conf,打开后台运行选项:
daemonize yes

编写启动shell:vim /etc/init.d/redis(copy网上的)

# chkconfig: 2345 10 90
# description: Start and Stop redis
 
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
 
REDISPORT=6379 #实际环境而定
EXEC=/usr/local/redis/src/redis-server #实际环境而定
REDIS_CLI=/usr/local/redis/src/redis-cli #实际环境而定
 
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/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 exists, 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
赋予权限chmod +x /etc/init.d/redis

设置开机启动:

# 尝试启动或停止redis
service redis start
service redis stop
 
# 开启服务自启动
chkconfig redis on

搞定。

你可能感兴趣的:(redis,centos)