如何将redis3安装成Linux(centos7)系统服务,开机启动

10-31 22:32:41   来源:   评论: 点击:

如何在linux(centos7)上安装redis,设置redis为系统服务,并开机自启动
#首先下载redis安装包
wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar zxvf redis-3.0.1.tar.gz //解压
cd redis-3.0.1  //切换到解压目录
make && make test && make install 

=================安装过程中可能会遇到如下错误=============
异常一:
make[2]: cc: Command not found
异常原因:没有安装gcc
解决方案:yum install gcc-c++
 
异常二:
zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
异常原因:一些编译依赖或原来编译遗留出现的问题
解决方案:make distclean。清理一下,然后再make。
 
在make成功以后,需要make test。在make test出现异常。
异常一:
couldn't execute "tclsh8.5": no such file or directory
异常原因:没有安装tcl
解决方案:yum install -y tcl
===============================================

安装好了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

 

相关热词搜索:redis3 linux系统服务

上一篇:Redis有序集合类型的常用命令小结 下一篇:最后一页
分享到: 收藏