zoukankan      html  css  js  c++  java
  • Redis shell

    Redis shell
    命令 参数 功能
    redis-cli -r 将一个命令执行多次
    -i 每隔几秒执行一次
    -x 和|一起接收前面地输出,并执行命令
    -c  
    -a  
    --scan/--pattern 扫描指定模式地键
    --slave 监控节点更新操作
    --rdb 实例生成并发送RDB持久化文件
    --pipe  
    --bigkeys 提取占用内存比较大的键
    --eval 执行Lua脚本
    --latency 检测网络延迟
    --stat 查看一些统计信息
    --raw/--no-row 可以返回原始格式
    redis-benchmark -c  客户端并发数量
    -n <requests> 客户端请求总量
    -q  
    -r 批量生成键
    -p  
    -k  
    -t  
    --csv 格式化输出
    redis-server --test-memory 检查系统是否能提供指定内存

    Redis提供了redis-cli、redis-server、redis-benchmark等shell工具,下面会分别介绍它们的用法。

     在此之前我们先来回顾一下两种连接Redis服务器的方式。

    第一种是交互式方式:通过redis-cli -h (host) -p (port)的方式连接到Redis服务,之后所有的操作都是通过交互的方式实现;

    第二种是命令方式:用redis-cli -h (host) -p (port) {command}就可以直接得到命令的返回结果。

    注意:如果没有指定IP和端口号,那么默认连接127.0.01和6379端口。

    一、redis-cli详解

    之前曾经简单的介绍过了redis-cli,包括-p、-h参数,但是除了这些参数,还有许多有用的参数,在某些情况下非常有用。

    如果要了解redis-cli的全部参数,可以执行redis-cli -help命令来进行查看,下面介绍部分常用的。

    Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
      -h <hostname>      Server hostname (default: 127.0.0.1).
      -p <port>          Server port (default: 6379).
      -s <socket>        Server socket (overrides hostname and port).
      -a <password>      Password to use when connecting to the server.
      -r <repeat>        Execute specified command N times.
      -i <interval>      When -r is used, waits <interval> seconds per command.
                         It is possible to specify sub-second times like -i 0.1.
      -n <db>            Database number.
      -x                 Read last argument from STDIN.
      -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: 
    ).
      -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.
      --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 <keys>  Simulate a cache workload with an 80-20 distribution.
      --slave            Simulate a slave showing commands received from the master.
      --rdb <filename>   Transfer an RDB dump from remote server to local file.
      --pipe             Transfer raw Redis protocol from stdin to server.
      --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                         no reply is received within <n> seconds.
                         Default timeout: 30. Use 0 to wait forever.
      --bigkeys          Sample Redis keys looking for big keys.
      --scan             List all keys using the SCAN command.
      --pattern <pat>    Useful with --scan to specify a SCAN pattern.
      --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                         The test will run for the specified amount of seconds.
      --eval <file>      Send an EVAL command using the Lua script at <file>.
      --help             Output this help and exit.
      --version          Output version and exit.
    redis-cli --help

    1.-r

    -r(repeat)选项代表将命令执行多次。

    [root@Redis ~]# redis-cli -r 3 ping
    PONG
    PONG
    PONG

     

    2.-i

    -i(interval)选项代表每隔几秒执行一次命令,但是-i选项必须和-r选项一起使用。

    [root@Redis ~]# date;redis-cli -r 3 -i 2 ping;date
    2017年 12月 22日 星期五 23:56:39 CST
    PONG
    PONG
    PONG
    2017年 12月 22日 星期五 23:56:45 CST
    #时间间隔是6秒

    注意-i的单位是秒,不支持毫秒为单位,但如果想每隔10毫秒执行一次,可以使用-i 0.01。

    3.-x

    -x选项代表从标准输入(stdin)读取数据作为redis-cli的最后一个参数,一般与管道符在一起使用。

    [root@Redis ~]# echo dbsize|redis-cli -x 
    (integer) 14

    4.-c

    -c(cluster)选项是连接Redis Cluster节点时需要使用的,-c选项可以防止moved和ask异常,后面会详细讲解。

    5.-a

    如果Redis配置了密码,可以使用-a(auth)选项,有了这个选项就不需要手动输入auth命令。

    6.--scan和--pattern

    --scan选项和--pettern选项用于扫描指定模式的键,相当于使用scan命令。

    7.--slave

    --slave选项是把当前客户端模拟成当前Redis节点的从节点,可以用来获取当前Redis节点的更新操作,

    合理的利用这个选项可以记录当前连接Redis节点的更新操作,这些更新操作可能是实际开发业务时需要的数据。

    第一个客户端使用--slave选项,可以看到它会一直处于等待状态:

    [root@Redis ~]# redis-cli --slave
    SYNC with master, discarding 1765 bytes of bulk transfer...
    SYNC done. Logging commands from master.
    "PING"

    另外一些客户端进行一些数据的操作:

    127.0.0.1:6379> set zj sb
    OK
    127.0.0.1:6379> del zj
    (integer) 1

    设置--slave选项的客户端会出现这些操作的指示:

    [root@Redis ~]# redis-cli --slave
    SYNC with master, discarding 1765 bytes of bulk transfer...
    SYNC done. Logging commands from master.
    "PING"
    "PING"
    "PING"
    "PING"
    "PING"
    "SELECT","0"
    "set","zj","sb"
    "PING"
    "PING"
    "del","zj"

    8.--rdb

    --rdb选项会请求Redis实例生成并发送RDB持久化文件,保存在本地,可以使用它做持久化文件的定期备份。

    9.--pipe

    --pipe选项用于将命令封装成Redis通信协议定义的数据格式,批量发送给Redis执行。

    10.--bigkeys

    --bigkeys选项使用scan命令对redis的键进行采样,从中找到内存占用比较大的键值,这些键可能是系统的瓶颈。

    11.--eval

    --eval选项用于执行指定的Lua脚本。

    12.--latency

    latency有三个选项,分别是--latency、--latency-history、--latency-dist。它们都可以检测网络延迟,对Redis开发和运维非常有帮助。

    (1)--latency

    该选项可以测试客户端到目标Redis的网络延迟。

    例如如下拓扑结构:

    机房A和B是跨地区,客户端B可以直接访问Redis服务器。

    客户端B的网络延迟:

    [root@Redis ~]# redis-cli --latency
    min: 0, max: 1, avg: 0.07 (824 samples)

    客户端A的网络延迟:

    [root@chenxing2 redis]# redis-cli -h 192.168.71.135 --latency
    min: 0, max: 1, avg: 0.33 (113 samples)

    可以看到客户端A由于距离Redis比较远,平均的网络延时会高一些。

    (2)--latency--history

    --latency1的执行结果只有一条,如果想以分时段的形式了解延迟信息,可以使用--latency-history选项

    [root@chenxing2 redis]# redis-cli -h 192.168.71.135 --latency-history
    min: 0, max: 2, avg: 0.31 (1357 samples) -- 15.01 seconds range
    min: 0, max: 2, avg: 0.35 (1355 samples) -- 15.00 seconds range
    min: 0, max: 2, avg: 0.38 (1354 samples) -- 15.00 seconds range

    可以看到延时信息每15秒输出一次,可以通过-i参数控制间隔信息。

    [root@chenxing2 redis]# redis-cli -h 192.168.71.135 -r 3 -i 1 --latency-history
    min: 0, max: 1, avg: 0.27 (92 samples) -- 1.00 seconds range
    min: 0, max: 1, avg: 0.23 (91 samples) -- 1.00 seconds range

     

    (3)--latency-dist

    该选项会使用图表的形式从控制台输出延迟统计信息。

    13.--stat

    --stat选项可以实时获取Redis的重要统计信息,虽然info命令中的统计信息更全,但是能实时看到一些增量的数据(例如requests)对于Redis的运维还是有帮助的。

    [root@chenxing2 redis]# redis-cli --stat
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    0          862.15K  1       0       0 (+0)              2           
    0          862.15K  1       0       1 (+0)              2           
    0          862.15K  1       0       2 (+1)              2      
    ......

     14.--raw和--no-raw

    --no-raw选项是要求命令的返回结果必须是原始的格式,--raw恰恰相反,返回格式化后的结果。

    在Redis中设置一个键,如果用get或--no-row选项,那么返回的结果是二进制格式:

    [root@Redis ~]# redis-cli set hello "你好"
    OK
    [root@Redis ~]# redis-cli get hello
    "xe4xbdxa0xe5xa5xbd"
    [root@Redis ~]# redis-cli --no-raw get hello
    "xe4xbdxa0xe5xa5xbd"

    如果使用--raw选项,就会返回中文:

    [root@Redis ~]# redis-cli --raw get hello
    你好

    二、redis-benchmark

    redis-benchmark可以为Redis做基准性能测试,它提供了很多选项帮助开发和运维人员测试Redis的相关属性。

    1.-c

    -c(clients)选项代表客户端的并发数量

    2.-n <requests>

    -n(num)选项代表客户端请求总量(默认是10000)。

    例如redis-benchmark -c 100 -n 20000代表100个客户端同时请求Redis,一共执行20000次。

    redis-benchmark会对各类数据结构的命令进行测试,并给出性能指标:

    ====== GET ======
      20000 requests completed in 0.17 seconds
      100 parallel clients
      3 bytes payload
      keep alive: 1
    
    95.92% <= 1 milliseconds
    99.75% <= 2 milliseconds
    100.00% <= 2 milliseconds
    116959.06 requests per second

    例如上面一共执行了20000次操作,在0.17秒完成,每个请求是3个字节,95.92%的命令执行时间小于1毫秒,Redis每秒可以处理116959.06次get请求。

    -3.-q

    -q选项仅仅显示redis-benchmark的requests per second1信息:

    [root@Redis ~]# redis-benchmark -c 100 -n 20000 -q
    PING_INLINE: 113636.37 requests per second
    PING_BULK: 121951.22 requests per second
    SET: 115606.94 requests per second
    GET: 118343.20 requests per second
    INCR: 116959.06 requests per second
    LPUSH: 113636.37 requests per second
    LPOP: 113636.37 requests per second
    SADD: 114285.72 requests per second
    SPOP: 108695.65 requests per second
    LPUSH (needed to benchmark LRANGE): 104166.66 requests per second
    LRANGE_100 (first 100 elements): 46403.71 requests per second
    LRANGE_300 (first 300 elements): 19940.18 requests per second
    LRANGE_500 (first 450 elements): 11750.88 requests per second
    LRANGE_600 (first 600 elements): 11117.29 requests per second
    MSET (10 keys): 85470.09 requests per second

    4.-r

    一个空的Redis上执行了redis-benchmark会发现只有三个键:

    [root@chenxing2 redis]#  redis-benchmark -c 100 -n 20000 -q
    PING_INLINE: 82644.62 requests per second
    PING_BULK: 96618.36 requests per second
    SET: 98522.17 requests per second
    GET: 93896.71 requests per second
    INCR: 93023.26 requests per second
    LPUSH: 100000.00 requests per second
    LPOP: 102564.11 requests per second
    SADD: 104166.66 requests per second
    SPOP: 99502.48 requests per second
    LPUSH (needed to benchmark LRANGE): 105263.16 requests per second
    LRANGE_100 (first 100 elements): 37950.66 requests per second
    LRANGE_300 (first 300 elements): 17873.10 requests per second
    LRANGE_500 (first 450 elements): 12961.76 requests per second
    LRANGE_600 (first 600 elements): 10256.41 requests per second
    MSET (10 keys): 39840.64 requests per second
    
    [root@chenxing2 redis]# redis-cli
    127.0.0.1:6379> dbsize
    (integer) 3
    127.0.0.1:6379> keys *
    1) "key:__rand_int__"
    2) "counter:__rand_int__"
    3) "mylist"

    如果想向Redis插入更多的键,可以执行使用-r(random)选项,可以向Redis插入更多的随机键。

    [root@chenxing2 redis]#  redis-benchmark -c 100 -n 20000 -r 10000

    -r选项会在key、counter键上加一个12位的后缀,-r 10000代表只对后四位做随机处理(-r不是随机数的个数),

    例如上面操作后,key的数量和结果结构如下:

    127.0.0.1:6379> dbsize
    (integer) 18656
    127.0.0.1:6379> scan 0
    1) "5120"
    2)  1) "counter:000000002683"
        2) "key:000000008908"
        3) "counter:000000005887"
        4) "counter:000000008039"
        5) "key:000000001037"
        6) "key:000000004592"
        7) "counter:000000003728"
        8) "counter:000000009443"
        9) "counter:000000000413"
       10) "key:000000009427"

    5.-p

    -p选项代表每个请求pipline的数据量(默认位1).

    6.-k <boolean>

    -k选项代表客户端是否使用keepalive,1为使用,0为不使用,默认值为1.

    7.-t

    -t选项可以对指定进行基准测试

    [root@chenxing2 redis]#  redis-benchmark -t get,set -q
    SET: 96246.39 requests per second
    GET: 106951.88 requests per second

    8.--csv

    --csv选项会将结果按照CSV格式输出,便于后续处理,如导出到Execl等。

    [root@chenxing2 redis]#  redis-benchmark -t get,set -q --csv
    "SET","101010.10"
    "GET","106837.61"

    三、redis-server

    redis-server除了启动Redis外,还有一个--test-memory选项。

    redis-server --test-memory可以用来检测当前操作系统能否稳定地分配指定容量地内存给Redis,通过这种检测可以有效避免因为内存问题造成Redis崩溃。

  • 相关阅读:
    how to write a paper
    attention mechanism思考
    OSX-KVM 安装备忘指南: 在KVM虚拟机中运行macOSX Big Sur / Catalina
    Unity CacheServer6.x版本 安装配置说明
    Linux常用命令2
    Ubuntu 18.04 + apache2.4 配置https证书(SSL)笔记
    Linux常用命令随笔
    软件测试-8 集成测试
    软件测试-7 在实际测试时的一些想法
    软件测试-6 白盒测试
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/8077157.html
Copyright © 2011-2022 走看看