Steps for Redis installation and self starting configuration on CentOS systems

  • 2020-05-10 23:09:46
  • OfStack

1. Install Redis

The installation of Redis is actually quite simple. The recommended way is to download the source code of redis and install it after native compilation.

Enter the download directory of the main folder for the first time and execute wget to download the source code


[zhxilin@localhost ~]$ cd  download 
[zhxilin@localhost  download ]$ wget http://download.redis.io/redis-stable.tar.gz

After unzip, move to /usr/redis


[zhxilin@localhost  download ]$ tar -zxvf redis-stable.tar.gz 
[zhxilin@localhost  download ]$ su mv redis-stable /usr/redis

Then enter the redis directory, execute the make command, and compile the redis source code


[root@localhost  download ]# cd /usr/redis/
[root@localhost redis]# make

After compiling, there are two important programs in the src directory, one is redis-server The other one is redis-cli ; Then go to the src directory and execute make install At this point, the executable will be copied to /usr/local/bin Directory, because /usr/local/bin Is defined under the system's environment variable $PATH, so the terminal can execute from anywhere redis-server and redis-cli .


[root@localhost redis]# cd src/
[root@localhost src]# make install

The installation of redis is now complete.

Let's take a look at what the compiled programs do:

      redis-server : as the name implies, redis service

      redis-cli : redis client, providing an redis client for connecting to redis service, adding, deleting, changing and checking

      redis-sentinel : monitoring management, notification, and instance failure backup services for redis instances

      redis-cli0 : performance testing tool for redis

      redis-check-aof : if the log is generated in AOF mode, it is used for quick repair when an accident occurs

      redis-check-rdb : if the log is generated in RDB mode, it is used for quick repair when an accident occurs

When the   installation is complete, start redis-server And run redis-cli test


[zhxilin@localhost ~]$ redis-server

[zhxilin@localhost ~]$ redis-cli 
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> 

This indicates that the redis service is working properly, and runs if the redis service is not started redis-cli When the newspaper Could not connect to Redis at 127.0.0.1:6379: Connection refused The error.

2. Configure startup

In order for redis-server to run automatically at system startup, you need to run the redis service as a daemon (daemon) /usr/redis/ Find 1 in the directory redis.conf This file is the configuration loaded by the redis service runtime. Let's observe the contents of it first


[zhxilin@localhost redis]$ vi redis.conf 

This file is very long, but mostly comments, and we'll focus on a few Settings daemonize and pidfile :

Among them daemonize The default is false, pidfile The default value is pidfile /var/run/redis_6379.pid

So the first one says daemon, so obviously we're going to change it to daemon daemonize yes ;

The second means that when the service is running as a daemon, redis writes pid by default /var/run/redis_6379.pid File, which exists during the service operation, is automatically deleted when service 1 stops the file, so it can be used to determine whether redis is running or not.

Save and exit.

With the basic configuration, redis also needs a script to manage startup, shutdown, and restart. redis source code actually provides an initialization script in the location /usr/redis/utils/redis_init_script .

Let's take a look at what this script does:


#!/bin/sh#

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"

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

The script specifies the port, server path, cli path, pidfile path, and conf path, all of which need to be configured correctly make install , so the script doesn't need to change much here, because make install Copy both server and cli /usr/local/bin The below.

Also see the conf path here, we need to copy the redis.conf file in the redis directory /usr/local/bin0


[root@localhost utils]# cd /etc
[root@localhost etc]# mkdir redis
[root@localhost etc]# cp /usr/redis/redis.conf /etc/redis/6379.conf 

then /usr/local/bin1 Copy the script to /usr/local/bin2


[root@localhost etc]# cp /usr/redis/utils/redis_init_script /etc/init.d/redisd 

The scripts under /etc/ init.d are all services that can be started automatically when the system is started, and now the configuration for one system startup is still missing:


[zhxilin@localhost  download ]$ tar -zxvf redis-stable.tar.gz 
[zhxilin@localhost  download ]$ su mv redis-stable /usr/redis
0

You will then notice that an error was reported: service redisd does not support chkconfig?

  this is because we need to be in /usr/local/bin1 Add a small change to the beginning:


[zhxilin@localhost  download ]$ tar -zxvf redis-stable.tar.gz 
[zhxilin@localhost  download ]$ su mv redis-stable /usr/redis
1

Save and copy again to /usr/local/bin2 After that, run again chkconfig And we're done.

Once the 1 cut is in place, you can verify that service was set up successfully by executing the following command:


[zhxilin@localhost  download ]$ tar -zxvf redis-stable.tar.gz 
[zhxilin@localhost  download ]$ su mv redis-stable /usr/redis
2

Is equivalent to


[root@localhost zhxilin]# /etc/init.d/redisd start 
[root@localhost zhxilin]# /etc/init.d/redisd stop

conclusion

Finally, restart the system for 1 time. After entering the system, directly run redis-cli to verify whether the redis service has been run automatically. The above is the entire content of this article, I hope the content of this article can bring 1 definite help to everyone's study or work, if you have any questions, you can leave a message to communicate.


Related articles: