In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Redis command line tool what to use", in daily operation, I believe many people have doubts about what to use Redis command line tool, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Redis command line tool what to use" doubts helpful! Next, please follow the small series to learn together!
Execute a single command
Usually when visiting Redis server, we usually use redis-cli to enter interactive mode, and then ask and answer to read and write the server. In this case, we use its "interactive mode". There is another "direct mode" in which commands are executed and output results obtained by passing command parameters directly to redis-cli.
$ redis-cli incrby foo 5
(integer) 5
$ redis-cli incrby foo 5
(integer) 10
If the output is large, you can also redirect the output to an external file
$ redis-cli info > info.txt
$ wc -l info.txt
120 info.txt
The above command points to the default server address, which you can do if you want to point to a specific server
// -n 2 means to use the second library, which is equivalent to select 2
$ redis-cli -h localhost -p 6379 -n 2 ping
PONG
batch execution command
In the usual online development process, sometimes we inevitably have to manually create data, and then import Redis. Usually we write scripts to do this. However, there is another convenient way to use redis-cli directly to batch execute a series of instructions.
$ cat cmds.txt
set foo1 bar1
set foo2 bar2
set foo3 bar3
......
$ cat cmds.txt | redis-cli
OK
OK
OK
...
The instructions above use Unix pipes to connect the standard output of the cat instruction to the standard input of redis-cli. You can also use input redirection directly to batch execute instructions.
$ redis-cli
< cmds.txt OK OK OK ... set 多行字符串 如果一个字符串有多行,你希望将它传入 set 指令,redis-cli 要如何做?可以使用 -x 选项,该选项会使用标准输入的内容作为最后一个参数。 $ cat str.txt Ernest Hemingway once wrote, "The world is a fine place and worth fighting for." I agree with the second part. $ redis-cli -x set foo < str.txt OK $ redis-cli get foo "Ernest Hemingway once wrote,\n\"The world is a fine place and worth fighting for.\"\nI agree with the second part.\n" 重复执行指令 redis-cli 还支持重复执行指令多次,每条指令执行之间设置一个间隔时间,如此便可以观察某条指令的输出内容随时间变化。 // 间隔1s,执行5次,观察qps的变化 $ redis-cli -r 5 -i 1 info | grep ops instantaneous_ops_per_sec:43469 instantaneous_ops_per_sec:47460 instantaneous_ops_per_sec:47699 instantaneous_ops_per_sec:46434 instantaneous_ops_per_sec:47216 如果将次数设置为 -1 那就是重复无数次永远执行下去。如果不提供 -i 参数,那就没有间隔,连续重复执行。在交互模式下也可以重复执行指令,形式上比较怪异,在指令前面增加次数 127.0.0.1:6379>5 ping
PONG
PONG
PONG
PONG
PONG
#The command below is terrible, your screen is going to be angry
127.0.0.1:6379> 10000 info
.......
export CSV
redis-cli cannot export the contents of an entire library to csv at once, but it can export the output of individual instructions to csv format.
$ redis-cli rpush lfoo a b c d e f g
(integer) 7
$ redis-cli --csv lrange lfoo 0 -1
"a","b","c","d","e","f","g"
$ redis-cli hmset hfoo a 1 b 2 c 3 d 4
OK
$ redis-cli --csv hgetall hfoo
"a","1","b","2","c","3","d","4"
Of course, this export function is weak, just a bunch of strings separated by commas. However, you can see the export effect of multiple instructions in combination with batch execution of commands.
$ redis-cli --csv -r 5 hgetall hfoo
"a","1","b","2","c","3","d","4"
"a","1","b","2","c","3","d","4"
"a","1","b","2","c","3","d","4"
"a","1","b","2","c","3","d","4"
"a","1","b","2","c","3","d","4"
The effect of the--csv parameter is to transform the output, separating it with commas, and nothing more.
Execute lua script
In the lua scripts section, we used the eval directive to execute script strings, compressing the script content into a single-line string each time and invoking the eval directive, which was cumbersome and poorly readable. Redis-cli takes this into account and can execute script files directly.
127.0.0.1:6379> eval "return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])" 2 foo1 foo2 bar1 bar2
OK
127.0.0.1:6379> eval "return redis.pcall('mget', KEYS[1], KEYS[2])" 2 foo1 foo2
1) "bar1"
2) "bar2"
Below we execute the above instructions in the form of scripts. The parameter forms are different. The comma separation between KEY and ARGV is required, and the number parameter of KEY is not required.
$ cat mset.txt
return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])
$ cat mget.txt
return redis.pcall('mget', KEYS[1], KEYS[2])
$ redis-cli --eval mset.txt foo1 foo2 , bar1 bar2
OK
$ redis-cli --eval mget.txt foo1 foo2
1) "bar1"
2) "bar2"
If your lua script is too long, --eval can be useful.
Monitoring server status
We can use the--stat parameter to monitor the status of the server in real time, outputting it in real time every 1s.
$ redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
2 6.66M 100 0 11591628 (+0) 335
2 6.66M 100 0 11653169 (+61541) 335
2 6.66M 100 0 11706550 (+53381) 335
2 6.54M 100 0 11758831 (+52281) 335
2 6.66M 100 0 11803132 (+44301) 335
2 6.66M 100 0 11854183 (+51051) 335
If you think the interval is too long or too short, you can adjust the output interval using the-i parameter.
Scan for large KEY
This feature is so useful that I have tried it countless times online. Every time we encounter Redis accidental stuck problem, the first thing we think of is whether there is a big KEY in the instance. The memory expansion and release of the big KEY will cause the main thread to be stuck. If you know there is no big KEY inside, you can write your own program scan, but this is too cumbersome. redis-cli provides the--bigkeys parameter to quickly scan out large KEY in memory, and uses the-i parameter to control the scan interval to avoid the scan command causing the ops of the server to increase sharply.
$ ./ redis-cli --bigkeys -i 0.01
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
[00.00%] Biggest zset found so far 'hist:aht:main:async_finish:20180425:17' with 1440 members
[00.00%] Biggest zset found so far 'hist:qps:async:authorize:20170311:27' with 2465 members
[00.00%] Biggest hash found so far 'job:counters:6ya9ypu6ckcl' with 3 fields
[00.01%] Biggest string found so far 'rt:aht:main:device_online:68:{-4}' with 4 bytes
[00.01%] Biggest zset found so far 'machine:load:20180709' with 2879 members
[00.02%] Biggest string found so far '6y6fze8kj7cy:{-7}' with 90 bytes
redis-cli records the KEY with the largest length for each object type, and for each object type, the highest record is immediately output once refreshed. It can guarantee that the output length is KEY of Top1, but KEY such as Top2 and Top3 cannot be guaranteed to be scanned. The general processing method is to scan several times, or to eliminate the KEY of Top1 and then scan to confirm that there is no second largest KEY.
sample server instruction
Now there is a Redis server online whose OPS is too high. Many business modules are using this Redis. How can I determine which business is causing the abnormally high OPS? At this time, the instructions of the online server can be sampled, and the service points with high OPS ratio can be analyzed by observing the sampled instructions. This is when you use the monitor command, which displays all the commands that the server executes in an instant. But be careful when using it even if you use ctrl+c interrupt, otherwise your monitor will crackle too many commands to dazzle you instantly.
$ redis-cli --host 192.168.x.x --port 6379 monitor
1539853410.458483 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.459212 [0 10.100.90.61:56659] "PFADD" "growth:dau:20181018" "2klxkimass8w"
1539853410.462938 [0 10.100.90.62:20681] "GET" "6yax3eb6etq8:{-7}"
1539853410.467231 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2kei0to86ps1"
1539853410.470319 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.473927 [0 10.100.90.61:58128] "GET" "6yax3eb6etq8:{-7}"
1539853410.475712 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2km8sqhlefpc"
1539853410.477053 [0 10.100.90.62:61292] "GET" "6yax3eb6etq8:{-7}"
Diagnostic server latency
Usually we diagnose the delay of two machines by using Unix ping command. Redis also provides latency diagnostic instructions, but its principle is not the same, it is to diagnose the instruction (PING instruction) latency between the current machine and Redis server, it is not only the latency of the physical network, but also whether the current Redis main thread is busy. If you find that Unix ping latency is small and Redis latency is large, it means that Redis server has a slight delay in executing instructions.
$ redis-cli --host 192.168.x.x --port 6379 --latency
min: 0, max: 5, avg: 0.08 (305 samples)
The delay unit is ms. Redis-cli can also display the distribution of delay, and is a graphical output.
$ redis-cli --latency-dist
The meaning of this figure is not described by the author, but readers can try to decipher it.
Remote rdb backup
To back up the remote Redis instance to the local machine, execute the following command. The remote server will perform a bgsave operation and then transfer the rdb file to the client. Remote rdb backup gives us a feeling of "scholar doesn't go out, knows everything".
$ ./ redis-cli --host 192.168.x.x --port 6379 --rdb ./ user.rdb
SYNC sent to master, writing 2501265095 bytes to './ user.rdb'
Transfer finished with success.
analog slave library
If you want to see what data is synchronized between the master and slave servers, you can use redis-cli to simulate the slave repository.
$ ./ redis-cli --host 192.168.x.x --port 6379 --slave
SYNC with master, discarding 51778306 bytes of bulk transfer...
SYNC done. Logging commands from master.
...
The first thing that the slave library does when it connects to the master library is full synchronization, so it's normal to see the above command stuck. After the first full synchronization is completed, it will output incremental aof logs.
At this point, the study of "What are the ways to use Redis command-line tools" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.