redis environment deployment

Keywords: Linux Redis Database Tomcat Apache

Welcome to join the operation and maintenance development technology exchange group (QQ:722381733)

I. Redis Service Introduction:

Redis is simply a database, a database container used to store the cache. It is mainly designed to make the project data write into the cache and provide users with a more comfortable experience. Or it can also be understood as a software specially designed to extract database information slowly for projects with large number of visits. Of course, redis can also do distributed locks and other functions, which will not be covered here.

 

II. Redis deployment:

1. Go to the compressed package storage directory (download address: wget http://download.redis.io/releases/redis-3.2.0.tar.gz):

 

[root@web1 package]# ls
apache-tomcat-8.5.39.tar.gz  jdk-8u131-linux-x64.tar.gz  redis-3.2.0.tar.gz
[root@web1 package]# 

 

2. Decompress

[root@web1 package]# tar xf redis-3.2.0.tar.gz 
[root@web1 package]# ls
apache-tomcat-8.5.39.tar.gz  jdk-8u131-linux-x64.tar.gz  redis-3.2.0  redis-3.2.0.tar.gz

3. Go to the extracted redis directory and analyze it

[root@web1 package]# cd redis-3.2.0
[root@web1 redis-3.2.0]# make
cd src && make all
make[1]: Entering directory `/package/redis-3.2.0/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
(cd ../deps && make distclean)
make[2]: Entering directory `/package/redis-3.2.0/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd geohash-int && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true

4. Move the resolved directory to / usr/local /, and create a soft connection

[root@web1 redis-3.2.0]# mv /package/redis-3.2.0 /usr/local/
[root@web1 redis-3.2.0]# ln -s /usr/local/redis-3.2.0/ /usr/local/redis
[root@web1 redis-3.2.0]# cd /usr/local/
[root@web1 local]# ls
apache-tomcat-8.5.39  bin  etc  games  include  jdk  jdk1.8.0_131  lib  lib64  libexec  redis  redis-3.2.0  sbin  share  src  tomcat

5. Now you can go to the installation directory to start redis

[root@web1 local]# cd /usr/local/redis
[root@web1 redis]# src/redis-server ./redis.conf 
4610:M 23 May 00:15:54.657 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4610
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4610:M 23 May 00:15:54.698 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4610:M 23 May 00:15:54.698 # Server started, Redis version 3.2.0
4610:M 23 May 00:15:54.698 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4610:M 23 May 00:15:54.698 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4610:M 23 May 00:15:54.698 * The server is now ready to accept connections on port 6379

 

3. redis simple optimization:

1. Introduction to redis.conf configuration file (remove the file comments first)

[root@web1 redis]# cp redis.conf redis.conf.`date +%F`
[root@web1 redis]# sed -ri '/#|^$/d' redis.conf
[root@web1 redis]# cat redis.conf
bind 127.0.0.1  #Binding host address (here is the IP address of the host. If you want to make "single point" for multiple machines to access, directly {bind ip ip...}).
protected-mode yes
port 6379  #Appoint Redis Listening port. The default port is 6379.
tcp-backlog 511
timeout 0  #Close the connection after how long the client is idle. If 0 is specified, the function is closed.
tcp-keepalive 0
daemonize no  #Redis does not run as a daemons by default. It can be modified through this configuration item and daemons can be enabled by using yes. (Note: if you do not select Yes, if you directly write the startup command into / etc/rc.local, it will be directly stuck in the environment variable.).
supervised no
pidfile /var/run/redis.pid  #When Redis runs as a daemons, by default, Redis will write PID to / var/run/redis.pid file, which can be specified through pidfile.
loglevel notice  #Specify the logging level. Redis supports four levels: debug, verbose, notice, and warning. The default is notice.
logfile ""  #Configure the log storage path at startup. If it is empty, it means no logging.
databases 16  #Set the number of databases. The default database is 0. You can use SELECT <dbid>Command to specify a database on a connection id
save 900 1  #Specify the time and number of update operations to synchronize the data to the data file. Multiple conditions can be matched, redis The following three are provided by default save Configuration.
save 300 10
save 60 10000  #They represent 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes) and 10000 changes in 60 seconds, respectively.
stop-writes-on-bgsave-error yes
rdbcompression yes  #Specifies whether to compress the data when it is stored in the local database. The default value is yes. Redis uses LZF compression. If you want to save CPU time, you can turn off this option, but it will cause the database file to become huge.
rdbchecksum yes
dbfilename dump.rdb  #Specifies the local database file name. The default value is dump.rdb.
dir ./  #Specify the local database storage directory.
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no  #Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no.
appendfilename "appendonly.aof"  #Specifies the name of the update log file. The default is appendonly.aof
appendfsync everysec  #Specify the update log conditions. There are three optional values: no: indicates that the operating system will synchronize the data cache to the disk (fast), always: indicates that fsync() will be called manually after each update operation to write the data to the disk (slow, safe), and everysec: indicates that the data will be synchronized once per second (compromise, default)
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10

2. Simply modify the redis.conf configuration file

[root@web1 redis]# vim redis.conf

protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize yes
supervised no
pidfile /var/run/redis.pid
loglevel notice
logfile "/usr/local/redis/log/redis.log"
databases 16
......
##Others don't change###

3. Create a log directory in the redis directory to save the startup log, and then start redis

[root@web1 redis]# ./src/redis-server ./redis.conf

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 11
>>> 'logfile "./log/redis.log"'
Can't open the log file: No such file or directory  #Note: the log directory is not created here
[root@web1 redis]# ls
00-RELEASENOTES  CONTRIBUTING  deps      INSTALL   MANIFESTO  redis.conf             runtest          runtest-sentinel  src    utils
BUGS             COPYING       dump.rdb  Makefile  README.md  redis.conf.2019-05-23  runtest-cluster  sentinel.conf     tests
[root@web1 redis]# 
[root@web1 redis]# mkdir log
[root@web1 redis]# ./src/redis-server ./redis.conf

4. Check the port process

[root@web1 redis]# lsof -i:6379
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 4634 root    4u  IPv4  27306      0t0  TCP localhost:6379 (LISTEN)
[root@web1 redis]# ps -ef|grep 6379
root      4634     1  0 00:51 ?        00:00:00 ./src/redis-server 127.0.0.1:6379
root      4641  1286  0 00:55 pts/0    00:00:00 grep --color=auto 6379
[root@web1 redis]# netstat -lnutp|grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4634/./src/redis-se 

5. Deploy start-up

[root@web1 redis]# cd /etc/init.d/
[root@web1 init.d]# vim redis.sh 
#!/bin/sh
# chkconfig: 2345 10 90  
# description: Start and Stop redis   

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

PIDFILE=/run/redis.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
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac
####################After preservation################

[root@web1 init.d]# chmod +x redis.sh
[root@web1 init.d]# chkconfig --add redis.sh

6. View startup items

[root@web1 init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
redis.sh           0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@web1 init.d]# 

7. Restart redis

[root@web1 init.d]# /etc/init.d/redis.sh restart
Stopping ...
Redis stopped
Starting Redis server...
[root@web1 init.d]# lsof -i:6379
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 4777 root    4u  IPv4  29529      0t0  TCP localhost:6379 (LISTEN)
[root@web1 init.d]# 

###The configuration is completed here. If there is any problem with the service deployment, please help me to put it forward. Thank you!!! It is very important.

Posted by gabeg on Wed, 06 Nov 2019 09:40:04 -0800