redis-cli命令使用指南

2023-02-20 17:24:04 浏览数 (1)

redis-cli是一个终端程序,用于向redis服务器发送命令和读取响应。它包括两种模式:交互模式和命令模式。

发送命令:

代码语言:javascript复制
# 执行incr操作
redis-cli INCR myCounter

# 重定向输出
redis-cli INCR myCounter > /tmp/output.txt

# 输出原始的结果(去掉自带的类型提示)
redis-cli --raw INCR myCounter

字符串引用和转义: n t '

代码语言:javascript复制
SET MYKEY "HELLOnworld";
GET MYKEY

Host, port, password, and database:

代码语言:javascript复制
redis-cli -h redis15.localnet.org -p 6390 PING
redis-cli -a myUnguessablePazzzzzword123 PING
# 指定数据库
redis-cli -n 1 INCR mycounter

输入流:

代码语言:javascript复制
redis-cli -x SET mykey < /etc/services

重复运行:

代码语言:javascript复制
redis-cli -r 5 INCR mycouner

csv output:

代码语言:javascript复制
redis-cli --csv lrange mylist 0 -1

运行lua脚本:

代码语言:javascript复制
cat /tmp/script.lua
return redis.call('SET',KEYS[1],ARGV[1])

# 逗号前后需要有空格
redis-cli --eval /tmp/script.lua location:hastings:temp , 23

切换主机:

代码语言:javascript复制
127.0.0.1:6379> connect  metal 6379
metal:6379> PING
PONG

扫描大Key:

代码语言:javascript复制
redis-cli --bigkeys

[00.00%] Biggest string found so far 'key-419' with 3 bytes
[05.14%] Biggest list   found so far 'mylist' with 100004 items
[35.77%] Biggest string found so far 'counter:__rand_int__' with 6 bytes
[73.91%] Biggest hash   found so far 'myobject' with 3 fields

-------- summary -------

Sampled 506 keys in the keyspace!
Total key length in bytes is 3452 (avg len 6.82)

Biggest string found 'counter:__rand_int__' has 6 bytes
Biggest   list found 'mylist' has 100004 items
Biggest   hash found 'myobject' has 3 fields

504 strings with 1403 bytes (99.60% of keys, avg size 2.78)
1 lists with 100004 items (00.20% of keys, avg size 100004.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
1 hashs with 3 fields (00.20% of keys, avg size 3.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)

监控模式:

代码语言:javascript复制
redis-cli monitor
OK
1460100081.165665 [0 127.0.0.1:51706] "set" "shipment:8000736522714:status" "sorting"
1460100083.053365 [0 127.0.0.1:51707] "get" "shipment:8000736522714:status"

查看redis时延:

代码语言:javascript复制
redis-cli --latency

远程备份:

代码语言:javascript复制
redis-cli --rdb /tmp/dump.rdb
SYNC sent to master, writing 13256 bytes to '/tmp/dump.rdb'
Transfer finished with success.

副本检查:

该功能是用于开发人员和调试人员检查主节点给副节点发送数据是否有问题。

代码语言:javascript复制
$ redis-cli --replica
SYNC with master, discarding 13256 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"SELECT","0"
"SET","last_name","Enigk"
"PING"
"INCR","mycounter"

LRU模拟: 测试使用管道,会对服务器造成压力,不要在生产环境使用。

代码语言:javascript复制
redis-cli --lru-test 1000

0 人点赞