Redis installation and basic understanding

Redis installation and basic understanding

A, redis basic understanding

1.1 Redis is completely free open source, using c language, is a single-threaded, high-performance (key / value) memory database, nosql database based on memory to run and support persistence.

1.2 Redis is mainly used for the cache, but not only do caching, such as: redis counter generates distributed unique primary key, redis implement distributed lock queue, the session cache, thumbs up, site traffic statistics.

Two, redis installed in the Linux environment

Download: http://download.redis.io/releases/ , you can choose to download the latest stable version

I use version redis-5.0.7.tar.gz, download and install:

[root@winner operation]#tar -zvxf redis-5.0.7.tar.gz
[root@winner operation]#cd redis-5.0.7
[root@winner redis-5.0.7]#make
[root@winner redis-5.0.7]#make install
cd src && make install
make[1]: Entering directory `/operation/redis-5.0.7/src'
CC Makefile.dep
make[1]: Leaving directory `/operation/redis-5.0.7/src'
make[1]: Entering directory `/operation/redis-5.0.7/src'

Default installation path: / usr / local / bin

 

Will be compiled service program redis redis-server, as well as the client program redis-cli after make for testing under redis-5.0.7 directory, two programs located in the installation directory under the src directory:

The following start redis service.

[root@winner operation]# cd redis-5.0.7
[root@winner redis-5.0.7]# cd src/
[root@winner src]# ./redis-server 
14026:C 27 Dec 2019 16:44:09.563 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14026:C 27 Dec 2019 16:44:09.564 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=14026, just started
14026:C 27 Dec 2019 16:44:09.564 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf

Note that this redis way to start using the default configuration. You can also tell by redis startup parameters using the specified configuration file using the following command to start

[root@winner redis-5.0.7]# cd src/
[root@winner src]# ./redis-server ../redis.conf 
14078:C 27 Dec 2019 16:47:57.914 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14078:C 27 Dec 2019 16:47:57.914 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=14078, just started
14078:C 27 Dec 2019 16:47:57.914 # Configuration loaded

The start-redis will be printed to the console, close the console can not continue to be connected, then you need to modify redis.conf

daemonize no replacement for the daemonize yes

Start-up and testing the connection, close the exit 
[root @ Winner src] # / Redis-Server ../redis.conf.
[Root @ Winner src] # PS -ef | grep Redis
root 14135 1 0 16:54 00:00:? ./redis-server 127.0.0.1:6379 00
root 14151 13942 0 16:55 PTS / 0 00:00:00 grep Redis
[root @ Winner src] # ./redis-cli
127.0.0.1:6379> GET YW
(nil )
127.0.0.1:6379> Exit

Three, redis.conf basic understanding

#redis default configuration file 
# redis.conf 
# Redis the Configuration File Example. 
# ./redis-server /path/to/redis.conf 
 
# This is useful when you have a standard configuration template but each server they need redis personalized settings . 
INSTEAD the If you are interested # in the override to a using Includes the Configuration 
# Options, IT IS AS of Better to use the include at The Last Line. 
# Introduce other file 
# the include /path/to/local.conf 
# the include /path/to/other.conf 
 
# Note ON units: the when Memory size iS needed, IT iS Possible to the Specify 
# IT in at the usual form of 1K 5GB 4M and SO Forth: 
# configure the size of the unit, at the beginning of the definition of some basic unit of measurement, only supports bytes, not supported bit case insensitive 
# 1K => 1000 bytes 
# 1KB => 1024 bytes 
# 1M => 1000000 bytes 
# 1MB => 1024 * 1024 bytes 
# 1g => 1000000000 bytes 
# 1GB => 1024 * 1024 * 1024 bytes
# 
# INSENSITIVE SO 1GB Case Units are 1Gb 1 gB All are at The Same,. 


# IF the ARE YOU SURE YOU WANT YOUR INSTANCE the TO LISTEN the TO ALL THE INTERFACES 
# JUST the COMMENT THE FOLLOWING LINE. 
# ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ 
# bind Ip designation can connect the Redis instance of ip If a comment (deleted) then any IP can connect 
the bind 127.0.0.1 

# prohibit external network access redis, if enabled, even if comment out the bind 127.0.0.1, and then access redisd time still can not connect 
conditions enabled it # there are two, the first is not using bind, and the second is not set access password. 
yes the MODE-protected 
 
# whether performed in the background, yes: the background; no: not running in the background 
daemonize yes 
 
# When Redis when running in daemon mode, the default Redis will write /var/run/redis.pid pid file, pidfile specified by 
pidfile /var/run/redis/redis-server.pid 
 
# Redis specified listening port 
port 6379 
 
# this parameter defines the queue TCP connection has been completed (after the completion of three-way handshake) in length, 
# of course, this value must be no greater than Linux system defined in / proc / sys / net / core / somaxconn value, the default is 511, 
# and Linux the default parameter value is 128. When the system is complicated and large slow speed when the client can be set with reference to these two parameters together. 
# In high concurrency environment you need a high backlog value to prevent a slow client connection issues 
# the default kernel parameter value is generally 128, for heavily loaded service program is big enough. It generally will be revised to 2048 or more. 
# Added to /etc/sysctl.conf: net.core.somaxconn = 2048, and then perform sysctl -p in the terminal. 
backlog 511-tcp 

 
# Configure unix socket to let listens redis support local connections. 
Unixsocket /var/run/redis/redis.sock # 
# configuration file permissions using unix socket 
# unixsocketperm 700 

# This parameter is to set the client idle for more than timeout, the server will be disconnected for 0 server does not take the initiative to break open connections, not less than 0. 
0 timeout 
 
# Set how long to detect dead connections in seconds, if set to 0, it will not be Keepalive detection 
tcp-Keepalive 0 

# systemd and upstart can guard by Redis management process, and this parameter is related to the specific operating system . 
supervised no

 
# Specified level server logs. Levels include:  
Debug (a lot of information to facilitate development, test),
verbose (a lot of useful information, but no debug-level information and more, the default is verbose), 
Notice (appropriate log level for production environments), 
The warn (only very important information) 
the LogLevel Notice 
 
# specifies logging file. Empty string, then the log will be printed to the standard output device. Redis standard output running in the background is / dev / null. 
logfile "" 
 
# syslog function is turned record 
# syslog-Enabled NO 
 
#syslog identifier. 
# Syslog-ident redis 
 
source # logs, device 
# syslog-facility local0 
 
number # database, the database is used by default DB 0. By "SELECT" command to select a different database 
#dbid is a number between 0 to databases - a value between. 1 
Databases 16 
 
###################### SNAPSHOTTING ################################ ########## 
# snapshot configuration 
# comment out " save "this line configuration item you can make to save the database function failure 
# set sedis database mirroring frequency. 
At least one of the key values change # 900 seconds (15 minutes) (the database stored - persistent)
# Within 300 seconds (5 minutes) at least 10 changes the key value (the database stored - persistence) 
of at least 10,000 # key value changes within 60 seconds (1 minute) (the database stored - persistent)
900 1 the Save 
the Save 300 10 
the Save 60 10000 
 
# when RDB persistent errors, whether still continues to carry out work 
#yes: can not work, no: you can continue to work 
# can learn about the info in rdb_last_bgsave_status RDB whether there is persistent error 
# By default, if the last redis background saving failed, redis will stop accepting write so in a tough way to let the user know that the data is not correctly persisted to disk, 
# otherwise it would have no one noticed disaster happened. If the background save process to restart work, redis also will automatically allow a write operation. 
# If you configured no, that you do not care about inconsistent data, or other means to discover and control the 
STOP-Writes-ON-bgsave-error yes 
 
# Use compression rdb file, rdb files compressed using LZF compression algorithm, yes: compression, but need some cpu consumption. no: no compression, need more disk space 
rdbcompression yes 
 
# After storage snapshot, you can also make use redis CRC64 algorithm for data verification, but doing so will increase to about 
# 10% of the consumption of performance, if you want to get maximum performance improvement, you can turn this feature off 
rdbchecksum yes 
 
name #rdb file 
dbfilename dump.rdb 
dir / var / lib / redis
 
# Dbfilename file storage directory. Must be a directory, rdb, aof file will be saved to the directory
 
################################# REPLICATION ################ ################# 

# set when the unit is slave service, master service set the IP address and port, when Redis starts, it will automatically synchronize the data from the master 
# replicaof  
# When the master service is password-protected, slave master password for the service connection< masterip >  < masterport > 

# when a slave lost contact with the master, or copy the time being, coping behavior slave request: 
# If yes (the default value), slave still answering client requests, but returned data may be outdated, or the data may be empty at the time of the first synchronization 
# If no, when you execute other commands in addition to the info and salveof, slave will return a "sYNC with master in progress" of error. 
-serve-STALE-Replica the Data yes 

# copy option, slave to copy the corresponding master. 
Slaveof # < masterip >  < masterport >

# If the master set requirepass, then the slave to connect to the master, the job requires a master password. 
#masterauth is used to configure the master password, which can be connected to the master after authentication. 
Masterauth # < Master-password > 
 
 
# as from the server, default is read (Yes), NO can be modified, for writing (not recommended). 
the Read-only yes-Slave 
 
# whether to use the socket reproduce data. Redis currently provides two ways to copy, disk and socket. 
# If the new slave even up or reconnection can not be part of the slave synchronization, it will perform a full amount of synchronization, master generates rdb file. 
# There are 2 ways: disk to create a new way to master the process to save the rdb file to disk, then transfer rdb files on the disk to the slave. 
#socket master is to create a new process, rdb files directly to the socket of the way distributed to slave. When disk mode, when a rdb preservation process, multiple slave can share this rdb file. 
one slave sequential #socket way to copy. In the slow disk speed, fast speed case recommended way socket. 
repl-diskless-sync no 
 
delay #diskless copied, 0 is set to prevent. Once the copy start, copy the new node will not be received until the next slave request transmission rdb. So it is best to wait for some time, and so even more slave up. 
--Sync-diskless the repl Delay. 5 
 
#slave send a ping request to the server according to the specified time interval. Time interval can be set by repl_ping_slave_period, 10 seconds by default.
The repl-of ping-Slave #-period 10 
 
# replication connection time. master and slave have the timeout setting time. master slave detected time last sent exceeds repl-timeout, i.e. off-line that slave, the slave to clear information. 
#slave detected last time and master the interaction of more than repl-timeout, it considers that the master offline. 
# Note that repl-timeout to set a larger value than repl-ping-slave-period, or will often timeout is detected. 
Repl-timeout 60 # 
 
# prohibit copying the link tcp tcp nodelay parameters can be passed yes or no. 
# The default is no, that is, use tcp nodelay. If the master is set to yes prohibit tcp nodelay set, when copying data to the slave, it will reduce the number of packets and less network bandwidth. 
# But it also may bring delayed data. By default we recommend smaller delays, but in a large amount of data transmission scenario, it is recommended to select yes. 
-disable-tcp-repl nodelay NO 
 
# copy buffer size, which is a ring-shaped copy buffer is used to save the latest copy of the order. So that slave when they are offline, the data does not need to completely master copy of 
# if you can perform a partial synchronization, just need to copy the data to the buffer part of the slave, will be able to return to normal replication status. 
the greater size # buffer, slave off time can be longer, the copy buffer memory is allocated only when there is slave connection. No slave period of time, the memory will be released, the default 1m. 
The repl-size backlog-# 5MB 
 
#MASTER period no slave releases the buffer memory copy, repl-backlog-ttl to set the length of time. Seconds.
Repl-backlog-ttl # 3600 
 
# when the master is unavailable, Sentinel will be a master slave according to the priority of the election. Lowest priority slave, was elected master. And configured to 0, it will never be elected. 
100-priority slave 
 
#redis provided allows master stops writing manner, if the configuration min-slaves-to-write, the slave is less than the number of healthy N, mater writing is prohibited. master must have a minimum number of healthy survival of the slave can execute the write command. This configuration will not guarantee that all the N slave must be able to write received master's, but not enough to avoid a healthy slave of time, master can not be written to avoid data loss. 0 is set to turn off the feature. 
Min-to-slaves #-Write. 3 
 
# delay is less than min-slaves-max-lag second slave slave was considered healthy. 
Min-max-slaves # LAG-10 
 
# 1 or the other set to 0 to disable this feature. 
One or the Setting The OTHER # to 0 disables The Feature. 
# By default min-to-Write-slaves IS SET to 0 (Feature Disabled) and 
# min-max-LAG-slaves to 10. The IS SET
 
################################## SECURITY ############### ####################
#requirepass configuration allows users to use the AUTH command to authenticate password before you can use other commands. This allows redis can be used in an untrusted network. To maintain backward compatibility, you can annotate the order, because most users do not need certification. Use requirepass need to pay attention, because redis too fast, you can password authentication 15w times per second, simple passwords are easily compromised, so it is best to use a more complex password. 
Requirepass foobared # 
 
# to dangerous command to change it to another name. CONFIG commands such as a command to rename difficult to guess, so that users can not use, and can then use an internal tool. 
B840fc02d524045429941cc15f59e41cb7be6c52 the rename the CONFIG-Command # 
 
# set to a null value, a command may be prohibited 
# Command-the rename the CONFIG "" 
######################### LIMITS #################################### ########## 
 
# settings can redis even on the largest number of client connections. The default is 10 000 client connections. Since the connection is redis client does not distinguish between the internal connection or open a file or the like and connected slave, it is set to the minimum recommended maxclients 32. If more than maxclients, redis will send a new connection 'max number of clients reached', and close the connection. 
# Maxclients 10000
 
#redis configured maximum memory capacity. When the memory is full, the need to meet maxmemory-policy strategy for processing. Note that the slave output buffer is not calculated in the maxmemory. Therefore, in order to prevent maxmemory host memory used up, recommended settings need to be smaller. 
Maxmemory # < bytes > 
 
# memory capacity after more than maxmemory processing strategy. 
# volatile-lru: use LRU algorithm removes key expiration time set too. 
# volatile-random: random removal of key expiration time set too. 
# volatile-ttl: removing the expiring key, according to a recent expiration time to remove (supplemented TTL) 
# AllKeys-the LRU: LRU algorithm removes the use of any key. 
# allkeys-random: random removal of any key. 
#noeviction: do not remove any key, just return a write error. 
# Above these eviction policy, if no suitable key redis expulsion, for a write command, or returns an error. redis will no longer receive a write request, receive only get request. Write commands include: set setnx setex append incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby getset mset msetnx exec sort.
# Maxmemory-policy noeviction 
 
number of samples #lru detection. Use lru or ttl out algorithm, select sample from a key need to eliminate random list, select idle the longest key removed. 
5-maxmemory the Samples # 
 
############################## APPEND ONLY the MODE ############ ###################
# Default redis using rdb persistent way, this way in many applications have been sufficient. But redis if she goes down, there may be a few minutes can cause data loss, save for persistence according to policy, Append Only File is another persistent way, it can provide a better persistence characteristics. Redis data will be written each time after receiving written appendonly.aof all files each time you start Redis data will first read this file into memory, Ignore RDB file. 
NO appendOnly 
 
#aof file name 
appendFileName "appendonly.aof" 
 
#aof persistent policy configuration 
#no means no fsync, the operating system to ensure data synchronization to disk, the fastest. 
#always represent each write are executed fsync, to ensure data synchronization to disk. 
#everysec that the implementation of fsync once per second, which could result in loss of data 1s. 
everysec appendfsync 
 
# when aof rewrite or write rdb file, it will perform a number of IO, this time for aof mode of everysec and always, the execution would cause obstruction fsync too long, no-appendfsync-on-rewrite field is set the default setting is no. If the requirements for high application latency, this field can be set to yes, or otherwise set to no, so this is a safer choice for persistence characteristic is. Set to yes to indicate a write operation during rewrite is not new fsync, temporarily stored in memory, and so on and then rewrite the writing is completed, the default is no, recommend yes. Linux's default fsync strategy is 30 seconds. You may lose 30 seconds of data. 
no-appendfsync-on-rewrite no
 
#aof automatically rewrite configuration. What percentage of the current aof when the file size exceeds the last rewrite the aof file size to be rewritten, that is, when aof file grows to a certain size when Redis can call bgrewriteaof log files to be rewritten. AOF current log file size is the last AOF rewrite get twice the file size (to 100), the log automatically start a new process of rewriting. 
the rewrite--aof-Auto PERCENTAGE 100 
# aof arrangement allows rewriting the minimum file size, to avoid achieving the prescribed size is still small percentage of cases, but also rewriting 
Auto-aof the rewrite-min-size-64MB 
 
#aof files may the tail is incomplete, when redis start, aof data file is loaded into memory. Restart may occur after the host redis where the operating system is down, especially in the ext4 file system without adding data = ordered option (redis down or abort incomplete phenomenon will not cause the tail.) This phenomenon, you can choose to let redis exit, or import as much data. If you select yes, when truncated aof file is imported, a log will be automatically published to the client and then load. If no, the user must manually redis-check-aof AOF files can repair. 
aof-Load-truncated yes 
 
################################ LUA SCRIPTING ############### ################ 
# If the maximum time limit (ms) to achieve, redis will remember a log, then return error. When a script exceeds the maximum limit. Only SCRIPT KILL and SHUTDOWN NOSAVE can be used. The first tune write command can not kill something. If has been called write, you can only kill a second command. 
Time-limit 5000-Lua 
 
################################ ########## Redis the CLUSTER ##################### 
# cluster switch, the default is not open cluster model. 
# Cluster-enabled yes
 
# Cluster configuration file name, each node has a relevant configuration file for the cluster, persistent information stored cluster. This document does not require manual configuration, the configuration file has Redis generated and updated every Redis cluster node requires a separate configuration file, make sure the system instance running in the configuration file name does not conflict 
# cluster-config-file nodes- 6379.conf 
 
# interconnection node timeout threshold. Timeout milliseconds cluster nodes 
# 15000 Cluster-Node-timeout
# Cluster-require-full-coverage yes
  
# during failover when all the slave will request applications for the master, but some slave may be disconnected for some time with the master, resulting in data too old, so no slave it should be promoted to master. This parameter is used to determine the master and slave nodes break time is too long. Analyzing method:
# Comparison and slave disconnection time (timeout Node-slave-validity-factor *) + the repl-slave-period of ping- 
# If the node timeout to thirty seconds, and the slave-validity-factor is 10, assuming that the default repl-ping-slave-period is 10 seconds, i.e. if more than 310 seconds slave will not attempt to failover 
# Cluster-slave-factor-10 the validity 
 
#MASTER value is greater than the number of slave, slave to migrate to other master isolated , this parameter as if they are set to 2, then only when a master node has two can work from the node, it will attempt to migrate from one node. 
1 # cluster-migration-barrier 
 
under # default, the cluster node is responsible for all of the slot there, cluster status is only ok, in order to provide services. Set to no, we can not provide services in all the time slot allocation. Not recommended to open the configuration, this will result in the partition, the little master partition has been accepted write requests, resulting in inconsistent data for a long time. 
# Execution time ratio slowlog-log-slower-than large slowlog requests to the inside, in microseconds, so that one second 1,000,000. Note that the negative time will disable slow query log, and 0 will be forced to record all commands.
 
################################## SLOW LOG ############## ##################### 
### slog log is used to record slower command execution took redis operation. When the command execution exceeds the specified time, it is recorded in the slow log in, slog log stored in memory, so there is no IO operations. 
It would help--log-slowlog Within last 10000 
 
# slow query log length. When a new command is written into the log, the oldest record will be deleted. This length is not limited. As long as there is enough memory on the line. You can release memory by SLOWLOG RESET. 
128-max-len slowlog 
 
################################ ########## LATENCY the MONITOR #################### 
# delay monitoring function is used to perform some operation monitoring redis slow, treated with lATENCY redis examples Processed print run command when the chart . Only the recording operation value lower than or equal to the set. 0, then, is to close monitoring. The default latency monitoring feature is turned off, if you need to open, you can also dynamically set by CONFIG SET command. 
Monitor-threshold 0-Latency 
 
############################# the EVENT the NOTIFICATION ############# ################# 
# key space notification enables the client to receive those who in some way change the Redis data collection events by subscribing to the channel or mode. Because open space notification feature keys need to consume some CPU, so in the default configuration, the function is disabled.
# notify-keyspace-events of argument can be any combination of the following characters, which specifies what type of server the notification: 
## K key space notice, all notifications to __keyspace @__ prefixed 
## E key event notification, All notification __keyevent @__ prefixed 
independent general command type ## g DEL, EXPIRE, RENAME, etc. notification 
## notifies $ string command 
notification command list ## L 
notification command set ## S 
## H notice hash command 
notification command ## z ordered collection of  
## x expired event: send whenever a key is deleted expired
## e expel (evict) event: send whenever a key is being deleted because maxmemory policy 
## aliases a parameter g $ lshzxe the 
parameter # input at least to have a K or E, otherwise, no matter what the rest of the argument is, there will not be any notification being distributed. Details may be used in reference http://redis.io/topics/notifications 
 
Notify-keyspace-Events "" 
 
############################ ############################### the ADVANCED the CONFIG ### 
# data amount less than or equal hash-max-ziplist-entries by ziplist, larger than the hash-max-ziplist-entries with the hash 
hash-max-ziplist-entries 512
#value size less than or equal hash-max-ziplist-value with ziplist, larger than the hash-max-ziplist-value with the hash. 
--max-ziplist the hash value 64 
 
# data amount less than or equal list-max-ziplist-entries with ziplist, larger than the list-max-ziplist-entries with the list. 
-max-ziplist-List 512 entries It 
#VALUE size less than or equal list-max-ziplist-value with ziplist, larger than the list-max-ziplist-value with list. 
-max-ziplist-List 64 value 
 
# is smaller than the data amount equal to set-max-intset-entries with iniset, is greater than the set-max-intset-entries with set. 
IntSet--max-SET 512 entries It 
 
# data amount less than or equal zset-max-ziplist-entries with ziplist, greater than zset-max-ziplist-entries with zset. 
-max-ziplist-zset 128 entries It 
#VALUE size less zset-max-ziplist-value with ziplist, greater than zset-max-ziplist-value with zset. 
zset-max-ziplist-value 64
 
#value size less than or equal hll-sparse-max-bytes data structure using a sparse (sparse), greater than hll-sparse-max-bytes data structure using dense (dense). A value larger than 16,000 is almost useless, the recommended value of around 3000. If you do not ask for the CPU, for space-sensitive, it is recommended set to around 10,000. 
-sparse-max-HLL 3000 bytes 
 
#Redis the CPU time of 1 millisecond every 100 milliseconds when the re-hash to the hash table redis, memory usage may be reduced. When you use scenes, there are very strict real-time needs, from time to time Redis can not accept a request for a 2-millisecond delay, then put this configuration is no. If there are no such strict real-time requirements, it can be set to yes, to be able to release memory as quickly as possible. 
Yes activerehashing 
 
## the client can force the output buffer to be limiting as those client does not read data from a server is disconnected, the slow closing force to transfer client. 
client-output-buffer-limit pubsub 32mb 8mb 60
# For normal client, first of 0 disables hard limit, second and third 0 of 0 disables soft limit, normal client cancel the default limit, because if not asked to find that they are not receiving data.
-client-output-buffer limit Normal 0 0 0 
# to slave client and MONITER client, if the client-output-buffer once over 256mb, and 64mb or more than 60 seconds, the server will immediately disconnect clients. 
-client-output-buffer 256MB 64MB limit Slave 60 
# For pubsub client, if the client-output-buffer once more than 32mb, or if more than 8mb for 60 seconds, then the server will immediately disconnect clients. 
 
Frequency #redis mission is 1s divided by hz. 
10 Hz 
 
# aof when rewriting, if open aof-rewrite-incremental-fsync switch, the system will be executed once every 32MB fsync. This file is written to the disk is helpful to avoid excessive delay in the peak. 
aof-rewrite-incremental-fsync yes

Guess you like

Origin www.cnblogs.com/vincentYw/p/12108629.html