Install and configure Redis service under CentOS7

Official website: https://redis.io

Download: https://redis.io/download

1. Installation dependency

➜  yum install -y gcc gcc-c++ kernel-devel

2. Download the source package

# It is recommended to store the source code of each software in this directory
➜  cd /usr/local/src

# Download the specified version
➜  wget http://download.redis.io/releases/redis-5.0.5.tar.gz

# Download the latest stable version
➜  wget http://download.redis.io/redis-stable.tar.gz
# View specific version of source code
➜  cat redis-stable/src/version.h

3. Compile and install

➜  tar zxvf redis-5.0.5.tar.gz
➜  cd redis-5.0.5
➜  make

# Install to specified directory
➜  make PREFIX=/usr/local/redis install

# Copy the default profile to the specified directory
➜  mkdir /usr/local/redis/etc/
➜  cp redis.conf /usr/local/redis/etc/

# Copy executable program to system directory
➜  cd /usr/local/redis/bin/
➜  cp redis-benchmark redis-cli redis-server /usr/bin/

# Configure environment variables
➜  echo 'export PATH="$PATH:/usr/local/redis/bin"' >> /etc/profile
➜  source /etc/profile

# Verification
➜  redis-cli -v
redis-cli 5.0.5

4. Modify the default configuration file

Here are only some recommended common basic configurations. For details, please refer to the description of each configuration item in the configuration file, or refer to here https://www.runoob.com/redis/redis-conf.html

➜  vi /usr/local/redis/etc/redis_6379.conf

# Start the daemons (background) mode to run
daemonize yes

# Process file
pidfile /var/run/redis_6379.pid

# Only the specified host connection is allowed, and all networks are default
bind 127.0.0.1

# Modify port number, default 6379
port 6379

# How long does the client idle (unit: s) to close the connection
# Default 0, unlimited
timeout 300

# Log level, default notice      
loglevel verbose

# Logging mode, default to standard output (stdout)
# If running as a daemons, and standard output is used here, the log is sent to / dev/null
logfile stdout

# Set connection password
requirepass <Password>

5. Start service

Basic startup mode

# Start with default configuration
redis-server 

# Specify profile
redis-server /usr/local/redis/etc/redis.conf

# See more usage parameters
redis-server -h

Start with script

➜  cd redis-5.0.5/utils
➜  cp redis_init_script /etc/init.d/
➜  mv /etc/init.d/redis_init_script /etc/init.d/redis_6379
➜  vim /etc/init.d/redis_6379
#!/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

# Modify the port number and path according to the actual installation

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis_${REDISPORT}.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
        ;;
    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
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

Start service test with script

➜  ./redis_6379 start
Starting Redis server...
19261:C 08 Nov 2019 01:42:46.848 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19261:C 08 Nov 2019 01:42:46.848 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=19261, just started
19261:C 08 Nov 2019 01:42:46.848 # Configuration loaded

# Check whether the startup is successful
➜  ps -ef | grep redis
root     19262     1  0 01:42 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root     19267 19129  0 01:43 pts/0    00:00:00 grep --color=auto redis

# Client connection test
➜  redis-cli
127.0.0.1:6379&gt; KEYS *
(empty list or set)
127.0.0.1:6379&gt; exit

# Out of Service
➜  ./redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@cnetos7-localhost init.d]#

6. Add power on auto start

# Use root user action

# Add to self starting list
# The redis file name here is consistent with the / etc / init.d/redis file name
➜  chkconfig --add redis_6379

# Set level 2 3 4 5 to self start
➜  chkconfig --level 2345 redis_6379 on

# Restart to check whether the self start is effective
➜  reboot

You can also use the following CentOS7 + commands for unified management:

# View service status
➜  systemctl status redis_6379
● redis_6379.service - LSB: Redis data structure server
   Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
   Active: active (running) since Mon 2019-11-11 02:21:03 UTC; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1042 ExecStop=/etc/rc.d/init.d/redis_6379 stop (code=exited, status=0/SUCCESS)
  Process: 1056 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/redis_6379.service
           └─1058 /usr/local/redis/bin/redis-server 127.0.0.1:6379

Nov 11 02:21:03 cnetos7-localhost systemd[1]: Starting LSB: Redis data structure server...
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: Starting Redis server...
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # oO0OoO0OoO0Oo Re...0Oo
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Redis version=5....ted
Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Configuration loaded
Nov 11 02:21:03 cnetos7-localhost systemd[1]: Started LSB: Redis data structure server.
Hint: Some lines were ellipsized, use -l to show in full.

# Startup service
➜  systemctl start redis_6379

# Shut down service
➜  systemctl stop redis_6379
```</Password>

Keywords: Programming Redis yum vim Linux

Added by mcl on Mon, 11 Nov 2019 18:04:03 +0200