Compile and install Redis and manage using systemd

Environmental Science

OS redis version Firewall and selinux
CentOS7 4.0.14 Close

Installation steps

Pre-installation Equipment

1.because redis Yes C Written, so installation is required gcc
#yum -y install gcc redis Source Package
#wget -P /usr/local/src/

Compile Installation

#cd /usr/local/src/
#tar xf redis-4.0.14.tar.gz
#cd redis-4.0.14
#make PREFIX=/apps/redis install #PREFIX indicates the installation directory of the specified redis
#mkdir /apps/redis/{etc,logs,data,run} #Create directories for configuration files, logs, data, etc.
#cp redis.conf /apps/redis/etc/

Edit the redis service startup script to manage the service using systemd

Note: This script is taken from the service file generated by redis installed by yum and modified slightly

#cat > /usr/lib/systemd/system/redis.service <<EOF
Description=Redis persistent key-value database

ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd


Note: Turning off the redis service in this script is accomplished through another script/usr/libexec/redis-shutdown, which is included in the redis installed by yum. If compiled and installed redis do not have this script, the following is true:

#cat /usr/libexec/redis-shutdown 
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x


# Retrieve service name
if [ -z "$SERVICE_NAME" ]; then

# Get the proper config file based on service name

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port
if [ "$SERVICE_NAME" = redis ]; then

# Setup additional parameters
# e.g password-protected redis instances

# shutdown the service properly
if [ -e "$SOCK" ] ; then
#chmod +x /usr/libexec/redis-shutdown

Create redis users and set permissions

#groupadd -g 888 redis && useradd -r -u 888 -g 888 redis -s /sbin/nologin
#chown redis.redis -R /apps/redis/

Create Soft Connections

#ln -s /apps/redis/bin/* /usr/bin/
#ll /usr/bin/redis-*
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-benchmark -> /apps/redis/bin/redis-benchmark #redis performance test tool
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-check-aof -> /apps/redis/bin/redis-check-aof #AOF File Check Tool
lrwxrwxrwx 1 root root 31 Feb  9 21:34 /usr/bin/redis-check-rdb -> /apps/redis/bin/redis-check-rdb #RDB File Check Tool
lrwxrwxrwx 1 root root 25 Feb  9 21:34 /usr/bin/redis-cli -> /apps/redis/bin/redis-cli #redis Client Tool
lrwxrwxrwx 1 root root 30 Feb  9 21:34 /usr/bin/redis-sentinel -> /apps/redis/bin/redis-sentinel #Sentry, soft-connected to server
lrwxrwxrwx 1 root root 28 Feb  9 21:34 /usr/bin/redis-server -> /apps/redis/bin/redis-server #redis service start command

Change the directory path where rdb files are stored and set permissions

#sed -i '/^dir/s#./#/apps/redis/data#' /apps/redis/etc/redis.conf

Verify redis startup

Resolve redis errors and warnings

Failed opening the RDB file dump.rdb (in server root dir /) for saving: Permission denied

Reference material:

When executing the systemctl stop redis command, the above error was found in the / var/log/messages log because the data needs to be saved to the rdb file before redis can be closed, but the error is caused by permissions. The solution is as follows:
Specify the directory where the rdb file will be saved in the redis configuration file and ensure that the relevant permissions are correct
#ll /apps/redis/data/ -d
drwxr-xr-x 2 redis redis 22 Feb 11 20:33 /apps/redis/data/
#vim /apps/redis/etc/redis.conf
dir /apps/redis/data

Socket Maximum Connection

Reference material:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

backlog Parameters control when shaking hands three times server End Received client ack Queue value after confirmation number.

Memory Overcommit

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.

0:Indicates that the kernel will check to see if there is enough available memory for the process to use; if there is enough available memory, the memory request is allowed; otherwise, the memory request fails and an error is returned to the application process. 
1:Indicates that the kernel allows all physical memory to be allocated regardless of the current memory state. 
2:Represents that the kernel allows allocation of more memory than the sum of all physical and swap space

Since these two are related, resolve them together

#vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 1024

For these configurations to take effect, they need to be reloaded

#sysctl -p

Transparent Huge Pages

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.

//Large pages are allocated dynamically and redis need to be turned off for memory management.

Provisional Effectiveness

echo never > /sys/kernel/mm/transparent_hugepage/enabled

To fix this problem permanently, follow the log recommendations and modify rc.local

#vim /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
#chmod +x /etc/rc.d/rc.local

Tags: Linux Redis yum network vim

Posted on Tue, 11 Feb 2020 10:28:50 -0800 by tommyboy123x