| 命令 | 參數 | 功能 |
| 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: \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. --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.
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 "\xe4\xbd\xa0\xe5\xa5\xbd" [root@Redis ~]# redis-cli --no-raw get hello "\xe4\xbd\xa0\xe5\xa5\xbd"
如果使用--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崩潰。
