Redis3 installation under Centos7

Redis official website http://redis.io

Chinese manual http://www.cnblogs.com/stephen-liu74/archive/2012/02/27/2370212.html

  1. Download the latest version of redis

Download http://redis.io/download

Currently the latest version of redis3.2.11

cd /usr/local/src

wget http://download.redis.io/releases/redis-3.2.11.tar.gz

  1. Compile and install redis (see the README in the source root directory for details)

tar -zxvf redis-3.2.11.tar.gz

cd redis-3.2.11

make

After the make command is executed, six executable files will be generated in the src directory, namely redis-server, redis-cli, redis-benchmark, redis-check-aof, redis-check-dump, redis-sentinel, and their The role is as follows:

redis-server: daemon startup program for Redis server

redis-cli: Redis command line operation tool. Of course, you can also use telnet to operate according to its plain text protocol

redis-benchmark: Redis performance testing tool to test the read and write performance of Redis in your system and your configuration

redis-check-aof: update log check

redis-check-dump: used for local database check

redis-sentinel: cluster management tool

installation:

make PREFIX=/usr/local/redis install

test

make test

Depend on tcl, if not installed, please install first

yum -y install tcl tcl-devel

Three, configure Redis

mkdir -p /usr/local/redis/etc /usr/local/redis/log /usr/local/redis/data #Create configuration file, log file, database storage directory

cp /usr/local/src/redis-3.2.11/redis.conf /usr/local/redis/etc/

touch /usr/local/redis/log/redis.log

  1. Redis.conf configuration parameters:

Whether to run as a daemon

daemonize yes

If running as a background process, you need to specify a pid, the default is /var/run/redis.pid

pidfile /var/run/redis.pid

Bind the host IP, the default value is 127.0.0.1

bind 0.0.0.0

Redis default listening port

port 6379

How many seconds after the client is idle, it will disconnect, the default is 300 (seconds)

timeout 300

Logging level, there are 4 optional values, debug, verbose, notice (default value), warning

loglevel notice

Specify the file name of the log output, the default value is stdout, or it can be set to /dev/null to shield the log

logfile "/usr/local/redis/log/redis.log"

The number of available databases, the default value is 16, the default database is 0

databases 16

Strategy for saving data to disk

When a key data is changed, refresh to disk once in 900 seconds

save 900 1

When 10 Keys data is changed, refresh to disk once in 300 seconds

save 300 10

When 1w pieces of key data are changed, refresh to disk once in 60 seconds

save 60 10000

Whether to compress data objects when dumping the .rdb database

rdbcompression yes

The name of the local database file, the default value is dump.rdb

dbfilename dump.rdb

Local database storage path, the default value is ./

dir /usr/local/redis/data

Set PID file directory

pidfile /usr/local/redis/redis.pid

########### Replication #####################

Redis replication configuration

slaveof When this machine is a slave service, set the IP and port of the master service

masterauth When this machine is a slave service, set the connection password of the master service

Connection password

requirepass foobared

Maximum number of client connections

maxclients 30000

Maximum memory usage setting. After reaching the maximum memory setting, Redis will first try to clear the expired or about to expire keys. After this method is processed, the maximum memory setting will not be reached and no more write operations can be performed.

maxmemory

Whether to log after each update operation. If it is not turned on, it may cause data loss for a period of time when the power is off. 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 value is no

appendonly no

Update log file name, the default value is appendonly.aof

appendfilename

Update log conditions, there are 3 optional values. no means waiting for the operating system to synchronize the data cache to the disk, always means manually calling fsync() to write the data to the disk after each update operation, and everysec means syncing once per second (default value).

appendfsync always

appendfsync everysec

appendfsync no

################ VIRTUAL MEMORY ###########

Whether to enable the VM function, the default value is no

vm-enabled no

vm-enabled yes

Virtual memory file path, the default value is /tmp/redis.swap, it cannot be shared by multiple Redis instances

vm-swap-file /tmp/redis.swap

Store all data larger than vm-max-memory into virtual memory, no matter how small vm-max-memory is set, all index data is stored in memory (Redis index data is keys), that is, when vm-max When -memory is set to 0, all values actually exist on the disk. The default value is 0.

vm-max-memory 0

vm-page-size 32

vm-pages 134217728

vm-max-threads 4

############# ADVANCED CONFIG ###############

