开源是一种精神,分享是一种美德!

redis-cli

相关命令:redis-server
redis-cli是Redis命令行界面,一个简单的程序,允许向Redis发送命令,并直接从终端读取服务器发送的回复。

它有两种主要的模式:一种是交互模式,在这种模式下,用户输入命令并获得回复的REPL(Read Eval Print Loop)另一种模式是将命令作为redis-cli的参数发送,执行并打印在标准输出上。

在交互模式下,redis-cli具有基本的行编辑功能,可以提供良好的打字体验。

主要参数:

-h host地址
-p 端口
-n 修改数据库
-r(repeat)选项代表江命令执行多次
-i(interval)选项代表每个几秒执行一次命令(必须和-r选项一致使用;单位是秒,不支持毫秒微单位)
-x 选项代表从标准输入(stdin)读取数据作为redis-cli的最后一个参数
-c(cluster)选项是连接Redis Cluster节点时需要使用的,-c选项可以防止moved和ask异常
-a 如果Redis配置了密码,可以用-a(auth)选项,有了这个选项就不需要手动输入auth命令
--scan和--pattern 选项用于扫描指定模式的键,相当于使用scan命令
--slave 选项是把当前客户端模拟成当前Redis节点的从节点,可以用来获取当前Redis节点的更新操作
--rdb 选项会请求Redis实例生成并发送RDB持久化文件,保存在本地,可使用它做持久化文件的定期备份
--pipe 选项用于将命令封装成Redis通信协议定义的数据格式,批量发送给Redis执行
--bigkeys 选项使用scan命令对Redis的键进行采样,从中找到内存占用比较大的键值,这些键可能是系统的瓶颈
--eval 选项用于执行指定Lua脚本
--latency 选项可以测试客户端到目标Redis的网络延迟(执行结果只有一条)
--latency-history 选项以分时段的形式了解延迟信息(延时信息每15秒输出一次),可以通过-i参数控制间隔时间
--latency-dist 选项会使用统计图表的形式从控制台输出延迟统计信息
--stat 选项可以实时获取Redis的重要统计信息(info命令中的统计信息更全),能实时看到一些增量的数据(例如requests)
--raw和--no-raw
--no-raw选项是要求命令的返回结果必须是原始的格式,--raw恰恰相反,返回格式化后的结果

# redis-cli -p 6381 incr no
(integer) 1
# redis-cli -p 6381 --raw incr no    #不输出类型
2
# redis-cli -p 6381 incr no > res.txt     #输出到文件
# cat res.txt 
3
# redis-cli -p 6381 --no-raw incr no > res.txt   #输出到文件,包括类型
# cat res.txt 
(integer) 4


$ redis-cli -h 192.168.0.21 -p 6390 ping  #可以使用-h 和 -p选项更改host和port
PONG

$ redis-cli -a xxxxxxxx ping    #-a后面,根密码
PONG


$ redis-cli flushall
OK
$ redis-cli -n 1 incr a
(integer) 1
$ redis-cli -n 1 incr a
(integer) 2
$ redis-cli -n 2 incr a    #-n改变数据库
(integer) 1


$ redis-cli -p 6381 -x set foo < /etc/services     #从文件取得数据
Ok
$ redis-cli -p 6381 getrange foo 0 50
"# /etc/services:\n# $Id: services,v 1.48 2009/11/11 "


# redis-cli -p 6379 -r 5 -i 1 ping   #每1秒执行一次命令
PONG
PONG
PONG
PONG
PONG


$ redis-cli lpush mylist a b c d
(integer) 4
$ redis-cli --csv lrange mylist 0 -1      #CSV格式输出
"d","c","b","a"


$ redis-cli --stat    #实时监控Redis实例
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
506        1015.00K 1       0       24 (+0)             7
506        1015.00K 1       0       25 (+1)             7
506        3.40M    51      0       60461 (+60436)      57
506        3.40M    51      0       146425 (+85964)     107
507        3.40M    51      0       233844 (+87419)     157
507        3.40M    51      0       321715 (+87871)     207
508        3.40M    51      0       408642 (+86927)     257
508        3.40M    51      0       497038 (+88396)     257


$ redis-cli --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 '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)


$ redis-cli --scan | head -10    #扫描key空间,取10个
key-419
key-71
key-236
key-50
key-38
key-458
key-453
key-499
key-446
key-371


$ redis-cli --scan --pattern '*-11*'    #匹配模式
key-114
key-117
key-118
key-113
key-115
key-112


$ redis-cli monitor    #监控收到的指令
OK
1460100081.165665 [0 127.0.0.1:51706] "set" "foo" "bar"
1460100083.053365 [0 127.0.0.1:51707] "get" "foo"

$ redis-cli --latency    #延迟检查工具是–latency选项
min: 0, max: 1, avg: 0.19 (427 samples)


$ redis-cli --latency-history   #延迟检查历史	
min: 0, max: 1, avg: 0.14 (1314 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.18 (1299 samples) -- 15.00 seconds range

$ redis-cli --rdb /tmp/dump.rdb     #主设备和从设备以RDB文件的形式交换整个数据集
SYNC sent to master, writing 13256 bytes to '/tmp/dump.rdb'
Transfer finished with success.


#这边跑着,同时主库执行set foo bar
$ redis-cli --slave
SYNC with master, discarding 13256 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"SELECT","0"
"set","foo","bar"
"PING"
"incr","mycounter"

评论(0)

暂无评论

评论

名字:
验证码:
获取验证码
内容: