CentOS8系统Redis单机安装

安装依赖包

sudo yum -y install gcc automake autoconf libtool make tcl

安装Redis

wget http://download.redis.io/redis-stable.tar.gz
sudo cp redis-stable.tar.gz /opt
cd /opt
sudo tar zxvf redis-stable.tar.gz
sudo mv redis-stable redis
cd /opt/redis
make
make test
sudo make install

配置Redis

创建Redis配置目录和数据目录

sudo mkdir /etc/redis
sudo mkdir -p /opt/redis-data/6379

复制启动脚本

sudo cp utils/redis_init_script /etc/init.d/redis

编辑启动脚本

sudo vi /etc/init.d/redis

根据情况修改REDISPORT。pid文件路径和配置文件名取决于端口号。

复制配置文件

sudo cp /opt/redis/redis.conf /etc/redis/6379.conf

修改配置文件

sudo vi /etc/redis/6379.conf

更改以下配置

# 后台启动
daemonize yes
# 设置端口
port 6379
# 设置pidfile
pidfile /var/run/redis_6379.pid
# 注释bind,允许远程访问
# bind 127.0.0.1
# 修改密码
requirepass 您的密码
# 关闭protected-mode模式,允许外网访问
protected-mode no
# 设置日志级别
loglevel notice
# 设置日志文件
logfile /var/log/redis_6379.log
# 设置redis数据存储目录
dir /opt/redis-data/6379

如果redis有设置密码,则/etc/init.d/redis需要增加密码:

#!/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

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

PASSWORD=nanjun

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 -a $PASSWORD shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

设置开机启动:

sudo chkconfig --add redis

开放端口

sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
sudo firewall-cmd --reload

启动redis

systemctl start redis

未经允许不得转载:君子如兰 » CentOS8系统Redis单机安装

赞 (0)