glueoutputbuf yes

hash-max-zipmap-entries 64

hash-max-zipmap-value 512

Whether to reset the Hash table

activerehashing yes

Note: The official Redis document puts forward some suggestions on the use of VM:

When your key is small and the value is large, the effect of using VM will be better. Because this saves more memory.

When your key is not small, you can consider using some very methods to turn a large key into a large value.For example, you can consider combining key and value into a new value.

It is best to use a file system that supports sparse files such as linux ext3 to save your swap files.

The parameter vm-max-threads can set the number of threads accessing the swap file. It is best not to exceed the number of cores of the machine. If it is set to 0, then all operations on the swap file are serial. It may cause longer Time delay, but there is a good guarantee for data integrity.

  1. Adjust system kernel parameters

If the memory situation is tight, you need to set the kernel parameters:

echo 1 > /proc/sys/vm/overcommit_memory

Here to talk about the meaning of this configuration: /proc/sys/vm/overcommit_memory

This file specifies the kernel's strategy for memory allocation, and its value can be 0, 1, 2.

0 , Which means that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an error is returned to the application process.

1 , Which means that the kernel allows all physical memory to be allocated, regardless of the current memory state.

2 , Which means that the kernel allows the allocation of memory that exceeds the sum of all physical memory and swap space

When Redis dumps data, it forks a child process. Theoretically, the memory occupied by the child process is the same as that of the parent. For example, the memory occupied by the parent is 8G. At this time, 8G of memory should also be allocated to the child. Unable to afford it, it will often cause the redis server to be down or the IO load is too high, and the efficiency is reduced. So the more optimized memory allocation strategy here should be set to 1 (indicating that the kernel allows all physical memory to be allocated regardless of the current memory status)

  1. Run Redis

Add environment variables

echo "export PATH=$PATH:/usr/local/redis/bin" >> /etc/profile

.  /etc/profile

  1. Run the service

redis-server /etc/redis/redis.conf

You can start the redis service in the background. After confirming that it is running, you can use the redis-benchmark command to test and see, or you can actually operate it through the redis-cli command, such as:

redis-cli set foo bar

OK

redis-cli get foo

bar

  1. Close service

redis-cli shutdown

If the port changes, you can specify the port:

redis-cli -p 6380 shutdown

  1. Save/backup

Data backup can be achieved by regularly backing up the file.

Because redis is written to disk asynchronously, if you want to write the data in memory to the hard disk immediately, you can execute the following command:

redis-cli save or redis-cli -p 6380 save (specify port)

Note that the above deployment operations require certain permissions, such as copying and setting kernel parameters.

When the redis-benchmark command is executed, the memory data will also be written to the hard disk.

  1. Synchronization mechanism

The synchronization mechanism implemented by redis is relatively simple and lacks the common check point and verification mechanism of synchronization mechanisms.

At runtime, if the forwarding of the master -> slave synchronization request is discarded, the slave will not be able to restore the related information of the request until the slave restarts when the data is fully loaded from the master. Therefore, it is recommended to use redis to use its key/value and value to support multiple types of features and store some relatively unimportant data.

  1. Set random start

vi /etc/init.d/redis #Add the following

#! /bin/bash

redis - this script starts and stops the redis-server daemon

chkconfig:   - 80 12

description:  Redis is a persistent key-value database

processname: redis-server

config:      /etc/redis/redis.conf

pidfile:     /var/run/redis.pid

source /etc/init.d/functions

BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/usr/local/redis/redis.pid"

Read configuration

[ - r "SYSCONFIG" ] && source "SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"

start() {
        if [ -e PIDFILE ];then              echo "desc already running...."
             exit 1
        fi
        echo -n $"Starting $desc: "
        daemon BIN/prog CONFIG         RETVAL=?
        echo
        [ RETVAL -eq 0 ] && touch /var/lock/subsys/prog
        return $RETVAL
}

stop() {
        echo -n $"Stop $desc: "
        killproc prog         RETVAL=?
        echo
        [ RETVAL -eq 0 ] && rm -f /var/lock/subsys/prog $PIDFILE
        return $RETVAL
}

restart() {
    stop
    start
}

case "1" in   start)         start         ;;   stop)         stop         ;;   restart)         restart         ;;   condrestart)         [ -e /var/lock/subsys/prog ] && restart
        RETVAL=$?
        ;;
  status)
        status prog         RETVAL=?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL

