【redis】redis shell 详解

1.redis shell 

redis提供了redis-cli,redis-server,redis-benchmark 等shell工具。

redis-cli 的命令 

[root@oracle1 redis6379]# redis-cli --help
redis-cli 4.0.11

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h       Server hostname (default: 127.0.0.1).
  -p           Server port (default: 6379).
  -s         Server socket (overrides hostname and port).
  -a       Password to use when connecting to the server.
  -u            Server URI.
  -r         Execute specified command N times.
  -i       When -r is used, waits  seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n             Database number.
  -x                 Read last argument from STDIN.
  -d      Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
                     If you use this mode in an interactive session it runs
                     forever displaying real-time stats. Otherwise if --raw or
                     --csv is specified, or if you redirect the output to a non
                     TTY, it samples the latency for 1 second (you can use
                     -i to change the interval), then produces a single output
                     and exits.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test   Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb    Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout  In --pipe mode, abort with error if after sending all data.
                     no reply is received within  seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --hotkeys          Sample Redis keys looking for hot keys.
                     only works when maxmemory-policy is *lfu.
  --scan             List all keys using the SCAN command.
  --pattern     Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency  Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval       Send an EVAL command using the Lua script at .
  --ldb              Used with --eval enable the Redis Lua debugger.
  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in
                     this mode the server is blocked and script changes are
                     are not rolled back from the server memory.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.

