Detailed explanation of redis cli command
1. After redis is installed, there are several executable files starting with redis under src and /usr/local/bin, called redis shell. These executable files can be many things.
1. redis-server starts redis
2. redis-cli redis command line tool
3. redis-benchmark benchmark testing tool
4. redis-check-aof AOF persistent file detection tool and repair tool
5, redis-check-dump RDB persistent file detection tool and repair tool
6, redis-sentinel starts redis- sentinel
2. There are two ways to connect to the redis server.
First way: interactive mode
1 2 3 4 5 6 7 8 9 10 11 |
|
Second way: command mode
1 2 3 4 5 |
|
redis-cli contains many parameters, such as -h, -p, you need to understand For all parameters, use the redis-cli -help command.
The first part of the command method
1. -r means to repeat the command multiple times
1 2 3 4 5 6 7 |
|
The ping command can be used to detect whether the redis instance is alive. If it is alive, Display PONG.
2. -i
Execute the command every few seconds (if you want to use ms, such as 10ms, write 0.01), it must be used together with -r.
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
3, -x
represents reading data from standard input as the last parameter of the command.
1 2 3 |
|
4, -c
Used when connecting to cluster nodes. This option can prevent moved and ask exceptions.
5, -a
If a password is configured, option a can be used.
6, –scan and –pattern
are used to scan the keys of the specified pattern, equivalent to the scan command.
7. –slave
When the current client simulates as the slave node of the current redis node, it can be used to obtain the update operation of the current redis node. Reasonable use can be used to record some update operations of the currently connected redis node. These updates may be data needed for business development.
8. –rdb
will request the redis instance to generate and send RDB persistence files and save them locally. Regular backups can be made.
9. –pipe
Encapsulate the command into the data format defined by the redis communication protocol and send it to redis for execution in batches.
10. –bigkeys
Statistics the distribution of bigkeys, use the scan command to sample the redis keys, and find the keys that occupy a large amount of memory. These keys may be the bottleneck of the system.
11. –eval
is used to execute lua scripts
12. –latency
has three options, –latency, –latency-history, – latency-dist. They detect network latency, which manifests itself in different ways.
13. –stat
can obtain important statistical information of redis in real time. Although the info command is relatively complete, you can see some added data here, such as requests (requests per second)
14, –raw and –no-raw
–no-raw requirements Return to original format. –raw displays formatting effects.
Part 2
redis-cli has many commands. For example,
Commands related to connection operations:
1. Default direct connection Remote connection -h 192.168.1.20 -p 6379
2. Ping: Test whether the connection is alive if normal Will return pong
3, echo: print
4, select: switch to the specified database, the database index number index is specified with a numeric value, and 0 is used as the starting index value
5. quit: close the connection
6. auth: simple password authentication
Server related commands:
1. time: return the current server time
2. client list: Returns all client information and statistical data connected to the server. See
3. http://redisdoc.com/server/client_list.html
4. client kill ip:port: Close the client with the address ip:port
5. save: Save the data to the disk synchronously
6. bgsave: Save the data to the disk asynchronously
7. lastsave: Returns the Unix timestamp of the last time data was successfully saved to disk.
8. shundown: Synchronously saves data to disk and then closes the service.
9. info: Provide server information and statistics
10. config resetstat: Reset certain statistical data in the info command
11. config get: Get configuration file information
12. config set: Dynamically adjust the configuration of the Redis server without restarting. The configuration parameters that can be modified can be listed using the command
13. CONFIG GET *
14. config rewrite: Rewrite the redis.conf file specified in the Redis server
15. monitor: real-time dump of received requests
16. slaveof: change the replication policy settings
Publish and subscribe related commands:
1. psubscribe: Subscribe to one or more channels that match a given pattern, such as psubscribe news.* tweet.*
2. publish: Send information message to Specified channel channel, such as publish msg "good morning"
3, pubsub channels: List the current active channels, such as PUBSUB CHANNELS news.i*
4, pubsub numsub: Return to the given channel The number of subscribers, such as PUBSUB NUMSUB news.it news.internet news.sport
news.music
5, pubsub numpat: Returns the sum of the number of all modes subscribed by the client
6. punsubscribe: Instruct the client to unsubscribe from all given modes.
7. Subscribe: Subscribe to the information of one or more given channels. For example, subscribe msg chat_room
8, unsubscribe: Instructs the client to unsubscribe from the given channel.
Commands for KEY operations:
1、exists(key):确认一个key是否存在
2、del(key):删除一个key
3、type(key):返回值的类型
4、keys(pattern):返回满足给定pattern的所有key
5、randomkey:随机返回key空间的一个
6、keyrename(oldname, newname):重命名key
7、dbsize:返回当前数据库中key的数目
8、expire:设定一个key的活动时间(s)
9、ttl:获得一个key的活动时间
10、move(key, dbindex):移动当前数据库中的key到dbindex数据库
11、flushdb:删除当前选择数据库中的所有key
12、flushall:删除所有数据库中的所有key
对String操作的命令:
1、set(key, value):给数据库中名称为key的string赋予值value
2、get(key):返回数据库中名称为key的string的value
3、getset(key, value):给名称为key的string赋予上一次的value
4、mget(key1, key2,…, key N):返回库中多个string的value
5、setnx(key, value):添加string,名称为key,值为value
6、setex(key, time, value):向库中添加string,设定过期时间time
7、mset(key N, value N):批量设置多个string的值
8、msetnx(key N, value N):如果所有名称为key i的string都不存在
9、incr(key):名称为key的string增1操作
10、incrby(key, integer):名称为key的string增加integer
11、decr(key):名称为key的string减1操作
12、decrby(key, integer):名称为key的string减少integer
13、append(key, value):名称为key的string的值附加value
14、substr(key, start, end):返回名称为key的string的value的子串
对List操作的命令:
1、rpush(key, value):在名称为key的list尾添加一个值为value的元素
2、lpush(key, value):在名称为key的list头添加一个值为value的 元素
3、llen(key):返回名称为key的list的长度
4、lrange(key, start, end):返回名称为key的list中start至end之间的元素
5、ltrim(key, start, end):截取名称为key的list
6、lindex(key, index):返回名称为key的list中index位置的元素
7、lset(key, index, value):给名称为key的list中index位置的元素赋值
8、lrem(key, count, value):删除count个key的list中值为value的元素
9、lpop(key):返回并删除名称为key的list中的首元素
10、rpop(key):返回并删除名称为key的list中的尾元素
11、blpop(key1, key2,… key N, timeout):lpop命令的block版本。
12、brpop(key1, key2,… key N, timeout):rpop的block版本。
13、rpoplpush(srckey, dstkey):返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
对Set操作的命令:
1、sadd(key, member):向名称为key的set中添加元素member
2、srem(key, member) :删除名称为key的set中的元素member
3、spop(key) :随机返回并删除名称为key的set中一个元素
4、smove(srckey, dstkey, member) :移到集合元素
5、scard(key) :返回名称为key的set的基数
6、sismember(key, member) :member是否是名称为key的set的元素
7、sinter(key1, key2,…key N) :求交集
8、sinterstore(dstkey, (keys)) :求交集并将交集保存到dstkey的集合
9、sunion(key1, (keys)) :求并集
10、sunionstore(dstkey, (keys)) :求并集并将并集保存到dstkey的集合
11、sdiff(key1, (keys)) :求差集
12、sdiffstore(dstkey, (keys)) :求差集并将差集保存到dstkey的集合
13、smembers(key) :返回名称为key的set的所有元素
14、srandmember(key) :随机返回名称为key的set的一个元素
对Hash操作的命令
1、hset(key, field, value):向名称为key的hash中添加元素field
2、hget(key, field):返回名称为key的hash中field对应的value
3、hmget(key, (fields)):返回名称为key的hash中field i对应的value
4、hmset(key, (fields)):向名称为key的hash中添加元素field
5、hincrby(key, field, integer):将名称为key的hash中field的value增加integer
6、hexists(key, field):名称为key的hash中是否存在键为field的域
7、hdel(key, field):删除名称为key的hash中键为field的域
8、hlen(key):返回名称为key的hash中元素个数
9、hkeys(key):返回名称为key的hash中所有键
10、hvals(key):返回名称为key的hash中所有键对应的value
11、hgetall(key):返回名称为key的hash中所有的键(field)及其对应的value
实例
query在线分析
1 |
|
监控正在请求执行的命令
在cli下执行monitor,生产环境慎用。
模拟oom
1 |
|
模拟宕机
1 |
|
模拟hang
1 |
|
获取慢查询
1 |
|
结果为查询ID、发生时间、运行时长和原命令 默认10毫秒,默认只保留最后的128条。单线程的模型下,一个请求占掉10毫秒是件大事情,注意设置和显示的单位为微秒,注意这个时间是不包含网络延迟的。
slowlog get 获取慢查询日志
slowlog len 获取慢查询日志条数
slowlog reset 清空慢查询
配置:
1 2 3 |
|
更多redis知识请关注PHP中文网redis教程栏目。
The above is the detailed content of Detailed explanation of redis cli command. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
