redis-cli 参数,都是在解决 Redis 一些坑问题的过程中,所了解到的 tricks。
--scan
和 --pattern
用 scan 命令扫描 Redis 中的 key,--pattern 选项指定扫描的 key 的 pattern。
对比 keys pattern 命令,虽然 scan 无法一次性返回所有匹配结果,但是可以规避长时间阻塞系统的风险。
$ ./redis-cli -h myhost -p 12345 --scan --pattern 's_*' s_WHY s_ZX s_WL ...
--bigkeys
使用 scanning 方式,对 Redis 整个 keyspace 进行统计(数据量大时采样),寻找每种数据类型 key 的最大 size(key) 和平均 size。
$ ./redis-cli -h myhost -p 12345 --bigkeys # 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 string found so far 's_WHY' with 812 bytes [00.00%] Biggest string found so far 's_ZX' with 7212 bytes [00.00%] Biggest string found so far 'th_data' with 26760 bytes [15.50%] Biggest string found so far 'travel_data' with 31340 bytes [27.66%] Biggest string found so far 'audio_data' with 34198 bytes [92.71%] Biggest string found so far 'video_data' with 68831 bytes -------- summary ------- Sampled 329 keys in the keyspace! Total key length in bytes is 9419 (avg len 28.63) Biggest string found 'video_data' has 68831 bytes 329 strings with 620245 bytes (100.00% of keys, avg size 1885.24) 0 lists with 0 items (00.00% of keys, avg size 0.00) 0 sets with 0 members (00.00% of keys, avg size 0.00) 0 hashs with 0 fields (00.00% of keys, avg size 0.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00) 0 streams with 0 entries (00.00% of keys, avg size 0.00)
--stat
滚动显示服务器信息(keys、mem、clients、blocked、requests、connections)
$ ./redis-cli -h myhost -p 12345 --stat ------- data ------ --------------------- load -------------------- - child - keys mem clients blocked requests connections 330 1.54M 2 0 268544 (+0) 20 330 1.54M 2 0 268545 (+1) 20 330 1.54M 2 0 268546 (+1) 20 330 1.54M 2 0 268547 (+1) 20 330 1.54M 2 0 268551 (+4) 20 ...
-r
和 -i
-r 执行某个命令 N
次
-i 使用 -r 时,设置命令间隔时间(单位秒,可以使用小数)
#默认查看本机 6379 redis-cli -r 60 -i 1 info | grep connected_clients #查看指定服务器连接情况 redis-cli -h 1.1.1.1 -p 6377 -r 60 -i 1 info|grep connected_clients connected_clients:20 connected_clients:22 connected_clients:21 connected_clients:29 ...
--rdb
滚动显示服务器信息(keys、mem、clients、blocked、requests、connections)
redis-cli --rdb backup.rdb
--pipe
发送原始的 Redis 协议格式数据到服务器端执行。做测试时可以用这个命令批量造数据。
cat data.txt | redis-cli --pipe
--eval
执行 lua 脚本。
redis-cli --eval test.lua key1
--latency
和 --latency-history
--latency 持续采样服务器延迟时间,--latency-history
则是持续采集并每隔一段时间(15秒)输出一个记录。两者区别在于前者显示的是持续的采样结果,而后者则只输出一段时间内的采样结果。
用这两个参数可以判断客户端与服务端之间的网络通信造成的延迟对整体延迟的影响。
$ ./redis-cli -h 1.1.1.1 -p 6376 --latency min: 0, max: 13, avg: 0.26 (1667 samples) $ ./redis-cli -h 1.1.1.1 -p 6376 --latency-history min: 0, max: 10, avg: 0.25 (1454 samples) -- 15.01 seconds range min: 0, max: 9, avg: 0.24 (1453 samples) -- 15.01 seconds range min: 0, max: 5, avg: 0.20 (1457 samples) -- 15.01 seconds range min: 0, max: 14, avg: 0.40 (1431 samples) -- 15.00 seconds range min: 0, max: 11, avg: 0.23 (1453 samples) -- 15.01 seconds range
slowlog
命令默认的 Redis 配置中有这么两条:
$ cat redis.conf |grep slow slowlog-log-slower-than 10000 slowlog-max-len 128
这两条配置会产生这样的效果: 如果一条命令的响应时间超过了 10000us (即 10ms) ,那么将会作为 slow command 被记录,并且将只保留最新的 128
条 记录。
需要说明的是,这里所说的 响应时间 不包括客户端与服务端之间的通信开销,仅仅指命令在 Redis server 中的执行时间消耗。
使用 slowlog 这个子命令可以获取当前的这些记录:
127.0.0.1:6379> slowlog get 3 1) 1) (integer) 26 2) (integer) 1577416869 3) (integer) 43097 4) 1) "flushdb" 2) 1) (integer) 25 2) (integer) 1577416917 3) (integer) 33115 4) 1) "flushdb" 3) 1) (integer) 24 2) (integer) 1577416980 3) (integer) 3328650 4) 1) "flushdb"
在不指定记录数量的情况下(slowlog get),默认返回 10 条记录,每条记录包含四个字段。
$ date -d @1577416869 Fri Dec 27 11:21:09 CST 2019
因此上面这个例子的含义就是: 在 2019 年 12 月 27 日 11 时 21 分 09 秒执行的 flushdb
操作耗时 43ms ,因为超过了设定的值被记录到慢日志中。