Redis uses very straightforward command line interface. Dù relatively simple, nó provide some interesting features might not expect. Let's go over basics và work around most of client's functionality và features.
To start, simple connection:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword
127.0.0.1:6379> PING
PONG
Alright! Connected to our very own Redis server và authenticated using super secret password.
Alternatively, omit -a option và authenticate after connect:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> AUTH mysupersecretpassword
OK
127.0.0.1:6379> PING
PONG
Nếu Redis server và client running on same machine, might choose connect via Unix socket.
Note: Nếu still provide hostname và port as well as socket, redis-cli still connect via Unix socket.
cweid@strange:~$ redis-cli -s /tmp/redis.sock
127.0.0.1:6379> AUTH mysupersecretpassword
OK
127.0.0.1:6379> PING
PONG
Okay, now understand how to connect và authenticate Redis instance via command line, let's see examples useful things we can do.
Giả sử want execute command via command line và only its output returned to standard out:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword PING
PONG
Hoặc perhaps execute same command n number of times:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -r 4 PING
PONG
PONG
PONG
PONG
Notice added -r to command supply "repeat" option. Alternatively, add delay using -i conjunction with -r.
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 1 -r 4 PING
PONG
PONG
PONG
PONG
Adds one-second sleep between each PING command. Also supply subseconds using float:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 0.1 -r 4 PING
PONG
PONG
PONG
PONG
Run PING command every 10th of a second.
Generate simple diagnostic info about Redis instance connected to, run redis-cli với --stat option.
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --stat
------- data ------ --------------------- load -------------------- - child - keys mem clients blocked requests connections
0 790.80K 1 0 122 (+0) 16
0 790.80K 1 0 123 (+1) 16
0 790.80K 1 0 124 (+1) 16
0 790.80K 1 0 125 (+1) 16
0 790.80K 1 0 126 (+1) 16
Here we can see:
- How many keys are set on the server.
- The server's total memory usage.
- The total number of clients connected or blocked.
- The total number of requests the server has served.
- The total current number of connections.
Command useful get overview Redis server as whole. Think of it like stating a file.
Now know how to generate simple stats about Redis server, let's check latency of Redis commands coming in. Super simple via command line:
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --latency
min: 0, max: 1, avg: 0.13 (763 samples)
Here we see minimum, maximum, average request time, as well as number of samples taken.
Note: These are recorded in microseconds. For more info about Redis latency, take a look at documentation for latency monitoring.
Analyze keyspace search large strings hoặc other data structures, run --bigkeys option. Good để find large keys in keyspace, cũng như get count of overall distribution of key types.
cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --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 'user:paul' with 4 bytes
[00.00%] Biggest string found so far 'barrrr' with 19612 bytes
-------- summary -------
Sampled 4 keys in the keyspace!
Total key length in bytes is 29 (avg len 7.25)
Biggest string found 'barrrr' has 19612 bytes
4 strings with 19624 bytes (100.00% of keys, avg size 4906.00)
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)
Gives us a lot of useful information back about different keys, including their types and sizes.
Overall, Redis CLI là powerful tool giúp manage Redis instance. Ability use its built-in options really help analyzing problematic Redis server.