Home >Database >Redis >Detailed explanation of redis cli command

Detailed explanation of redis cli command

尚
forward
2020-03-09 10:16:1813456browse

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

redis-cli -h {host} -p {port}方式连接,然后所有的操作都是在交互的方式实现,不需要再执行redis-cli了。

$redis-cli -h 127.0.0.1-p 6379

127.0.0.1:6379>set hello world

OK

127.0.0.1:6379>get hello

"world"

Second way: command mode

redis-cli -h {host} -p {port} {command}直接得到命令的返回结果。

$redis-cli -h 127.0.0.1-p 6379 get hello

"world"

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

$redis-cli -r 3 ping

PONG

PONG

PONG

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.

$redis-cli -r 3 -i 1 ping

PONG

PONG

PONG
$redis-cli -r 10 -i 1 info|grep used_memory_human

used_memory_human:2.95G

.....................................

used_memory_human:2.95G

每隔1秒输出内存的使用量,一共输出10次。



$redis-cli -h ip -p port info server|grep process_id

process_id:999

获取redis的进程号999

3, -x

represents reading data from standard input as the last parameter of the command.

$echo "world" |redis-cli -x set hello

Ok

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在线分析

redis-cli MONITOR | head -n 5000 | ./redis-faina.py

监控正在请求执行的命令

在cli下执行monitor,生产环境慎用。

模拟oom

redis-cli debug oom

模拟宕机

redis-cli debug segfault

模拟hang

redis-cli -p 6379 DEBUG sleep 30

获取慢查询

SLOWLOG GET 10

结果为查询ID、发生时间、运行时长和原命令 默认10毫秒,默认只保留最后的128条。单线程的模型下,一个请求占掉10毫秒是件大事情,注意设置和显示的单位为微秒,注意这个时间是不包含网络延迟的。

slowlog get 获取慢查询日志

slowlog len 获取慢查询日志条数

slowlog reset 清空慢查询

配置:

config set slow-log-slower-than 20000
config set slow-max-len 1000
config rewrite

更多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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete