本文所有内容是从https://redis.io/topics/rediscli来的
1.直接使用redis-cli来使用命令
redis-cli get name #返回name的值
redis-cli get name >/usr/local/redis.txt
2.redis-cli连接host,port,password,database(最有用)
redis-cli -h xxxx -p 6379 -a password -n 0(指定几号数据库,默认0号数据库)
密码可以通过以下命令来设置
config set requirepass "123456" #本次生效,要持久化就用config rewrite
config rewrite #写到文件中,就是redis-server redis.conf启动时指定的redis.conf,不然不能写
3.从外部文件中读取内容
test.txt
12
13
14
redis-cli set name
结果为12\n13\n14\n
test.txt
set name tom
set age 18
set addr hangzhou
cat test.txt |redis-cli #会把test.txt里的命令传给redis-cli
4.重复执行
redis-cli -r 5 get name #重复五次,-1就是一直执行
redis-cli -r 5 -i 1(每隔一秒) get name
5.海量数据插入
cat data.txt | redis-cli --pipe #我自己试的时候,每行开头要空一格,不然报错
6.从redis-cli导出数据用逗号分隔
redis-cli --csv lrange list 0 -1 #可用于一条命令导出,不能导出整个数据库
7.运行lua脚本
test.lua
return redis.call('get',KEYS[1])
redis-cli --eval test.lua age
8.互动模式(大部分情况使用的都是这个)
redis-cli
xxxxx:xxx>
挑几个写
192.168.10.250:6379> 5 get age #在命令前面加数字,重复
"10"
"10"
"10"
"10"
"10"
192.168.10.250:6379> help set #在命令前面加help可以得到命令的信息
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
192.168.10.250:6379> help config
192.168.10.250:6379> help config set
CONFIG SET parameter value
summary: Set a configuration parameter to the given value
since: 2.0.0
group: server
xxx:xxx>clear #可以清空屏幕
9.查看客户端状态
好像挺有用的命令,可以看内存,客户端连接等信息
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --stat -i 5 #默认每隔一秒,这里设置5秒
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
105 810.11K 1 0 366 (+0) 32
105 810.11K 1 0 367 (+1) 32
105 810.11K 1 0 368 (+1) 32
105 810.11K 1 0 369 (+1) 32
10.bigkeys
可以看key的情况,觉得有用
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --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 'test17' with 6 bytes
[00.00%] Biggest zset found so far 'zset' with 2 members
[20.95%] Biggest string found so far 'addr' with 8 bytes
[89.52%] Biggest list found so far 'list' with 4 items
-------- summary -------
Sampled 105 keys in the keyspace!
Total key length in bytes is 609 (avg len 5.80)
Biggest string found 'addr' has 8 bytes
Biggest list found 'list' has 4 items
Biggest zset found 'zset' has 2 members
103 strings with 604 bytes (98.10% of keys, avg size 5.86)
1 lists with 4 items (00.95% of keys, avg size 4.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)
1 zsets with 2 members (00.95% of keys, avg size 2.00)
11.一些可以用互动模式的
redis-cli --scan | head -10
redis-cli --scan --pattern '*-test'
redis-cli psubscribe '*'
redis-cli monitor
12.检测延迟
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --latency #每秒ping命令100次
min: 0, max: 2, avg: 0.13 (3484 samples)^C #毫秒为单位
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --latency-history #默认每隔15秒,可用-i设置
min: 0, max: 1, avg: 0.18 (1366 samples) -- 15.00 seconds range
min: 0, max: 1, avg: 0.06 (1365 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.18 (863 samples)^C
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --latency-dist
---------------------------------------------
. - * # .01 .125 .25 .5 milliseconds
1,2,3,...,9 from 1 to 9 milliseconds
A,B,C,D,E 10,20,30,40,50 milliseconds
F,G,H,I,J .1,.2,.3,.4,.5 seconds
K,L,M,N,O,P,Q,? 1,2,4,8,16,30,60,>60 seconds
From 0 to 100%:
---------------------------------------------
。。。。。。。。#有颜色的,可以看延迟的光谱
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --intrinsic-latency 5
Max latency so far: 1 microseconds.
Max latency so far: 55 microseconds.
Max latency so far: 1272 microseconds.
Max latency so far: 1451 microseconds.
Max latency so far: 2052 microseconds.
Max latency so far: 2475 microseconds.
72240980 total runs (avg latency: 0.0692 microseconds / 69.21 nanoseconds per run).
Worst run took 35759x longer than the average latency.
12.备份
redis-cli --rdb /tmp/dump.rdb
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --slave
SYNC with master, discarding 1650 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING" #复制的时候可以看到底复制了那些东西
13.根据内存和key来分配cache
[root@master redis]# src/redis-cli -h 192.168.10.250 -a redis123456 --lru-test 1000000 #(keys)
152500 Gets/sec | Hits: 35762 (23.45%) | Misses: 116738 (76.55%)
145250 Gets/sec | Hits: 76993 (53.01%) | Misses: 68257 (46.99%)
137000 Gets/sec | Hits: 93136 (67.98%) | Misses: 43864 (32.02%)
137000 Gets/sec | Hits: 104979 (76.63%) | Misses: 32021 (23.37%)
130000 Gets/sec | Hits: 106334 (81.80%) | Misses: 23666 (18.20%)
140250 Gets/sec | Hits: 119526 (85.22%) | Misses: 20724 (14.78%)
140500 Gets/sec | Hits: 123435 (87.85%) | Misses: 17065 (12.15%)
140750 Gets/sec | Hits: 126279 (89.72%) | Misses: 14471 (10.28%)
147750 Gets/sec | Hits: 134649 (91.13%) | Misses: 13101 (8.87%)
153500 Gets/sec | Hits: 141723 (92.33%) | Misses: 11777 (7.67%)
150000 Gets/sec | Hits: 139756 (93.17%) | Misses: 10244 (6.83