redis common commands common errors configuration tips and more

  • 2020-05-09 19:36:16
  • OfStack

1. redis view all current key


KEYS *

2. View the current redis configuration information

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.

As a result, the redis run user has no permission to write rdb files or the disk space is full.


config set stop-writes-on-bgsave-error no

Such as:

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 dynamically set through set

5.(error) OOM command not allowed when used memory >
The maxmemory option is set, and the redis memory usage is capped.
You can free up space by setting the LRU algorithm to delete part of key.
The default is by expiration time. If the expiration time is not added to set, the data will be written to maxmemory.
32-bit systems use up to 3GB memory if you do not set maxmemory or if you set it to 0 64-bit without limiting memory.

volatile-lru - > Delete according to the expiration time generated by LRU algorithm.
allkeys-lru - > Delete any key according to the LRU algorithm.
volatile-random - > Randomly delete key based on the expiration setting.
allkeys- > random - > Random deletion without distinction.
volatile-ttl - > Delete according to the latest expiration date (supported by TTL)
noeviction - > No one is deleted, and an error is returned during the write operation.

6. reids log location

logfile logging, stdout by default, if set to stdout and run as a daemon, the log will be redirected to /dev/null, which means no logging.

7. Details of reids configuration parameters


#daemonize no  By default, redis Not running in the background, if you need to run in the background, change the value of the item to yes
daemonize yes
#  when redis When you're running in the background, Redis By default the pid Files in /var/run/redis.pid , you can configure to other addresses.
#  When running multiple redis When serving, you need to specify a different one pid Files and ports
pidfile /var/run/redis_6379.pid
#  The specified redis The port to run on, by default 6379
port 6379
#  In a high concurrency environment, setup is required to avoid slow client connection issues 1 High - speed background log
tcp-backlog 511
#  The specified redis Receive only from this IP Request for address, if not set, then all requests will be processed
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
#  Sets the timeout time in seconds when the client connects. When the client does not issue any instructions during this time, close the connection
# 0 Is to turn off this setting
timeout 0
# TCP keepalive
#  in Linux On, a value (in seconds) is specified for sending ACKs The time. Note that closing the connection takes twice as long. The default is 0 .
tcp-keepalive 0
#  Specify logging levels recommended for production environments notice
# Redis A total of support 4 Two levels: debug , verbose , notice , warning By default, verbose
# debug     Record a lot of information for development and testing
# varbose   Useful information, unlike debug It's going to record that much
# notice    ordinary verbose , often used in production environments
# warning   Only very important or serious information is logged
loglevel notice
#  configuration log Address of the file
#  The default value is stdout , standard output, if the background mode will output to /dev/null .
logfile /var/log/redis/redis.log
#  Number of available databases
#  The default value is 16 , the default database is 0 , the database scope is 0- ( database-1 ) between
databases 16
################################ The snapshot #################################
#  Save the data to disk in the following format :
#   save 
#    Indicates how many update operations are performed over how long a period of time to synchronize the data to the data file rdb .
#    This is equivalent to a conditional snapshot trigger, which can be combined with multiple conditions
#    For example, the Settings in the default configuration file are set 3 A condition
#   save 900 1  900 At least in seconds 1 a key Be changed
#   save 300 10  300 At least in seconds 300 a key Be changed
#   save 60 10000  60 At least in seconds 10000 a key Be changed
# save 900 1
# save 300 10
# save 60 10000
#  Background storage error stops write.
stop-writes-on-bgsave-error yes
#  When stored to a local database (persisted to rdb File) whether to compress data, default is yes
rdbcompression yes
# RDB Whether the file is a direct icon chcksum
rdbchecksum yes
#  Local persistent database file name with a default value of dump.rdb
dbfilename dump.rdb
#  Working directory
#  The path where the database image backup files are placed.
#  The reason the path and file name are configured separately here is that redis When doing a backup, the state of the current database is written to 1 Two temporary files, and when the backup is completed,
#  The temporary file is then replaced with the file specified above, and both the temporary file and the backup file configured above are placed in the specified path.
# AOF Files will also be stored in this directory
#  Notice that you have to specify here 1 Directories instead of files
dir /var/lib/redis-server/
################################# copy #################################
#  A master-slave replication . Set this database to be a slave to another database .
#  Set when the native is slav Service, set master The service of IP Address and port, in Redis When started, it will automatically start from master Data synchronization
# slaveof
#  when master When the service is password protected ( with requirepass Formulated password )
# slave Service connection master The password
# masterauth
#  When the slave library loses connection to the host or replication is in progress, the slave library has two modes of operation:
# 1)  if slave-serve-stale-data Set to yes( The default Settings ) , the slave library continues to respond to the client's request
# 2)  if slave-serve-stale-data Refers to as no To go out INFO and SLAVOF Any request outside the command is returned 1 a
#     error "SYNC with master in progress"
slave-serve-stale-data yes
#  configuration slave Whether the instance accepts writes. write slave To store transient data (in the same master Data can be easily deleted after synchronization) is useful, but client writes can send problems if not configured.
#  from Redis2.6 Later, the default slave for read-only
slaveread-only yes
#  The slave library will follow 1 Is sent to the main library at intervals of PINGs. Can be achieved by repl-ping-slave-period Set this interval by default 10 seconds
# repl-ping-slave-period 10
# repl-timeout  Set the batch data transfer time of the master library or ping The default value is 60 seconds
#  1 To ensure that repl-timeout Is greater than repl-ping-slave-period
# repl-timeout 60
#  in slave socket the SYNC After disabling TCP_NODELAY
#  If you choose" yes " ,Redis Will use the 1 A smaller number TCP Packets and less bandwidth to send data to slave . But this can cause data to be sent to slave There will be a delay at the end , If it is Linux kernel The default configuration will be reached 40 ms .
#  If you choose to "no" , then send the data to slave The latency at the end 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 replicated background log, slave The longer it takes to disconnect and possibly later perform a partial replication.
#  Background logs are at least there 1 a slave When connected, only allocate 1 Times.
# repl-backlog-size 1mb
#  in master No longer connect slave After that, the background log will be released. The following configuration definition starts at the end 1 a slave Time to be released after disconnection (seconds).
# 0 This means that the background logs are never released
# repl-backlog-ttl 3600
#  if master Can no longer work normally, then will be in multiple slave , select the one with the lowest priority 1 a slave Promoted to master , the priority value is 0 Can not be promoted to master .
slave-priority 100
#  If less than N a slave Connect, and delay time <=M Second, master Can be configured to stop accepting writes.
#  For example need at least 3 a slave Connect, and delay <=10 Second configuration:
# min-slaves-to-write 3
# min-slaves-max-lag 10
#  Set up the 0 For the disabled
#   The default min-slaves-to-write for 0 (disabled), min-slaves-max-lag for 10
################################## security ###################################
#  Set the password you need to use after the client connection before making any other specification.
#  Warning: because redis It's pretty fast, so in the 1 Under a better server, 1 Eight external users are available 1 Seconds to 150K Second password attempt, which means you need to specify a very, very strong password to prevent brute force
# requirepass foobared
#  Command rename .
#  in 1 You can rename relatively dangerous commands in a Shared environment. Such as the CONFIG Wish for 1 It's not easy to guess.
#  For example, :
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#  If you want to delete 1 Rename it to 1 A null character "" Yes, as follows:
# rename-command CONFIG ""
################################### The constraint ###################################
# Set up with 1 Time maximum client connections, default unlimited,
#Redis The number of client connections that can be opened simultaneously is Redis The maximum number of file descriptors that a process can open,
# If set   maxclients 0 , means no restriction.
# When the number of client connections reaches the limit, Redis The new connection is closed and returned to the client max number of clients reached The error message
# maxclients 10000
#  The specified Redis Maximum memory limit, Redis At startup, the data is loaded into memory, and when the maximum memory is reached, Redis Attempts will be made to clear expired items according to the clear policy Key
#  if Redis Cannot provide enough space after clearing according to the policy, or the policy is set to " noeviction " , then commands that use more space will report an error, for example SET, LPUSH And so on. But you can still do a read
#  Note: Redis The new vm The mechanism will take Key Store memory, Value Will be stored in a swap area
#  The option to LRU Strategy works.
# maxmemory The Settings are more suitable for the redis Treat as analogous to memcached The cache is used and not appropriate as 1 A real DB .
#  When the Redis As a 1 When a real database is used, the memory usage will be 1 It's a big expense
# maxmemory
#  When memory is at its maximum Redis Which data will you choose to delete? There are 5 There are several ways to choose from
# volatile-lru ->  using LRU Algorithm to remove the set expiration time key (LRU: Recently used   Least RecentlyUsed )
# allkeys-lru ->  using LRU The algorithm removes any key
# volatile-random ->  Removes randomness that has been set to expire key
# allkeys->random -> remove a randomkey, any key
# volatile-ttl ->  Remove items that are about to expire key(minor TTL)
# noeviction ->  Don't remove anything you can, just return 1 A write error
#  Note: for the above strategy, if none is appropriate key You can remove it when you write it Redis Returns the 1 A mistake
#  The default is :  volatile-lru
# maxmemory-policy volatile-lru 
# LRU  and   minimal TTL The algorithm is not a precise algorithm, but a relatively precise algorithm ( To save memory ) You can choose the sample size to test.
# Redis Default grey selection 3 One sample to test, you can pass maxmemory-samples set
# maxmemory-samples 3
############################## AOF###############################
#  By default, redis The database image will be asynchronously backed up to disk in the background, but the backup is very time-consuming, and the backup is not very frequent, if there is such a situation as power cut, unplug, then will cause a large range of data loss.
#  so redis Provides additional 1 A more efficient way of database backup and disaster recovery.
#  open append only After the pattern, redis It will take everything it receives 1 Each write request is appended to appendonly.aof In the file, when redis When restarted, the previous state is restored from the file.
#  But that's what happens appendonly.aof The file is too big, so redis Also supports the BGREWRITEAOF Instruction, appendonly.aof Reorganize.
#  You can turn it on at the same time asynchronous dumps and   AOF
appendonly no
# AOF The file name   ( The default : "appendonly.aof")
# appendfilename appendonly.aof
# Redis support 3 A synchronous AOF Document policy :
# no:  Without synchronization, the system operates   . Faster.
# always: always Means that each write operation is synchronized . Slow, Safest.
# everysec:  Represents the accumulation of write operations, synchronized per second 1 time . Compromise.
#  The default is "everysec" This is the best compromise between speed and safety.
#  If you want Redis To run more efficiently, you can also set the "no" , let the operating system decide when to execute
#  Or if you want to make your data more secure you can set it to zero "always"
#  Use it if you're not sure   "everysec".
# appendfsync always
appendfsync everysec
# appendfsync no
# AOF Policy set to always or everysec , the background processing process ( Background save or AOF Log to rewrite ) It does a lot of things I/O operation
#  In some Linux Overlength is prevented in the configuration fsync() The request. Note that there are no fixes now, even though fsync On the other 1 It is processed by three threads
#  To mitigate the problem, set the following parameter no-appendfsync-on-rewrite
no-appendfsync-on-rewrite no
# AOF  Automatic override
#  when AOF File growth to 1 When you set the size Redis To be able to call   BGREWRITEAOF  Rewrite the log file
#  Here's how it works: Redis Will remember the size of the file since the last log ( If you haven't overwritten since startup, the date size will be determined at startup )
#  The base size is compared to the current size. If the current size is larger than the base size specified by a percentage, the rewrite function will be enabled
#  You also need to specify 1 The minimum size for AOF Rewrite, this is used to prevent the file from being rewritten even if the file is small but growing AOF File status
#  Set up the   percentage for 0 Just turn this feature off
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################################ LUASCRIPTING #############################
# 1 a Lua The maximum execution time of a script is 5000 Milliseconds ( 5 Seconds), if is 0 Or negative Numbers indicate infinite execution time.
lua-time-limit 5000
################################LOW LOG################################
# Redis Slow Log  Logs commands that exceed a specified execution time. Execution time is not included I/O Computes, such as connecting clients, returning results, etc., just command execution time
#  You can do this with two parameters slow log : 1 One is to tell Redis Execute the parameter over how long it has been logged slowlog-log-slower-than( subtle ) .
#  On the other 1 One is slow log The length of the. when 1 The earliest command will be removed from the queue when the new command is logged
#  The following time is in units of subtlety, therefore 1000000 On behalf of 1 Seconds.
#  Pay attention to the specified 1 Negative Numbers will turn off the slow log and set to 0 Each command will be forced to be logged
slowlog-log-slower-than 10000
#  There is no limit to log length, just be aware that it consumes memory
#  Can be achieved by   SLOWLOG RESET Reclaim memory consumed by slow logging
#  Default values are recommended 128 , when the slow log exceeds 128 , the first entry to the queue is kicked out
slowlog-max-len 128
################################  Event notification   #############################
#  When an event occurs, Redis You can notice Pub/Sub The client.
#  You can choose from the following table Redis The type of event to be notified. Event types are identified by a single character:
# K     Keyspace Events to _keyspace@_ Is published as a prefix
# E     Keyevent Events to _keysevent@_ Is published as a prefix
# g     Generic event (no type specified), like DEL, EXPIRE, RENAME, ...
# $     String The command
# s     Set The command
# h     Hash The command
# z     Ordered set command
# x     Expiration event (every time key Generated when expired)
# e     Clear the event (when key Generated when memory is cleared)
# A     g$lshzxe hence " AKE " It means everything
# notify-keyspace-events with 1 by 0 To a string parameter composed of more than one character. An empty string means that the notification is disabled.
#  Example: enable list And general events:
# notify-keyspace-events Elg
#  Notifications used by default are disabled because the user usually does not need to change the feature and there is a performance penalty for that feature.
#  Note if you don't specify at least K or E the 1 , no event will be sent.
notify-keyspace-events ""
##############################  Advanced configuration   ###############################
#  when hash Contains more than the specified number of elements and the largest element does not exceed the critical value,
# hash Will be in 1 These two thresholds can be set for storage in a special way that greatly reduces memory usage
# Redis Hash The corresponding Value The internal reality is 1 a HashMap Actually there will be 2 Different implementations,
#  this Hash When there are fewer members Redis Something similar is used to save memory 1 Dimensional arrays are stored compactly, without the real thing HashMap Structure, corresponding to valueredisObject the encoding for zipmap,
#  As the number of members increases, it will automatically become real HashMap, At this time encoding for ht .
hash-max-zipmap-entries 512
hash-max-zipmap-value 64 
#  and Hash 1 B: yes, several small ones list Code in a specific way to save space.
# list Data type node value sizes smaller than what bytes are stored in a compact format.
list-max-ziplist-entries 512
list-max-ziplist-value 64
# set Internal data of a data type is stored in a compact format if it is all numeric and how many nodes it contains.
set-max-intset-entries 512
#  and hashe and list 1 sample , Sort of set Saves space by storing in the specified encoding for a specified length
# zsort Data type node value sizes smaller than what bytes are stored in a compact format.
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# Redis Will be in every 100 Millisecond use 1 ms CPU Time to redis the hash Table reset hash Can reduce the memory usage
#  When you use the scene, there are very strict real-time needs, can not be accepted Redis From time to time there are requests 2 For a millisecond delay, configure this to no .
#  If there is no such strict real-time requirement, it can be set as yes In order to free up memory as quickly as possible
activerehashing yes
# The limit of the client's output buffer, because for some reason the client can't read the data from the server fast enough,
# Can be used to force disconnection ( 1 One of the most common reasons 1 A release / Subscribing clients cannot consume messages as fast as they can produce them.
#  can 3 Setup in different client ways:
# normal ->  Normal client
# slave  -> slave and MONITOR The client
# pubsub ->  At least subscribed 1 a pubsub channel or pattern The client
#  each client-output-buffer-limit grammar :
# client-output-buffer-limit  
#  1 Once the hard limit is reached, the client disconnects immediately, or the soft limit is reached and the specified number of seconds (continuous) is reached.
#  For example, if the hard limit is 32 Megabytes and soft limits are 16 megabytes /10 Second, the client will disconnect immediately
#  If the size of the output buffer is reached 32 Megabytes, client reached 16 Megabytes and contiguous exceeded the limit 10 Seconds, will also disconnect.
#  The default normal Clients do not make restrictions because they are in 1 No data will be received (in the form of a push) if not requested after a request,
#  Only an asynchronous client can possibly request data faster than it can read it.
#  Set both the hard limit and the soft limit to 0 To disable the feature
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb60
client-output-buffer-limit pubsub 32mb 8mb60
# Redis Internal functions are called to perform many background tasks, such as closing client timeout connections and clearing expired ones Key , and so on.
#  Not all tasks are performed at the same frequency, however Redis In accordance with the specified" Hz "Value to perform the check task.
#  By default," Hz "Is set as 10 .
#  Increasing the value will be in Redis Use more in your free time CPU When, but at the same time when there are more than one key A simultaneous expiration will cause Redis Are more responsive and timeouts can be handled more accurately.
#  The range is 1 to 500 Between, but the value exceeds 100 Is usually not 1 That's a good idea.
#  Most users should 10 This default value is only necessary for very low latency situations where it is necessary to raise the maximum to 100 .
hz 10 
#  when 1 Child node rewrite AOF When a file is created, if the following options are enabled, the file is generated each time 32M Data is synchronized.
aof-rewrite-incremental-fsync yes

8.Redis official documentation recommendations for the use of VM:

When your key is small and your value is large, VM works better because it saves more memory.
When your key is not too small, consider using some unusual methods to turn a large key into a large value. For example, consider combining key,value into a new value.
It is better to use linux ext3 to save your swap files on a file system with good support for sparse files.
As for the parameter of vm-max-threads, you can set the number of threads to access swap file, and it is better to set it not to exceed the machine's kernel. If it is set to 0, then all operations on swap file will be serial, which may cause a relatively long delay, but it is a good guarantee for data integrity.
With the VM feature,Redis is finally free of the memory limitation nightmare, and it seems that we can call it Redis database, and we can imagine how many new USES can be generated. Hopefully, this feature will not affect the memory performance of Redis's original excellent B.

9. redis modifies persistent path and log path


vim redis.conf
logfile /data/redis_cache/logs/redis.log # Log path

dir /data/redis_cache # persistent path. Remember to copy the dump.rdb persistent file to /data/redis_cache

First kill redis, copy dump.rdb, start

10. Clear redis cache


./redis-cli    # Enter the
dbsize
flushall     # perform
exit

11. Delete all Key in the current redis database


flushdb

12. Delete key from all databases of redis

flushall


Related articles: