Summarize the common commands of Redis operation under Linux

Posted by killah on Sat, 19 Feb 2022 20:39:44 +0100

1. Install redis for Ubuntu / CentOS (redis5.0.8 does not need to update gcc)

2. Start redis service and verify

If it is in the bin directory under the redis file, the command:
redis-server /etc/redis/redis.conf # start redis service

If it is in the redis file
/bin/redis-server /etc/redis.conf # start redis service

3. Connect to redis

redis-cli -h host -p port -a password

Host: remote redis server host

Port: remote redis service port

Password: remote redis service password (if there is no password, the - a parameter is not required)

redis-cli -h -p 6379

4. Use * to get all configuration items

config get *

  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "cluster-announce-ip"
  8) ""
  9) "unixsocket"
 10) ""
 11) "logfile"
 12) "/var/log/redis/redis-server.log"
 13) "pidfile"
 14) "/var/run/redis/"
 15) "slave-announce-ip"
 16) ""
 17) "maxmemory"
 18) "0"
 19) "proto-max-bulk-len"
 20) "536870912"
 21) "client-query-buffer-limit"
 22) "1073741824"
 23) "maxmemory-samples"
 24) "5"
 25) "lfu-log-factor"
 26) "10"
 27) "lfu-decay-time"
 28) "1"
 29) "timeout"
 30) "0"
 31) "active-defrag-threshold-lower"
 32) "10"
 33) "active-defrag-threshold-upper"
 34) "100"
 35) "active-defrag-ignore-bytes"
 36) "104857600"
 37) "active-defrag-cycle-min"
 38) "25"
 39) "active-defrag-cycle-max"
 40) "75"
 41) "auto-aof-rewrite-percentage"
 42) "100"
 43) "auto-aof-rewrite-min-size"
 44) "67108864"
 45) "hash-max-ziplist-entries"
 46) "512"
 47) "hash-max-ziplist-value"
 48) "64"
 49) "list-max-ziplist-size"
 50) "-2"
 51) "list-compress-depth"
 52) "0"
 53) "set-max-intset-entries"
 54) "512"
 55) "zset-max-ziplist-entries"
 56) "128"
 57) "zset-max-ziplist-value"
 58) "64"
 59) "hll-sparse-max-bytes"
 60) "3000"
 61) "lua-time-limit"
 62) "5000"
 63) "slowlog-log-slower-than"
 64) "10000"
 65) "latency-monitor-threshold"
 66) "0"
 67) "slowlog-max-len"
 68) "128"
 69) "port"
 70) "6379"
 71) "cluster-announce-port"
 72) "0"
 73) "cluster-announce-bus-port"
 74) "0"
 75) "tcp-backlog"
 76) "511"
 77) "databases"
 78) "16"
 79) "repl-ping-slave-period"
 80) "10"
 81) "repl-timeout"
 82) "60"
 83) "repl-backlog-size"
 84) "1048576"
 85) "repl-backlog-ttl"
 86) "3600"
 87) "maxclients"
 88) "10000"
 89) "watchdog-period"
 90) "0"
 91) "slave-priority"
 92) "100"
 93) "slave-announce-port"
 94) "0"
 95) "min-slaves-to-write"
 96) "0"
 97) "min-slaves-max-lag"
 98) "10"
 99) "hz"
100) "10"
101) "cluster-node-timeout"
102) "15000"
103) "cluster-migration-barrier"
104) "1"
105) "cluster-slave-validity-factor"
106) "10"
107) "repl-diskless-sync-delay"
108) "5"
109) "tcp-keepalive"
110) "300"
111) "cluster-require-full-coverage"
112) "yes"
113) "cluster-slave-no-failover"
114) "no"
115) "no-appendfsync-on-rewrite"
116) "no"
117) "slave-serve-stale-data"
118) "yes"
119) "slave-read-only"
120) "yes"
121) "stop-writes-on-bgsave-error"
122) "yes"
123) "daemonize"
124) "yes"
125) "rdbcompression"
126) "yes"
127) "rdbchecksum"
128) "yes"
129) "activerehashing"
130) "yes"
131) "activedefrag"
132) "no"
133) "protected-mode"
134) "no"
135) "repl-disable-tcp-nodelay"
136) "no"
137) "repl-diskless-sync"
138) "no"
139) "aof-rewrite-incremental-fsync"
140) "yes"
141) "aof-load-truncated"
142) "yes"
143) "aof-use-rdb-preamble"
144) "no"
145) "lazyfree-lazy-eviction"
146) "no"
147) "lazyfree-lazy-expire"
148) "no"
149) "lazyfree-lazy-server-del"
150) "no"
151) "slave-lazy-flush"
152) "no"
153) "maxmemory-policy"
154) "noeviction"
155) "loglevel"
156) "notice"
157) "supervised"
158) "no"
159) "appendfsync"
160) "everysec"
161) "syslog-facility"
162) "local0"
163) "appendonly"
164) "no"
165) "dir"
166) "/var/lib/redis"
167) "save"
168) "900 1 300 10 60 10000"
169) "client-output-buffer-limit"
170) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
171) "unixsocketperm"
172) "0"
173) "slaveof"
174) ""
175) "notify-keyspace-events"
176) ""
177) "bind"
178) ""

