Execute a single command
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.
$ redis-cli incrby foo 5<br/>(integer) 5<br/>$ redis-cli incrby foo 5<br/>(integer) 10<br/>
If the output content is large, you can also redirect the output to an external file
$ redis-cli info > info.txt<br/>$ wc -l info.txt<br/> 120 info.txt<br/>
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
// -n 2 表示使用第2个库,相当于 select 2<br/>$ redis-cli -h localhost -p 6379 -n 2 ping<br/>PONG<br/>
In the daily online development process, sometimes it is inevitable to manually create data and import it into Redis. Normally we would write a script to do this. But there is another more convenient way, which is to directly use redis-cli to execute a series of instructions in batches.
$ 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/>The above command uses a Unix pipe to connect the standard output of the cat command to the standard input of redis-cli. In fact, you can also directly use input redirection to execute instructions in batches.
$ redis-cli < cmds.txt<br/>OK<br/>OK<br/>OK<br/>...<br/>
set multi-line string
If a string has multiple lines and you want to pass it into the set command, how does redis-cli do it? You can use the -x option, which uses the contents of standard input as the last argument.
$ 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/>
Repeat execution of instructions
redis-cli also supports repeated execution of instructions multiple times. Set an interval between the execution of each instruction, so that you can observe the output content of a certain instruction over time. Variety.
// 间隔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/>If the number of times is set to -1, it will be repeated countless times and executed forever. If the -i parameter is not provided, there will be no interval and the execution will be repeated continuously. In interactive mode, you can also execute instructions repeatedly. The form is rather weird. Add the number of times in front of the instruction.
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/>
Export csv
Although the entire Redis database cannot be exported to CSV format alone, a single item can be exported. The results of the command are in CSV format.
$ 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/>Of course, this export function is relatively weak, it is just a bunch of strings separated by commas. However, you can combine the batch execution of commands to see the export effect of multiple instructions.
$ 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/>
After seeing this, readers should understand that the effect of the --csv parameter is to convert the output once and separate it with commas, nothing more.
Execute lua script
In the lua script section, we use the eval instruction to execute the script string. Each time, we compress the script content into a single-line string and then call the eval instruction, which is very cumbersome. , and the readability is very poor. redis-cli takes this into account and can execute script files directly.
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/>Below we execute the above instructions in the form of a script. The parameter format is different. KEY and ARGV need to be separated by commas, and there is no need to provide the number of KEY parameters
$ 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/>
If your lua script is too long, --eval will be useful.
Monitor server status
We can use the --stat parameter to monitor the status of the server in real time, and output it in real time every 1s.
$ 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/>If you think the interval is too long or too short, you can use the -i parameter to adjust the output interval.
Scan large KEY
This function is so practical, I have tried it online countless times. Every time you encounter the problem of occasional lag in Redis, the first thing that comes to mind is whether there is a large KEY in the instance. The memory expansion and release of the large KEY will cause the main thread to lag. If you know whether there is a big KEY inside, you can write a program to scan it yourself, but this is too cumbersome. redis-cli provides the --bigkeys parameter to quickly scan out the large KEYs in the memory. Use the -i parameter to control the scan interval to avoid the scan command causing a sudden increase in the server's ops alarm.
$ ./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/>redis-cli will record the KEY with the largest length for each object type. For each object type, refreshing the highest record will immediately output it. It can guarantee the output of KEYs with a length of Top1, but there is no guarantee that KEYs such as Top2 and Top3 can be scanned out. A common approach is to perform multiple scans, or to remove the highest priority keyword and then scan again to determine if there are still lower priority keywords.
Sampling Server Instructions
There is a Redis server online now whose OPS is too high. Many business modules are using this Redis. How can we determine which business is causing the abnormally high OPS? . At this time, you can sample the instructions of the online server. By observing the sampled instructions, you can roughly analyze the business points with a high proportion of OPS. Use the monitor command to display all instructions executed by the server at once. It should be noted that when using it, even if you want to force a break, you must press Ctrl C, otherwise too many instructions will appear on the display, which will be dazzling and crackling in an instant.
$ 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/>
诊断服务器时延
通常我们使用Unix的ping命令来测量两台计算机的延迟。Redis 也提供了时延诊断指令,不过它的原理不太一样,它是诊断当前机器和 Redis 服务器之间的指令(PING指令)时延,它不仅仅是物理网络的时延,还和当前的 Redis 主线程是否忙碌有关。如果你发现 Unix 的 ping 指令时延很小,而 Redis 的时延很大,那说明 Redis 服务器在执行指令时有微弱卡顿。
$ redis-cli --host 192.168.x.x --port 6379 --latency<br/>min: 0, max: 5, avg: 0.08 (305 samples)<br/>
时延单位是 ms。redis-cli 还能显示时延的分布情况,而且是图形化输出。
$ redis-cli --latency-dist<br/>
这个图形的含义作者没有描述,读者们可以尝试破解一下。
远程 rdb 备份
执行下面的命令就可以将远程的 Redis 实例备份到本地机器,远程服务器会执行一次bgsave操作,然后将 rdb 文件传输到客户端。远程 rdb 备份让我们有一种“秀才不出门,全知天下事”的感觉。
$ ./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/>
模拟从库
如果你想观察主从服务器之间都同步了那些数据,可以使用 redis-cli 模拟从库。
$ ./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/>
从库连上主库的第一件事是全量同步,所以看到上面的指令卡顿这很正常,待首次全量同步完成后,就会输出增量的 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!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