Before using this script to manage, you need to configure the following kernel parameters, otherwise the Redis script may report an error when restarting or stopping redis, and it cannot automatically synchronize data to the disk before stopping the service:

vi /etc/sysctl.conf

vm.overcommit_memory = 1

Then the application takes effect:

sysctl -p

( Adjust the memory (if the memory situation is tight, you need to set the kernel parameters)
echo 1 > /proc/sys/vm/overcommit_memory
Description:
This file specifies the kernel's strategy for memory allocation, and its value can be 0, 1, 2.
0 Indicates that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application 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 Indicates that the kernel allows the allocation of memory exceeding the sum of all physical memory and swap space
)

Then add the service and start it automatically:

chmod 755 /etc/init.d/redis

chkconfig redis on

chkconfig --list redis

Centos 7 startup script

/usr/lib/systemd/system/redis.service

#####################################

[ Unit]

Description=Startup script for the Redis

Documentation=http://redis.io

After=network.target remote-fs.target nss-lookup.target

[ Service]

Type=forking

PIDFile=/usr/local/redis/redis.pid

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[ Install]

WantedBy=multi-user.target

Specify running user

useradd -M -s /sbin/nologin redis

chown -R redis:redis /usr/local/redis/log /usr/local/redis/data pidfile

cat /usr/lib/systemd/system/redis.service

[ Unit]

Description=Startup script for the Redis

Documentation=http://redis.io

After=network.target remote-fs.target nss-lookup.target

[ Service]

User=redis

Group=redis

Type=forking

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[ Install]

WantedBy=multi-user.target

systemctl enable redis; systemctl start redis

Recommended Posts

Redis3 installation under Centos7
Redis cluster installation under CentOS
Redis cluster installation under CentOS
Installation and configuration of redis under centos7
2019-07-09 CentOS7 installation
centos7_1708 installation
Installation under centos6.9 of jenkins learning
Java-JDK installation and configuration under CentOS
Erlang 20.2 installation and deployment under CentOS 7
Centos5 installation guide
Python - centos6 installation
Docker installation (CentOS7 installation)
CentOS7 docker installation
Tomcat installation and configuration under CentOS 7 (Tomcat startup)
MySQL 8.0 installation, deployment and configuration under CentOS 6/7
Zabbix installation and deployment and localization under CentOS
Jenkins installation and deployment tutorial under CentOS 7
KVM installation and preliminary use under CentOS 7.2
CentOS online installation RabbitMQ3.7
Deploy GitBook under CentOS7
Compile Hadoop-2.7.6 under CentOS7.4
Install mysql5.7 under CentOS7
Install ActiveMQ under Centos7
lamp (centos7) installation lamp environment
Install PostgreSQL12 under CentOS7
Install CentOS under VMware
LNMP installation under Ubuntu
CentOS 7 installation and configuration graphic tutorials under VMware10
Graphical installation of CentOS8
Mysql8.0.15 installation configuration (centos7)
Install redis5.0 in CentOS7
Install mysql under Centos 7
Configure lamp under centos6.8
Linux notes (1): CentOS-7 installation
Installation and configuration of rsync server under CentOS 6.5
Install Jenkins under Centos 7
Install MariaDB under MariaDB Centos7
Install mysql5.1 under CentOS6.5
CentOS 7 install Redis 5.0.8 original
MySQL 8.0 installation and deployment under CentOS, super detailed!
CentOS online installation RabbitMQ3.7
Docker CentOS installation method
Non-Root installation of Microsoft R Open under Centos
Linux CentOS 7 installation tutorial
Centos7 docker installation details
From installation to entry of FastDFS under Centos7
Installation and cracking of confluence6.3 operation records under Centos
Installation and cracking of Jira7 operation records under Centos
Xen virtualization combat under CentOS 6.6
Discourse CentOS 8 new installation manual
Build docker environment under Centos6.5
Centos 7 mini installation process record
Build OpenV** Server under CentOS7
Centos mysql installation and configuration
Build OpenLDAP server under CentOS7
Glusterfs cluster installation on Centos7
CentOS 7 Galera Cluster installation guide
CentOS 7 installation and configuration PPTP
Configure static IP under CentOS 7
Install Oracle11gR2 database under CentOS6.9
CentOS installation and configuration cmake