Installing and Configuring Redis 8.0.0 on Linux

Operating System:openEuler 24.03 LTS SP1, CentOS-8, AlmaLinux, Rocky Linux, etc.Preparation Steps1. Disable SELinux

sestatus # Check status, 'disabled' indicates it has been disabled
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config # Disable SELinux
setenforce 0 # Temporarily disable
/usr/sbin/sestatus -v # Check SELinux status, 'disabled' indicates it is off

2. Firewall Configuration

2.1. Disable firewall:
systemctl stop firewalld.service # Stop firewall
systemctl disable firewalld.service # Prevent firewall from starting on boot
systemctl mask firewalld
systemctl stop firewalld
yum remove firewalld
2.2. Install iptables firewall
yum install iptables-services # Install
vi /etc/sysconfig/iptables # Edit firewall configuration file, open TCP port 2049
# Sample configuration for iptables service
# You can edit this manually or use system-config-firewall
# Please do not ask us to add additional ports/services to this default configuration
*filter:
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! # Save and exit
systemctl restart iptables.service # Finally, restart the firewall to apply the configuration
systemctl enable iptables.service # Set firewall to start on boot
/usr/libexec/iptables/iptables.init restart # Restart firewall

Installation Steps1. Download the installation package

Download link: https://download.redis.io/releases/redis-8.0.0.tar.gz
Upload the installation package to the server in the /usr/local/src directory

2. Install and upgrade GCC version

# Install GCC
yum -y install gcc gcc-c++   # Install GCC
yum -y install tcl tar telnet
# Redis source compilation requires GCC version >= 5
gcc -v  # Check GCC version

3. Install Redis

mkdir -p /data/server/redis  # Create installation directory
mkdir -p /data/server/redis/data  # Create data storage directory
mkdir -p /data/server/redis/log  # Create log directory
cd  /usr/local/src
tar  -zxvf  redis-8.0.0.tar.gz
cd  redis-8.0.0
make
make install  PREFIX=/data/server/redis

4. Configure Redis

cp  /usr/local/src/redis-8.0.0/redis.conf  /data/server/redis/redis.conf
vi /data/server/redis/redis.conf
daemonize yes  # Run Redis in daemon mode
pidfile /data/server/redis/redis_6379.pid
port 6379
bind 127.0.0.1 -::1
protected-mode yes
timeout 300 # Client timeout setting, in seconds
loglevel warning # Set log level, supports four levels: debug, verbose, notice, warning
logfile "/data/server/redis/log/redis.log"  # Log recording method, default is standard output, logs do not write to file, output to null device /dev/null
databases 16  # Number of databases enabled
save 900 1
save 300 10
save 60 10000
rdbcompression yes # Enable database lzf compression
dbfilename dump.rdb
dir "/data/server/redis/data"
requirepass 123456  # Set Redis database connection password
maxclients 10000 # Maximum number of client connections at the same time, 0 means unlimited
maxmemory 4096MB # Set the maximum memory Redis can use, value must be less than physical memory, must be set
appendonly yes  # Enable log recording, equivalent to MySQL's binlog
appendfilename "appendonly.aof"   # Log file name, note: not directory path
appendfsync everysec # Synchronize every second, there are two other parameters: always, no, generally set to everysec, equivalent to MySQL transaction log writing method
:wq! # Save and exit

5. Start Redis

# Start
/data/server/redis/bin/redis-server  /data/server/redis/redis.conf
# Enter console
/data/server/redis/bin/redis-cli -a 123456
AUTH 123456
shutdown
quit
# Close
/data/server/redis/bin/redis-cli -a 123456 shutdown

6. Add startup script

vi /data/server/redis/redis.sh
#!/bin/bash
# Application name
APP_NAME=redis
# Redis port
REDISPORT=6379
# Redis installation directory
DIR=/data/server/redis
# Redis process file
PIDFILE=/data/server/redis/redis_6379.pid
# Redis configuration file
CONF="/data/server/redis/redis.conf"
# Redis password
AUTH='123456'
# Usage instructions, used to prompt input parameters
usage() {
    echo "Usage: ./redis.sh [start|stop|restart|status]"
    exit 1
}
# Check if the program is running
is_exist() {
    if [ -f $PIDFILE ]
    then         pid=$(cat $PIDFILE)
    else pid=
    fi    # If it does not exist return 1, exists return 0
    if [ -z "${pid}" ]; then      return 1
    else      return 0
    fi
}
# Start method
start() {
   is_exist   if [ $? -eq "0" ]; then
     echo "${APP_NAME} is already running. pid=${pid} ."
   else
     echo "Starting Redis server..."
     $DIR/bin/redis-server  $CONF   fi
}
# Stop method
stop() {
   is_exist   if [ $? -eq "0" ]; then
        $DIR/bin/redis-cli -p $REDISPORT   -a  $AUTH  shutdown  2>/dev/null
        sleep 2
        while [ -x $PIDFILE ]
        do
            echo "Waiting for Redis to shutdown..."
            sleep 1
        done            echo "Redis stopped"
   else
     echo "${APP_NAME} is not running"
   fi
}
# Output running status
status() {
   is_exist   if [ $? -eq "0" ]; then
     echo "${APP_NAME} is running. Pid is ${pid}"
   else
     echo "${APP_NAME} is not running."
   fi
}
# Restart
restart() {
   stop   sleep 2   start
}
# Based on input parameters, choose to execute the corresponding method, if not input, execute usage instructions
case "$1" in   "start")     start     ;;   "stop")     stop     ;;   "status")     status     ;;   "restart")     restart     ;;   *)     usage     ;;esac
:wq! # Save and exit
chmod +x /data/server/redis/redis.sh  # Add execution permission
/data/server/redis/redis.sh start # Start Redis

Thus, the installation and configuration of Redis 8.0.0 on Linux is complete.

Leave a Comment