Redis is an advanced key-value database. It is similar to memcached, but data can be persisted and supports a wide range of data types. In operation and maintenance work, caching is a very important technology. For static file caching, we have: nginx cache, squid cache, etc. For database caching, we have redis and memcache, etc. Some companies even choose to build redis clusters. . Today we will take a look at the installation and configuration of the redis cache. (Please see the next breakdown for configuration and usage) |
Introduction to redis
Redis is an advanced key-value database. It is similar to memcached, but data can be persisted and supports a wide range of data types. There are strings, linked lists, sets and sorted sets. It supports calculating the union, intersection and complement (difference) of sets on the server side, and also supports a variety of sorting functions. So Redis can also be regarded as a data structure server.
PS: This installation uses redis to store strings to store user sessions.
#redis official website: https://redis.io/ #redis Chinese official website: http://www.redis.cn/ #We choose the latest stable version redis-3.2.9.tar.gz
Installation planning
Software package storage location: /usr/local/src Software package compilation location:/usr/local/src/redis-xx/ Software installation location:/usr/local/redis/ Software command location:/usr/local/redis/bin Software log storage location: /usr/local/reids/redis.log Software configuration file location:/usr/local/redis/etc/redis.conf Software pid file location:/var/run/redis.pid Software startup service location: /etc/init.d/redis Software authentication requires password
Redis installation
Preparation work for redis, installing dependency packages
Centos series:
yum repolist yum -y install gcc gcc-c++ make gmake cmake zlib tcl
Ubuntu series:
agt-get update Agt-get install gcc gcc-c++ make zlib tcl
Download the package and install redis
cd /usr/local/src \\Enter the package directory (normalized) wget http://download.redis.io/releases/redis-3.2.9.tar.gz \\Download package tar -xzf redis-3.2.9.tar.gz \\#Extract and enter the redis directory cd redis-3.2.9 \\Extract and enter the redis directory #Run test ./runtest #Precompile, compile and install make make test #Enter the src directory to install cd src make PREFIX=/usr/local/redis install PS: The compilation and installation process of redis is long. Here we compile and install redis to the "/usr/local/redis" directory. By default, the bin command is under "/usr/bin" and you can directly call the redis command . . After pointing to the installation path, we need to add system environment variables later.
Redis configuration file
Create the redis configuration file directory and copy the configuration file redis.conf
mkdir -p /usr/local/redis/etc cp /usr/local/src/redis-3.2.9/redis.conf /usr/local/redis/etc/redis.conf
Modify configuration file
vim /usr/local/redis/etc/redis.conf
cat /usr/local/redis/etc/redis.conf |grep -v "^$" |grep -v "#
bind 0.0.0.0 \\Modify content ======= External monitoring protected-mode yes port 6379 \\Modify the port number=====as required tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes \\Daemon mode is turned on, used to add system services supervised no pidfile /var/run/redis.pid \\pid location loglevel verbose \\log startup level logfile /usr/local/redis/redis.log \\log storage location databases 16 \\reids Number of databases save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ 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 requirepass mima\\Access Authentication==================Password maxmemory 256m \\Maximum memory setting maxmemory-policy volatile-ttl \\redis maximum memory discard rules appendonly no appendfilename "appendonly.aof" appendfsync everysec 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
Configuration file modification content:
port 6379 Modify port number bind 0.0.0.0 Modify the binding IP (as required) timeout 0 Modify the connection timeout loglevel verbose log three types debug, verbose, notice, warn logfile /var/log/redis.log log directory ***** maxmemory 256m redis memory size*** maxmemory-policy volatile-ttl memory discard policy requirepass password redis authentication rules** (on demand)
PS: Pay attention to the location of the configuration log and pid, etc., and they must be related to the startup script . The password must also be associated with the startup script .
Create and modify system startup configuration files
Create system service startup file
Startup file template:/usr/local/src/redis-3.2.9/utils/redis_init_script
cp /usr/local/src/redis-3.2.9/utils/redis_init_script /etc/init.d/redis chmod 755 /etc/init.d/redis
Modify system startup script
#!/bin/sh #Configurations injected by install_server below.... EXEC=/usr/local/redis/bin/redis-server ######Service command startup##### CLIEXEC=/usr/local/redis/bin/redis-cli ####Client command path### PIDFILE=/var/run/redis.pid ###The location of the pid file#####must be consistent with the configuration file CONF="/usr/local/redis/etc/redis.conf" ####Path to the redis configuration file## REDISPORT="6379" #####Start port#################### # SysV Init Information # chkconfig: - 58 74 #####Add content, comment but meaningful###### # description: redis_6379 is the redis daemon. ### BEGIN INIT INFO # Provides: redis_6379 #####Start port#################### # Required-Start: $network $local_fs $remote_fs # Required-Stop: $network $local_fs $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Should-Start: $syslog $named # Should-Stop: $syslog $named # Short-Description: start and stop redis_6379 # Description: Redis daemon ### END INIT INFO 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 -a mima shutdown \\###Modify the content and add the -a password verification item### while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; status) PID=$(cat $PIDFILE) if [ ! -x /proc/${PID} ] then echo 'Redis is not running' else echo "Redis is running ($PID)" fi ;; restart) $0 stop $0 start ;; *) echo "Please use start, stop, restart or status as first argument" ;; esac
PS: Anyone who has read " This is how you should learn Linux " knows how to write a system startup file and what format it is, so we won't explain it here.
The redis startup script adds two authentication methods
method one:
Method Two:
Add startup script to system services
chkconfig --add redis #Add system services chkconfig redis on #Set auto-start at boot service redis start service redis status service redis stop service redis restart
There is no chkconfig in Ubuntu system
PS: chkconfig may not be easy to install on Ubuntu systems. You can use the update-rc.d command to use it.
update-rc.d redisd defaults 80 80 root@linuxprobe:/etc/redis# update-rc.d redisd defaults 80 80 Adding system startup for /etc/init.d/redisd ... /etc/rc0.d/K80redisd -> ../init.d/redisd /etc/rc1.d/K80redisd -> ../init.d/redisd /etc/rc6.d/K80redisd -> ../init.d/redisd /etc/rc2.d/S80redisd -> ../init.d/redisd /etc/rc3.d/S80redisd -> ../init.d/redisd /etc/rc4.d/S80redisd -> ../init.d/redisd /etc/rc5.d/S80redisd -> ../init.d/redisd 2345 Start 0 1 6 Shut down
If the authentication field is not added in the startup script:
Startup file consistency
After the above operations, our redis installation is roughly completed, but there are still some configurations that have not been processed, such as our environment variables and our stress test tuning. Please see the breakdown in the next issue for more excitement! ! ! !