redis. The brief description of the conf configuration item is as follows:

  1. Redis does not run as a daemon by default. You can modify it through this configuration item and use yes to enable the daemon
    daemonize no

  2. When Redis runs as a daemon, Redis will write the pid to / var / run / by default pid file, which can be specified through pidfile
    pidfile /var/run/

  3. Specify the Redis listening port. The default port is 6379. In a blog post, the author explained why 6379 is selected as the default port, because 6379 is the number corresponding to MERZ on the mobile phone button, and MERZ is taken from the name of Italian singer Alessia Merz
    port 6379

  4. Bound host address
    5. Close the connection after the client is idle for a long time. If 0 is specified, it means that the function is closed
    timeout 300

  5. Specify the logging level. Redis supports four levels in total: debug, verbose, notice and warning. The default is verbose
    loglevel verbose

  6. The logging mode is standard output by default. If Redis is configured to run in daemon mode, and the logging mode is configured here as standard output, the log will be sent to / dev/null
    logfile stdout

  7. Set the number of databases. The default database is 0. You can use the SELECT command to specify the database id on the connection
    databases 16

  8. Specify how long and how many update operations will synchronize the data to the data file. Multiple conditions can be matched
    Redis default configuration file provides three conditions:
    save 900 1
    save 300 10
    save 60 10000
    It means 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes) and 10000 changes in 60 seconds respectively.

  9. Specify whether to compress the data when storing to the local database. The default is yes. Redis adopts LZF compression. If you want to save CPU time, you can turn this option off, but the database file will become huge
    rdbcompression yes

  10. Specify the local database file name. The default value is dump rdb
    dbfilename dump.rdb

  11. Specify local database storage directory
    dir ./

  12. 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

  13. When password protection is set for the master service, the slav e service uses the password to connect to the master

  14. Set the Redis connection password. If the connection password is configured, the client needs to provide the password through the AUTH command when connecting to Redis. It is closed by default
    requirepass foobared

  15. Set the maximum number of client connections at the same time. By default, there is no limit. 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, it means there is no limit. 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 128

  16. Specify the maximum memory limit of redis. When redis starts, it will load the data into the memory. After reaching the maximum memory, redis will first try to clear the expired or about to expire keys. After this method is processed, it still reaches the maximum memory setting, so it will no longer be able to write, but it can still be read. Redis's new vm mechanism will store the Key in memory and the Value in the swap area

  17. Specify whether to log after each update operation. Redis writes data to disk asynchronously by default. If it is not turned on, it may cause data loss for a period of time in case of power failure. Because redis synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no
    appendonly no

  18. The default file name is. Append aof
    appendfilename appendonly.aof

  19. Specify the update log condition. There are three optional values:
    no: indicates that the operating system performs data cache synchronization to the disk (fast)
    always: indicates that after each update operation, fsync() is called manually to write data to disk (slow and safe)
    everysec: indicates one synchronization per second (compromise, default)
    appendfsync everysec

  20. Specify whether to enable the virtual memory mechanism. The default value is no. for a brief introduction, the VM mechanism stores the data in pages. Redis will swap the cold data of the pages with less visits to the disk, and the pages with more visits will be automatically swapped out of memory by the disk (I will carefully analyze redis's VM mechanism in the following article)
    vm-enabled no

  21. Virtual memory file path. The default value is / TMP / Redis Swap, which cannot be shared by multiple Redis instances
    vm-swap-file /tmp/redis.swap

  22. 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 memory is set to 0, all values actually exist on disk. The default value is 0
    vm-max-memory 0

  23. Redis swap files are divided into many pages. One object can be saved on multiple pages, but one page cannot be shared by multiple objects. VM page size should be set according to the stored data size. The author suggests that if many small objects are stored, the page size should be set to 32 or 64 bytes; If you store very large objects, you can use a larger page. If you are not sure, you can use the default value
    vm-page-size 32

  24. Set the number of pages in the swap file. Since the page table (a bitmap indicating that the page is idle or used) is placed in memory, every 8 pages on the disk will consume 1 byte of memory.
    vm-pages 134217728

  25. 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, which may cause a long delay. The default value is 4
    vm-max-threads 4

  26. Set whether to combine smaller packages into one package for sending when answering to the client. It is on by default
    glueoutputbuf yes

  27. Specifies that a special hash algorithm is used when the number of elements exceeds a certain number or the maximum element exceeds a critical value
    hash-max-zipmap-entries 64
    hash-max-zipmap-value 512

  28. Specify whether to activate reset hash, which is on by default (described in detail later when Redis hash algorithm is introduced)
    activerehashing yes

  29. Specify to include other configuration files. You can use the same configuration file among multiple Redis instances on the same host, and each instance has its own specific configuration file
    include /path/to/local.conf

5. How to stop / start / restart redis service

5.1 if redis is installed with apt get or yum install, you can directly stop / start / restart redis through the following command

/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart

If redis is installed through the source code, you can restart redis through the shutdown command of redis cli, the client program of redis

If redis is installed with apt get or yum install, you can directly stop / start / restart redis through the following command, and you can directly restart redis through the following command

Under root
/etc/init.d/redis-server restart

Under ordinary users
sudo /etc/init.d/redis-server restart

5.2. If redis is installed through the source code, you can restart redis through the shutdown command of redis cli, the client program of redis

5.2.1. Close redis service and verify

Turn off redis without setting redis password
redis-cli -h -p 6379 shutdown

Turn off redis when redis password is set
Redis cli - H - P 6379 - a configure password shutdown

If the above fails, use kill to force the shutdown
kill -9 pid

5.2.2. Start redis service and verify

If it is in the bin directory under the redis file, the command:
redis-server /etc/redis/redis.conf # start redis service

If it is in the redis file
/bin/redis-server /etc/redis.conf # start redis service

Topics: Linux Redis Cache