Redis common commands, common errors, configuration skills, etc

Time:2020-3-12

1. Redis view all current keys

Copy codeThe code is as follows:
KEYS *

2. View the current redis configuration information

Copy codeThe code is as follows:
CONFIG GET *

3. MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.

When the redis snapshot is forced to stop, the redis running user does not have permission to write the RDB file or the disk space is full. Solution:

Copy codeThe code is as follows:
config set stop-writes-on-bgsave-error no

For example:

Copy codeThe code is as follows:
set ‘name’ ‘shenhui’
-MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
config set stop-writes-on-bgsave-error no
+OK
set ‘name’ ‘shenhui’
+OK

4. redis 127.0.0.1:6379> CONFIG SET logfile “/var/log/redis/redis-server.log”
(error) ERR Unsupported CONFIG parameter: logfile

Logfile cannot be set dynamically through set

5.(error) OOM command not allowed when used memory >
The maxmemory option is set, and redis memory usage reaches the upper limit.
You can set LRU algorithm to delete some keys and free space.
The default is based on the expiration time. If the expiration time is not added during the setting, the data will be filled with maxmemory.
If maxmemory is not set or 0 64 bit system is set to unlimited memory, 32-bit system can use up to 3gb memory.

Volatile LRU – > delete based on the expiration time generated by LRU algorithm.
Allkeys LRU – > delete any key according to LRU algorithm.
Volatile random – > randomly delete keys based on expiration settings.
All keys – > Random – > random.
Volatile TTL – > delete based on the latest expiration time (supplemented by TTL)
Noeviction – > no one will delete it, and an error will be returned when writing directly.

6. Reids log location

Logfile logging mode, the default value is stdout. If it is set to stdout and running as a daemons, the log will be redirected to / dev / null, that is, the log will not be recorded.

7. Details of reids configuration parameters

