[Redis] install Redis for Ubuntu and add Redis to the system service to run

Introduction to Redis

Redis is a memory database with excellent performance, which is stored through the key value storage system. Similar to memcached, it supports relatively more stored value types, Including string, list, set, zset (sorted set – ordered list), and hashes (hash type). These data types support push/pop, add/remove, union, difference and richer operations, and these operations are atomic. On this basis, redis supports sorting in different ways. Like memcached, in order to ensure efficiency, data is cached in memory. The difference is that redis will periodically write updated data to memory Disk or write the modification operation to the additional record file, and on this basis, master-slave synchronization is realized.

Redis is a high-performance key value database. The emergence of redis largely compensates for the shortage of key/value storage such as memcached, and can play a good supplementary role to relational databases on some occasions. It provides Python, Ruby, Erlang and PHP clients, which is very convenient to use. The installation and configuration of redis is relatively simple.

Download and install

System environment

cat /etc/issue
Ubuntu 18.04.1 LTS \n \l

1. Download

Official stable version

wget http://download.redis.io/redis-stable.tar.gz
  • 1

2. Decompress

tar -zxvf redis-stable.tar.gz
cd redis-stable		# Switch directory

3. Installation

make PREFIX=/usr/local/redis install

4. Installation succeeded

After checking / usr/local/redis /, there is only one bin directory below, which contains six files:

redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

Run the redis server command

The following screen appears, that is, the operation is successful:

Register as a system service

1. Edit service script

To view the service script included with the installation package:

cat /Your download directory/redis-stable/utils/redis_init_script
 You will see the following:

Copy it to / etc / init D) directory and rename to redis:

sudo cp /Your download directory/redis-stable/utils/redis_init_script /etc/init.d/redis

Then modify / etc / init.exe according to the installation path of redis d/redis 

The contents of the document are:

#!/bin/sh  
#chkconfig: 2345 80 90  
# Simple Redis init.d script conceived to work on Linux systems  
# as it does use of the /proc filesystem.  
REDISPORT=6379  
REDISPATH=/usr/local/redis/bin
EXEC=${REDISPATH}/redis-server 
 
CLIEXEC=${REDISPATH}/redis-cli  
PIDFILE=/var/run/redis_${REDISPORT}.pid  
CONF=/usr/local/redis/redis.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 

Note the bold font:

  • $exec $conf &, followed by &, indicates that the service will be transferred to the background;
  • The paths of EXEC, CLIEXEC and CONF should be changed.

2. Copy profile

Redis.com provided in / your download directory / redis stable / * Copy the conf default configuration file to the redis installation directory and rename it to 6379 conf:

sudo cp /Your download directory/redis-stable/redis.conf /usr/local/redis/redis.conf

3. Environment variable settings

Append the following content to the / etc/profile file:

vim /etc/profile
export PATH=/usr/local/redis/bin:$PATH

Execution:

source /etc/profile

For the configuration to take effect immediately and verify with the following code:

echo $PATH

The printed content includes:

/usr/local/redis/bin:
  • 1

4. Service registration

Before 10.04, Ubuntu uses chkconfig when configuring the startup service, but there is no chkconfig command in later versions:

sudo chkconfig --add redis

sudo: chkconfig: command not found

The alternative to chkconfig , is , update RC d :

sudo update-rc.d redis defaults

If the service is registered successfully, there will be the following outputs: (sometimes not)

   /etc/rc0.d/K20redis -> ../init.d/redis
   /etc/rc1.d/K20redis -> ../init.d/redis
   /etc/rc6.d/K20redis -> ../init.d/redis

Service startup and verification

$ sudo service redis start


$ sudo service redis stop

Service startup, execution

$ redis-cli ping	
PONG

Indicates that the service is started.

$ netstat -an|grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

The local 6379 port number has been monitored by Redis.

Persistent files and logs

Persistence

The default persistence mode of Redis is snapshot mode. By default, the snapshot will be "dump" The RDB # file is placed in the startup directory (the service mode startup is in the root directory). We can change the dir configuration (line 246) under the snapshot item in the 6379.conf file and put the snapshot file in the specified directory to prevent it from being deleted by mistake:

dir ./
Change to
dir /usr/local/redis/

journal

Redis outputs the log to / dev/null by default (i.e. discard). We can keep the log to the specified file by changing the logfile configuration (line 162) under the GENERAL item in 6379.conf file:

logfile ""
Change to
logfile /url/local/redis/redis.log

Restart Redis.

Keywords: Redis

Added by iconicCreator on Thu, 30 Dec 2021 01:13:00 +0200