(1)-r 
-r(repeat)选项代表将命令执行多次。
[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 -r 3 ping
PONG
PONG
PONG

(2)-i 
-i(interval)选项代表间隔几秒执行一次。-i 必须和 -r 一起使用。
-i:的单位是秒,不支持毫秒。
--间隔两秒执行一次,执行三次。
[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 -i 2 -r 3 ping
PONG
PONG
PONG

--可以支持小数秒。
[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 -i 0.1 -r 3 ping
PONG
PONG
PONG

--每隔1秒输出内存使用量。
[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 -i 1 -r 3 info |grep used_memory_human
used_memory_human:850.91K
used_memory_human:851.07K
used_memory_human:851.21K

(3)-x 
-x选项代表从标准输入(stdin)读取数据作为redis-cli的最后一个参数。
[root@oracle1 redis6379]# echo "world" |redis-cli -h 192.168.1.7 -p 6379 -x set hello
OK
[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 get hello
"world\n"

(4)-c 
-c(cluster)选项是连接redis cluster节点时需要使用的,-c选项可以防止moved
和ask异常。
(5)-a 
如果redis配置了密码,可以使用-a(auth)选项。有这个选项,不需要手动输入auth命令。

vi redis.conf 
requirepass myredis  
--设置一个密码。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6379 -a myredis
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.1.7:6379> 

(6)--scan,--pattern 
--scan和pattern选项用于扫描指定模式的键,相当于使用scan; 

(7)--slave 
--slave选项是吧当前客户端模拟成当前redis节点的从节点,可以用来获取当前
redis节点的更新操作。合理利用这个选项可以记录当前连接redis节点的一些更新操作。

--主库执行命令。
192.168.1.7:6379> keys * 
(empty list or set)
192.168.1.7:6379> set a b 
OK
192.168.1.7:6379> mset a a b b c c d d e e f f g g 
OK


--以从库模式登陆。可以查看主库执行的命令。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6379 -a myredis --slave
Warning: Using a password with '-a' option on the command line interface may not be safe.
SYNC with master, discarding 176 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"SELECT","0"
"set","a","b"
"PING"
"mset","a","a","b","b","c","c","d","d","e","e","f","f","g","g"

PING:命令是由于主从复制产生的。
(8)--rdb 
该选项会请求redis实例生成并发送RDB持久化文件,保存在本地。
可以用它做持久化文件的定期备份。

[root@oracle1 redis6379]# redis-cli -h 192.168.1.7 -p 6379 -a myredis --rdb ./redis6379.rdb
Warning: Using a password with '-a' option on the command line interface may not be safe.
SYNC sent to master, writing 217 bytes to './redis6379.rdb'
Transfer finished with success.
[root@oracle1 redis6379]# ll
total 20
-rw-r--r-- 1 root root  217 Jun 19 12:07 dump.rdb
-rw-r--r-- 1 root root  219 Jun 19 12:02 redis6379.conf
-rw-r--r-- 1 root root  217 Jun 19 12:07 redis6379.rdb
-rw-r--r-- 1 root root 4432 Jun 19 12:07 redis.log
--由此可见,这个选项就是个备份命令。

(9)--pipe 
该选项用于将命令封装成redis通信协议定义的数据格式,批量发送给redis执行。
--不好操作。
(10)--bigkeys 
该选项使用scan命令对redis的键进行采样,从中找到占用内存比较大的键值。
这些键这些键可能是系统的瓶颈。
[root@oracle1 redis6379]# redis-cli  -h 192.168.1.7 -p 6379 -a myredis --bigkeys
Warning: Using a password with '-a' option on the command line interface may not be safe.

# 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 'g' with 1 bytes
[00.00%] Biggest string found so far 'hello' with 15 bytes

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

Sampled 8 keys in the keyspace!
Total key length in bytes is 12 (avg len 1.50)

Biggest string found 'hello' has 15 bytes

8 strings with 22 bytes (100.00% of keys, avg size 2.75)
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)

(11)--eval 
该选项用于指定Lua脚本。
(12)--latency 
latency有三个选项:--latency,--latency-history,--latency-dist 
该选项可以测试客户端到目标redis的网络延迟。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6379 -a myredis --latency
Warning: Using a password with '-a' option on the command line interface may not be safe.
min: 0, max: 1, avg: 0.00 (708 samples)

[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6380 -a myredis --latency
Warning: Using a password with '-a' option on the command line interface may not be safe.
min: 0, max: 0, avg: 0.00 (629 samples)

--latency-histoy;
--latency的执行结果只有一条,如果想以分段的形式了解延迟信息。
--默认每16秒执行一次延迟。
redis-cli -h 192.168.1.7 -p 6380 -a myredis --latency-history
Warning: Using a password with '-a' option on the command line interface may not be safe.
min: 0, max: 1, avg: 0.00 (1365 samples) -- 15.00 seconds range
min: 0, max: 1, avg: 0.11 (1365 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.08 (1365 samples) -- 15.01 seconds range
min: 0, max: 0, avg: 0.00 (56 samples)

--latency-dist 
会使用统计图的形式从控制台输出延迟统计信息。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6380 -a myredis --latency-dist
Warning: Using a password with '-a' option on the command line interface may not be safe.
---------------------------------------------
. - * #          .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%:                    

---------------------------------------------
. - * #          .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%:                    
---------------------------------------------
(13)--stat 
该选项实时获取redis的重要统计信息。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6380 -a myredis --stat
Warning: Using a password with '-a' option on the command line interface may not be safe.
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections          
7          849.44K  2       0       14721 (+0)          14          
7          849.44K  2       0       14722 (+1)          14          
7          849.44K  2       0       14723 (+1)          14          
7          849.44K  2       0       14724 (+1)          14          
7          849.44K  2       0       14725 (+1)          14          
7          849.44K  2       0       14726 (+1)          14          
7          849.44K  2       0       14727 (+1)          14          
7          849.44K  2       0       14728 (+1)          14          
--有两个客户端,7个键。14个连接。
[root@oracle1 data]# netstat |grep 6380
tcp        0      0 oracle1:39850           oracle1:6380            TIME_WAIT  
tcp        0      0 oracle1:39846           oracle1:6380            ESTABLISHED
tcp        0      0 oracle1:6380            oracle1:39846           ESTABLISHED

(14)--raw,--no-raw 
--no-raw要求返回结果是原始格式。--raw返回格式化后的结果。
192.168.1.7:6380> set hello "我是很咯哦"
OK
192.168.1.7:6380> 

--格式化输出
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6380 -a myredis --raw get hello
Warning: Using a password with '-a' option on the command line interface may not be safe.
我是很咯哦
--未格式化。
[root@oracle1 data]# redis-cli -h 192.168.1.7 -p 6380 -a myredis --no-raw get hello
Warning: Using a password with '-a' option on the command line interface may not be safe.
"\xe6\x88\x91\xe6\x98\xaf\xe5\xbe\x88\xe5\x92\xaf\xe5\x93\xa6"

你可能感兴趣的:(数据库运维-Redis,redis,bootstrap,数据库)