Copy codeThe code is as follows:
#By default, redis is not running in the background. If you need to run in the background, change the value of this item to yes
daemonize yes
#When redis is running in the background, redis will put the PID file in / var / run / redis.pid by default. You can configure it to another address.
#When running multiple redis services, you need to specify different PID files and ports
pidfile /var/run/redis_6379.pid
#Specify the port on which redis runs. The default is 6379
port 6379
#In a highly concurrent environment, to avoid the connection problem of slow clients, a high-speed background log needs to be set
tcp-backlog 511
#Specifies that redis only receives requests from the IP address. If it is not set, all requests will be processed
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
#Sets the timeout in seconds for client connections. When the client does not issue any instructions during this period, close the connection
#0 is to turn off this setting
timeout 0
# TCP keepalive
#On Linux, specify the time (in seconds) for sending acks. Note that it takes twice as long to close the connection. The default is 0.
tcp-keepalive 0
#Specify the logging level. Notice is recommended for production environment
#Redis supports four levels: debug, verbose, notice, and warning. The default is verbose
#Debug records a lot of information for development and testing
#Varbose’s useful information is not as much as debug records
#Notice common verbose, commonly used in production environment
#Warning only very important or serious information will be recorded in the log
loglevel notice
#Configure log file address
#The default value is stdout, standard output. If the background mode is output to / dev / null.
logfile /var/log/redis/redis.log
#Number of databases available
#The default value is 16, the default database is 0, and the database range is between 0 – (database-1)
databases 16
################################Snapshot#################################
#Save data to disk in the following format:
#   save 
#It is pointed out that in how long and how many update operations there are, the data will be synchronized to the data file RDB.
#It is equivalent to a condition triggered snapshot, which can be matched by multiple conditions
#For example, three conditions are set in the default configuration file
#Save 900 at least one key is changed in 900 seconds
#Save 300 at least 300 keys are changed in 10 300 seconds
#Save 60 10000 at least 10000 keys changed in 60 seconds
# save 900 1
# save 300 10
# save 60 10000
#Background storage error stop writing.
stop-writes-on-bgsave-error yes
#Whether to compress data when it is stored in the local database (persisted to the RDB file) is yes by default
rdbcompression yes
#Is the RDB file directly idolized by chcksum
rdbchecksum yes
#Local persistent database file name, default value is dump.rdb
dbfilename dump.rdb
#Working directory
#The path where the files for the database mirror backup are placed.
#The path and filename here should be configured separately because redis will write the current database status to a temporary file before the backup is completed,
#Then replace the temporary file with the file specified above, and the temporary file and the backup file configured above will be placed in the specified path.
#Aof files will also be stored in this directory
#Note that a directory, not a file, must be created here
dir /var/lib/redis-server/
#################################Copy#################################
#Master – slave replication. Set this database as the slave database of other databases
#Set the IP address and port of the master service when the local machine is Slav service. When redis is started, it will automatically synchronize data from the master
# slaveof
#When the master service is password protected (using the password specified by requirepass)
#Password for slave service to connect to master
# masterauth
#When the slave library loses connection with the host or the replication is in progress, the slave library can run in two ways:
#1) if slave serve stale data is set to Yes (the default), the slave database will continue to respond to the client’s request
#2) if slave serve stale data is no, any request other than the info and slavof commands will return a
#Error “sync with master in progress”
slave-serve-stale-data yes
#Configure whether the slave instance accepts writes. Writing slave is useful for storing transient data (which can be easily deleted after synchronization with master data), but without configuration, client writing may cause sending problems.
#After redis2.6, the default slave is read only
slaveread-only yes
#The slave database will send pings to the master database at a time interval. You can set this time interval through repl Ping slave period, which is 10 seconds by default
# repl-ping-slave-period 10
#Repl timeout sets the batch data transmission time or Ping reply time interval of the main database. The default value is 60 seconds
#Make sure that the repl timeout is greater than the repl Ping slave period
# repl-timeout 60
#Disable TCP? Nodelay after sync of slave socket
#If “yes” is selected, redis will use a smaller digital TCP packet and less bandwidth to send data to the slave, but this may cause delay in sending data to the slave. If it is the default configuration of Linux kernel, it will reach 40 milliseconds
#If “no” is selected, the delay of sending data to the slave will be reduced, but more bandwidth will be used for replication
repl-disable-tcp-nodelay no
#Set the background log size for replication.
#The larger the background logs copied, the longer it takes for the slave to disconnect and possibly perform partial replication later.
#Background logs are allocated only once when there is at least one slave connection.
# repl-backlog-size 1mb
#After the master is no longer connected to the slave, the background log will be released. The following configuration defines the time (in seconds) to release after disconnecting from the last slave.
#0 means never release background logs
# repl-backlog-ttl 3600
#If the master can no longer work normally, the slave with the lowest priority value will be selected to be promoted to master in multiple slaves. If the priority value is 0, it means that it cannot be promoted to master.
slave-priority 100
#If there are less than n slave connections and the delay time is < = m seconds, the master can be configured to stop accepting writes.
#For example, you need to configure at least 3 slave connections with a delay of < 10 seconds:
# min-slaves-to-write 3
# min-slaves-max-lag 10
#Set 0 to disabled
#Default min slaves to write is 0 (disabled), min slaves Max lag is 10
##################################Safety###################################
#Set the password to be used before any other assignments are made after the client connects.
#Warning: because redis is very fast, an external user can make 150k password attempts in a second on a good server, which means you need to specify a very powerful password to prevent violent cracking
# requirepass foobared
#Command rename
#You can rename relatively dangerous commands in a shared environment. For example, rename config to a character that is not easy to guess.
Example:
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#If you want to delete a command, rename it to an empty character, as follows:
# rename-command CONFIG “”
###################################Constraints###################################
#Set the maximum number of client connections at the same time, unlimited by default,
#The number of client connections that redis can open at the same time is the maximum number of file descriptors that redis process can open,
#If maxclients 0 is set, there is no restriction.
#When the number of client connections reaches the limit, redis will close the new connection and return the max number of clients reached error message to the client
# maxclients 10000
#Specify the maximum memory limit of redis. When redis starts, it will load the data into the memory. When the maximum memory is reached, redis will try to clear the expired key according to the clearing policy
#If redis fails to provide enough space after clearing according to the policy, or the policy is set to “noeviction”, the command using more space will report an error, such as set, lpush, etc. But it can still read
#Note: redis’s new VM mechanism will store key in memory and value in swap area
#This option is useful for LRU strategies.
#Maxmemory is more suitable to use redis as a memcached like cache than as a real dB.
#When redis is used as a real database, memory usage will be a big overhead
# maxmemory
#When the memory reaches the maximum, what data will redis choose to delete? There are five ways to choose
#Volatile LRU – > use LRU algorithm to remove the key with expiration time set (LRU: last recently used)
#Allkeys LRU – > remove any key using LRU algorithm
#Volatile random – > remove random key with expiration time set
# allkeys->random -> remove a randomkey, any key
#Volatile TTL – > remove expiring key (minor TTL)
#Noeviction – > do not remove anything but return a write error
#Note: for the above policy, if there is no appropriate key to remove, redis will return an error when writing
#The default is: volatile LRU
# maxmemory-policy volatile-lru 
#LRU and minimal TTL are not accurate algorithms, but relatively accurate algorithms (in order to save memory), you can choose the sample size for detection at will.
#Redis’s default gray selection is 3 samples for testing. You can set them through maxmemory samples
# maxmemory-samples 3
############################## AOF###############################
#By default, redis will asynchronously back up the database image to disk in the background, but the backup is very time-consuming, and the backup can not be very frequent. If there is a situation such as power rationing and unplugging, a large range of data will be lost.
#So redis provides another more efficient way of database backup and disaster recovery.
#After the append only mode is turned on, redis will append every write request received to the appendonly.aof file. When redis restarts, it will restore the previous state from the file.
#However, this will cause the appendonly.aof file to be too large, so redis also supports the bgrewriteaof instruction to reorganize appendonly.aof.
#You can turn on asynchronous dumps and AOF at the same time
appendonly no
#Aof file name (default: “appendonly. AOF”)
# appendfilename appendonly.aof
#Redis supports three strategies for synchronizing AOF files:
#No: no synchronization, system operation. Fast
#Always: always means to synchronize every write operation. Slow, safe
#Everysec: indicates that write operations are accumulated and synchronized once a second. Compress
#The default is “everysec”, which is the best compromise between speed and security.
#If you want redis to run more efficiently, you can also set “no” to let the operating system decide when to execute
#Or on the contrary, if you want to make the data more secure, you can also set it to “always”
#If you are not sure, use “everysec”
# appendfsync always
appendfsync everysec
# appendfsync no
#When the AOF policy is set to always or everysec, the background processing process (background saving or AOF log rewriting) will perform a lot of I / O operations
#Too long fsync() requests are blocked in some Linux configurations. Note that there is no fix now, even if fsync is working on another thread
#To alleviate this problem, you can set the following parameter no appendfsync on rewrite
no-appendfsync-on-rewrite no
#Aof auto override
#When the AOF file grows to a certain size, redis can call bgrewriteaof to rewrite the log file
#It works like this: redis will remember the size of the file after the last log (if it has not been rewritten since the startup, the size of the day will be determined when the startup)
#The base size is compared to the current size. If the current size is a percentage larger than the base size, the override function will start
#At the same time, we need to specify a minimum size for AOF rewriting, which is used to prevent the rewriting of AOF files even if the files are small but the growth rate is large
#Set the percentage to 0 to turn off this feature
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################################ LUASCRIPTING #############################
#The maximum execution time of a Lua script is 5000 milliseconds (5 seconds). If it is 0 or negative, it means infinite execution time.
lua-time-limit 5000
################################LOW LOG################################
#Redis slow log records commands that have exceeded a specific execution time. The execution time does not include the I / O calculation, such as connecting the client and returning the result, but only the command execution time
#Slow log can be set through two parameters: one is to tell redis how long it has been recorded to execute the parameter slowlog slow than (subtle),
#The other is the length of slow log. When a new command is logged, the oldest command is removed from the queue
#The time below is in subtle units, so 1000000 represents one second.
#Note that specifying a negative number turns off slow logging, while setting to 0 forces each command to log
slowlog-log-slower-than 10000
#There is no limit on log length, just be aware that it consumes memory
#The memory consumed by slow log can be recovered through slow log reset
#The default value of 128 is recommended. When the slow log exceeds 128, the record that enters the queue first will be kicked out
slowlog-max-len 128
################################Event notification#############################
#When an event occurs, redis can notify the pub / sub client.
#You can select the event type to be notified by redis in the following table. Event types are identified by a single character:
#K keyspace event, which is published with the prefix of “keyspace @”
#E keyevent event, published with the prefix “keyevent @”
#G. general events (no type specified), such as del, exit, rename
#$string command
#S set command
#H hash command
#Z ordered set command
#X expiration event (generated every time the key expires)
#E. clear event (generated when the key is cleared in memory)
#A G $lshzxe, so “ake” means all events
#Notify keyspace events takes a string parameter of 0 to more than one character. An empty string means that notifications are disabled.
#Example: enable list and common events:
# notify-keyspace-events Elg
#Notifications used by default are disabled because users usually don’t need to change the feature, and the feature has a performance penalty.
#Note that if you do not specify at least one of K or E, no events will be sent.
notify-keyspace-events “”
##############################Advanced configuration###############################
#When the hash contains more than the specified number of elements and the maximum element does not exceed the threshold,
#Hash will be stored in a special encoding way (greatly reducing memory usage), and these two thresholds can be set here
#The value corresponding to redis hash is actually a HashMap. There are two different implementations,
#When the number of members of this hash is relatively small, redis will adopt the way similar to one-dimensional array for compact storage in order to save memory, rather than the real HashMap structure. The encoding of the corresponding valueredisobject is zipmap,
#When the number of members increases, it will automatically turn into a real HashMap, and the encoding is HT.
hash-max-zipmap-entries 512
hash-max-zipmap-value 64 
#Like hash, multiple small lists are encoded in a specific way to save space.
#List data type node value size less than how many bytes will be in compact storage format.
list-max-ziplist-entries 512
list-max-ziplist-value 64
#If the internal data of set data type is all numerical type, and the number of nodes below it will be stored in a compact format.
set-max-intset-entries 512
#Like hashes and lists, sorted sets are stored in a specified encoding within a specified length to save space
#Zsort data type node value size less than how many bytes will adopt compact storage format.
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
#Redis will use 1 millisecond CPU time every 100 milliseconds to rehash the hash table of redis, which can reduce the use of memory
#When you have very strict real-time needs in your use scenario, and cannot accept redis’s 2-millisecond delay on requests from time to time, configure this as No.
#If you don’t have such strict real-time requirements, you can set it to yes to free up memory as soon as possible
activerehashing yes
#The output buffer of the client is limited. For some reason, the client cannot read the data from the server fast enough,
#Can be used to force disconnection (a common reason is that a publish / subscribe client cannot consume messages as fast as it produces them).
#You can set it in three different ways:
#Normal – > normal client
#Slave – > slave and monitor clients
#PubSub – > a client that has subscribed to at least one PubSub channel or pattern
#Syntax of each client output buffer limit:
# client-output-buffer-limit  
#Once the hard limit is reached, the client will be disconnected immediately, or the soft limit will be reached and the specified number of seconds (continuous) will be reached.
#For example, if the hard limit is 32 megabytes and the soft limit is 16 megabytes / 10 seconds, the client will immediately disconnect
#If the size of the output buffer reaches 32 megabytes, the client reaches 16 megabytes and continuously exceeds the limit for 10 seconds, the connection will also be disconnected.
#By default, normal clients do not have restrictions because they do not receive data when they are not requested after a request,
#Only asynchronous clients may have scenarios that request data faster than it can read.
#Set both hard and soft limits to 0 to disable this feature
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb60
client-output-buffer-limit pubsub 32mb 8mb60
#Redis calls internal functions to perform many background tasks, such as closing the client’s timeout connection, clearing the expired key, and so on.
#Not all tasks are executed at the same frequency, but redis performs the check task according to the specified “Hz” value.
#By default, the value of “Hz” is set to 10.
#Increasing this value will use more CPUs when redis is idle, but at the same time, when multiple Keys expire at the same time, redis will be more responsive, and timeout can be handled more accurately.
#The range is between 1 and 500, but a value over 100 is usually not a good idea.
#Most users should use the default value of 10, and it is only necessary to increase it to 100 at a very low delay.
hz 10 
#When a child node rewrites an AOF file, if the following options are enabled, the file synchronizes every 32m of data generated.
aof-rewrite-incremental-fsync yes

