Usually when accessing the Redis server, we usually use redis-cli to enter the interactive mode, and then read and write the server by asking and answering. In this case, we use its "interactive" model". There is another "direct mode", which executes the command and obtains the output results by passing the command parameters directly to redis-cli.
<code class="hljs bash">$ redis-cli incrby foo 5<br>(integer) 5<br>$ redis-cli incrby foo 5<br>(integer) 10<br></code>
If the output content is large, you can also redirect the output to an external file
<code class="hljs php">$ redis-cli info > info.txt<br>$ wc -l info.txt<br> 120 info.txt<br></code>
The server pointed to by the above command is the default server address. If you want to point A specific server can execute commands in batches like this
<code class="hljs php">// -n 2 表示使用第2个库,相当于 select 2<br>$ redis-cli -h localhost -p 6379 -n 2 ping<br>PONG<br></code>
<code class="hljs bash">$ cat cmds.txt<br>set foo1 bar1<br>set foo2 bar2<br>set foo3 bar3<br>......<br>$ cat cmds.txt | redis-cli<br>OK<br>OK<br>OK<br>...<br></code>
<code class="hljs php">$ redis-cli < cmds.txt<br/>OK<br/>OK<br/>OK<br/>...<br/></code>
<code class="hljs vbscript">$ cat str.txt<br/>Ernest Hemingway once wrote,<br/>"The world is a fine place and worth fighting for."<br/>I agree with the second part.<br/>$ redis-cli -x set foo < str.txt<br/>OK<br/>$ redis-cli get foo<br/>"Ernest Hemingway once wrote,\n\"The world is a fine place and worth fighting for.\"\nI agree with the second part.\n"<br/></code>
Repeat execution of instructions
<code class="hljs makefile">// 间隔1s,执行5次,观察qps的变化<br/>$ redis-cli -r 5 -i 1 info | grep ops<br/>instantaneous_ops_per_sec:43469<br/>instantaneous_ops_per_sec:47460<br/>instantaneous_ops_per_sec:47699<br/>instantaneous_ops_per_sec:46434<br/>instantaneous_ops_per_sec:47216<br/></code>
<code class="hljs css">127.0.0.1:6379> 5 ping<br>PONG<br>PONG<br>PONG<br>PONG<br>PONG<br># 下面的指令很可怕,你的屏幕要愤怒了<br>127.0.0.1:6379> 10000 info<br>.......<br></code>
<code class="hljs php">$ redis-cli rpush lfoo a b c d e f g<br>(integer) 7<br>$ redis-cli --csv lrange lfoo 0 -1<br>"a","b","c","d","e","f","g"<br>$ redis-cli hmset hfoo a 1 b 2 c 3 d 4<br>OK<br>$ redis-cli --csv hgetall hfoo<br>"a","1","b","2","c","3","d","4"<br></code>
<code class="hljs php">$ redis-cli --csv -r 5 hgetall hfoo<br>"a","1","b","2","c","3","d","4"<br>"a","1","b","2","c","3","d","4"<br>"a","1","b","2","c","3","d","4"<br>"a","1","b","2","c","3","d","4"<br>"a","1","b","2","c","3","d","4"<br></code>
Execute lua script
<code class="hljs ruby">127.0.0.1:6379> eval "return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])" 2 foo1 foo2 bar1 bar2<br>OK<br>127.0.0.1:6379> eval "return redis.pcall('mget', KEYS[1], KEYS[2])" 2 foo1 foo2<br>1) "bar1"<br>2) "bar2"<br></code>
<code class="hljs php">$ cat mset.txt<br>return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])<br>$ cat mget.txt<br>return redis.pcall('mget', KEYS[1], KEYS[2])<br>$ redis-cli --eval mset.txt foo1 foo2 , bar1 bar2<br>OK<br>$ redis-cli --eval mget.txt foo1 foo2<br>1) "bar1"<br>2) "bar2"<br></code>
Monitor server status
<code class="hljs perl">$ redis-cli --stat<br>------- data ------ --------------------- load -------------------- - child -<br>keys mem clients blocked requests connections<br>2 6.66M 100 0 11591628 (+0) 335<br>2 6.66M 100 0 11653169 (+61541) 335<br>2 6.66M 100 0 11706550 (+53381) 335<br>2 6.54M 100 0 11758831 (+52281) 335<br>2 6.66M 100 0 11803132 (+44301) 335<br>2 6.66M 100 0 11854183 (+51051) 335<br></code>
Scan large KEY
<code class="hljs delphi">$ ./redis-cli --bigkeys -i 0.01<br># Scanning the entire keyspace to find biggest keys as well as<br># average sizes per key type. You can use -i 0.1 to sleep 0.1 sec<br># per 100 SCAN commands (not usually needed).<br><br>[00.00%] Biggest zset found so far 'hist:aht:main:async_finish:20180425:17' with 1440 members<br>[00.00%] Biggest zset found so far 'hist:qps:async:authorize:20170311:27' with 2465 members<br>[00.00%] Biggest hash found so far 'job:counters:6ya9ypu6ckcl' with 3 fields<br>[00.01%] Biggest string found so far 'rt:aht:main:device_online:68:{-4}' with 4 bytes<br>[00.01%] Biggest zset found so far 'machine:load:20180709' with 2879 members<br>[00.02%] Biggest string found so far '6y6fze8kj7cy:{-7}' with 90 bytes<br></code>
Sampling Server Instructions
<code class="hljs php">$ redis-cli --host 192.168.x.x --port 6379 monitor<br>1539853410.458483 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"<br>1539853410.459212 [0 10.100.90.61:56659] "PFADD" "growth:dau:20181018" "2klxkimass8w"<br>1539853410.462938 [0 10.100.90.62:20681] "GET" "6yax3eb6etq8:{-7}"<br>1539853410.467231 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2kei0to86ps1"<br>1539853410.470319 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"<br>1539853410.473927 [0 10.100.90.61:58128] "GET" "6yax3eb6etq8:{-7}"<br>1539853410.475712 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2km8sqhlefpc"<br>1539853410.477053 [0 10.100.90.62:61292] "GET" "6yax3eb6etq8:{-7}"<br></code>
通常我们使用Unix的ping命令来测量两台计算机的延迟。Redis 也提供了时延诊断指令,不过它的原理不太一样,它是诊断当前机器和 Redis 服务器之间的指令(PING指令)时延,它不仅仅是物理网络的时延,还和当前的 Redis 主线程是否忙碌有关。如果你发现 Unix 的 ping 指令时延很小,而 Redis 的时延很大,那说明 Redis 服务器在执行指令时有微弱卡顿。
<code class="hljs lua">$ redis-cli --host 192.168.x.x --port 6379 --latency<br>min: 0, max: 5, avg: 0.08 (305 samples)<br></code>
时延单位是 ms。redis-cli 还能显示时延的分布情况,而且是图形化输出。
<code class="hljs php">$ redis-cli --latency-dist<br></code>
这个图形的含义作者没有描述,读者们可以尝试破解一下。
执行下面的命令就可以将远程的 Redis 实例备份到本地机器,远程服务器会执行一次bgsave操作,然后将 rdb 文件传输到客户端。远程 rdb 备份让我们有一种“秀才不出门,全知天下事”的感觉。
<code class="hljs delphi">$ ./redis-cli --host 192.168.x.x --port 6379 --rdb ./user.rdb<br>SYNC sent to master, writing 2501265095 bytes to './user.rdb'<br>Transfer finished with success.<br></code>
如果你想观察主从服务器之间都同步了那些数据,可以使用 redis-cli 模拟从库。
<code class="hljs javascript">$ ./redis-cli --host 192.168.x.x --port 6379 --slave<br>SYNC with master, discarding 51778306 bytes of bulk transfer...<br>SYNC done. Logging commands from master.<br>...<br></code>
从库连上主库的第一件事是全量同步,所以看到上面的指令卡顿这很正常,待首次全量同步完成后,就会输出增量的 aof 日志。
The above is the detailed content of How to use the Redis command line tool. For more information, please follow other related articles on the PHP Chinese website!