跳转至

centos7.9 编译安装redis-5.0.14

下载安装

Bash
wget https://download.redis.io/releases/redis-5.0.14.tar.gz
tar xzf /share/soft/redis-5.0.14.tar.gz -C /opt/

cd /opt/redis-5.0.14/
make
make install

cp utils/redis_init_script /etc/init.d/redis
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf

配置文件

vim /etc/redis/6379.conf

Bash
bind 0.0.0.0  # 如需远程访问
protected-mode no  # 如需远程访问
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "/var/log/redis_6379.log"
dbfilename dump.rdb
dir /opt/redis-5.0.14/data/   # 数据目录创建好
masterauth hd6xxxxxxxxx1kj
requirepass hd6xxxxxxxxx1kj
maxmemory 2g
appendonly yes
appendfilename "appendonly.aof"

启动脚本修改

vim /etc/init.d/redis

Bash
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PASS="-a hd6xxxxxxxxx1kj"

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"


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 $PASS -p $REDISPORT shutdown 2>/dev/null
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
}
status(){
        if [ -f $PIDFILE ]
        then
                pid=$(cat $PIDFILE)
                echo "Redis server runnind. PID: $pid ..."
        else
                echo "Redis server not running"
        fi
}
restart(){
    stop
    sleep 2s
    start
}

case "$1" in
    start)start;;
    stop)stop;;
    restart)restart;;
    status)status;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

启动服务

Bash
systemctl daemon-reload
systemctl start redis