8. Suggestions for using VM in redis official documents:

When your key is very small and value is very large, the effect of using VM will be better, because this saves a lot of memory
When your key is not small, you can consider using some special methods to change a large key into a large value. For example, you can consider combining key and value into a new value
It’s best to save your swap files using a file system with good support for sparse files, such as Linux ext3
The VM Max threads parameter can be used to set the number of threads accessing the swap file. It is better not to exceed the number of cores of the machine. If it is set to 0, all operations on the swap file are serial. It may cause a long delay, but it has a good guarantee for data integrity
With the VM function, redis finally got rid of the nightmare of limited memory capacity. It seems that we can call it redis database. We can also imagine how many new uses can be produced. Of course, I hope this function will not affect the original memory storage performance of redis, which is very good

9. Redis modifies persistence path and log path

Copy codeThe code is as follows:
vim redis.conf
Logfile / data / redis? Cache / logs / redis.log? Log path

Dir / data / redis ﹐ cache ﹐ persistence path. Remember to copy the dump.rdb persistence file to / data / redis ﹐ cache after modification

Kill redis, copy dump.rdb, and start

10. Clear redis cache

Copy codeThe code is as follows:
. / redis cli
dbsize
Flushall ා execution
exit

11. Delete all keys in the current redis database

Copy codeThe code is as follows:
flushdb

12. Delete the key in all databases of redis

Copy codeThe code is as follows:
flushall