linux安装redis并设置开机自启

首先去redis官网下载redis最新版本

官网地址:https://redis.io/

我下载的为6.2.6版本

 

 然后用tar命令解压至随意文件夹,然后进入该文件夹

进行编译(注意,此步骤必须要有gcc-c++依赖)   make

编译完成后在此文件夹生成 src 目录,进入该目录 命令为   cd ./src

进行安装(一般安装至/usr/local/redis)  make install PREFIX=/usr/local/redis

安装完成之后拷贝redis的配置文件:

首先在 /usr/local/redis  文件夹下新建一个名为 /config的文件夹 ,命令为   mkdir /usr/local/redis/config

把reids解压后的配置文件粘贴过去  命令为    cp ../redis.conf /usr/local/redis/config

对配置文件进行修改

vim /usr/local/redis/config/redis.conf

如果需要外网访问,修改  bind参数(默认只能127.0.0.1:6379进行访问,按自己需求更改)

如果需要后台运行,将daemonize no 改成daemonize yes

开启redis命令:    /usr/local/redis/bin/redis-server /usr/local/redis/config/redis.conf 

运行客户端      /usr/local/redis/bin/redis-cli -p 6379

配置开机自启动

首先,直接新建并打开文件   vim /etc/init.d/redis

填入如下代码(如果按我如上命令安装的redis则此代码不用修改,如不是,自己修改路径即可):

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

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/config/redis.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)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

然后保存,赋予权限    chmod 755 /etc/init.d/redis

然后尝试启动  

/etc/init.d/redis start
查看是否可以启动redis
之后配置开机自启命令即可:
chkconfig --add /etc/init.d/redis
chkconfig redis on


至此,圆满!!!

posted @ 2021-11-02 15:53  某某混子,名不虚传  阅读(625)  评论(2编辑  收藏  举报