Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary and function of redis command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Today, what the editor shares with you is the summary and function of redis commands. I believe many people do not know much about it. In order to let you know more about redis commands and functions, I have summarized the following contents for you. Let's look down together. I'm sure you'll get something.

First, after redis is installed, there are several executables that begin with redis under src and / usr/local/bin, called redis shell, which can do a lot of things.

1. Redis-server starts redis

2. Redis-cli redis command line tool

3. Redis-benchmark benchmark testing tools

4. Redis-check-aof AOF persistence file detection tool and repair tool

5. Redis-check-dump RDB persistence file detection tool and repair tool

6. Redis-sentinel starts redis-sentinel

Second, you can connect to the redis server in two ways.

The first one: interactive way

Redis-cli-h {host}-p {port} is connected, and then all operations are implemented interactively, eliminating the need for redis-cli. $redis-cli-h 127.0.0.1 get hello p 6379127.0.0.1 get hello "world"

The second way: command mode

Redis-cli-h {host}-p {port} {command} directly gets the return result of the command. $redis-cli-h 127.0.0.1 Murp 6379 get hello "world"

Redis-cli contains a number of parameters, such as-hjingle talk p. To learn all the parameters, use the redis-cli-help command.

Part one: command mode

1.-r means to repeat the command many times

$redis-cli-r 3 pingPONGPONGPONG

The ping command can be used to detect whether the redis instance is alive or not, and if so, PONG is displayed.

2.-I

Execute the command every few seconds (if you want to use ms, such as 10ms), you must use it with-r.

$redis-cli-r 3-I 1 pingPONGPONGPONG$redis-cli-r 10-I 1 info | grep used_memory_humanused_memory_human:2.95G..used_memory_human:2.95G outputs the memory usage every 1 second, for a total of 10 times. $redis-cli-h ip-p port info server | grep process_idprocess_id:999 obtains the process number of redis

3.-x

Represents reading data from standard input as the last argument to the command.

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

4.-c

Used when connecting to cluster nodes, this option prevents moved and ask exceptions.

5.-a

If a password is configured, the an option is available.

6.-scan and-pattern

The key used to scan the specified mode, equivalent to the scan command.

7.-slave

When the current client is simulated as a slave node of the current redis node, it can be used to obtain the update operation of the current redis node. Rational use can be used to record some updates that are currently connected to the redis node, which may be the data needed for the actual development of the business.

8.-rdb

The redis instance is requested to generate and send RDB persistence files to be saved locally. Regular backups can be made.

9.-pipe

The command is encapsulated into the data format defined by redis communication protocol and sent to redis for execution in batch.

10.-bigkeys

Count the distribution of bigkey, use the scan command to sample the keys of redis, and find the keys that take up a large amount of memory, which may be the bottleneck of the system.

11.-eval

Used to execute lua scripts

12.-latency

There are three options,-latency,-latency-history,-latency-dist. They can detect network latency and show it in different forms.

13.-stat

The important statistical information of redis can be obtained in real time. Although the info command is relatively complete, you can see some additional data here, such as requests (requests per second)

-raw and-no-raw

-no-raw requires that the original format be returned. -raw shows the effect of formatting.

Part II

There are many redis-cli commands. such as

Commands related to the connection operation:

1. Default direct connection remote connection-h 192.168.1.20-p 6379

2. Ping: test whether the connection is alive. If it is normal, it will return pong.

3. Echo: printing

4. Select: switch to the specified database. The database index number index is specified by a numeric value, with 0 as the starting index value.

5. Quit: close the connection (connection)

6. Auth: simple password authentication

Server-related commands:

1. Time: returns the current server time

2. Client list: returns all client information and statistics connected to the server. See synonyms at

3. Http://redisdoc.com/server/client_list.html

4. Client kill ip:port: close the client with the address ip:port

5. Save: save data synchronization to disk

6. Bgsave: save data to disk asynchronously

7. Lastsave: returns the Unix timestamp of the last time data was successfully saved to disk

8. Shundown: save the data synchronization to disk, and then shut down the service

9. Info: provide server information and statistics

10. Config resetstat: reset some statistics in the info command

11. Config get: get configuration file information

12. Config set: dynamically adjust the configuration (configuration) of the Redis server without restarting. You can use the command to modify the configuration parameters.

13. CONFIG GET * to list

14. Rewrite the redis.conf file specified by the config rewrite:Redis server

15. Monitor: dump the received requests in real time

16. Slaveof: change 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 the information message to the 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: returns the number of subscribers for a given channel, such as PUBSUB NUMSUB news.it news.internet news.sport

News.music

5. Pubsub numpat: returns the total number of all patterns subscribed by the client

6. Punsubscribe: instructs the client to unsubscribe from all given modes.

7. Subscribe: subscribe to the information of a given channel or 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): confirm whether a key exists.

2. Del (key): delete a key

3. Type (key): the type of return value

4. Keys (pattern): returns all key that satisfy a given pattern

5. Randomkey: randomly returns one of the key spaces

6. Keyrename (oldname, newname): rename key

7. Dbsize: returns the number of key in the current database

8. Expire: set the active time of a key (s)

9. Ttl: get the active time of a key

10. Move (key, dbindex): move key in the current database to dbindex database

11. Flushdb: delete all key in the currently selected database

12. Flushall: delete all key in all databases

Commands for String operations:

1. Set (key, value): assign the value value to the string named key in the database

2. Get (key): returns the value of the string named key in the database

3. Getset (key, value): assign the last value to the string named key

4. Mget (key1, key2, … , key N): returns the value of multiple string in the library

5. Setnx (key, value): add string with a name of key and a value of value

6. Setex (key, time, value): add string to the library and set the expiration time time

7. Mset (key N, value N): set the values of multiple string in batch

8. Msetnx (key N, value N): if all string named key I do not exist

9. Incr (key): add 1 to string named key

10. Incrby (key, integer): add integer to the string named key

11. Decr (key): string minus 1 operation named key

12. Decrby (key, integer): string named key reduces integer

13. Append (key, value): the value of the string named key is appended to value

14. Substr (key, start, end): returns the substring of value named string with the name key

Commands for List operations:

1. Rpush (key, value): add an element with the value value to the end of the list named key

2. Lpush (key, value): add an element with a value of value to the list header named key

3. Llen (key): returns the length of the list named key

4. Lrange (key, start, end): returns the elements between start and end in the list named key

5. Ltrim (key, start, end): intercept the list named key

6. Lindex (key, index): returns the element of index location in the list named key

7. Lset (key, index, value): assign a value to the element of index position in the list named key

8. Lrem (key, count, value): delete the element whose value is value in the list of count key

9. Lpop (key): returns and deletes the first element in the list named key

10. Rpop (key): returns and deletes the tail element in the list named key

11. Blpop (key1, key2, … Key N, timeout): the block version of the lpop command.

12. Brpop (key1, key2, … Key N, timeout): the block version of rpop.

13. Rpoplpush (srckey, dstkey): returns and deletes the tail element of the list named srckey and adds it to the header of the list named dstkey

Commands for Set operations:

1. Sadd (key, member): add the element member to the set named key

2. Srem (key, member): delete the element member in the set named key

3. Spop (key): randomly returns and deletes an element in the set named key

4. Smove (srckey, dstkey, member): move to the collection element

5. Scard (key): returns the cardinality of the set named key

6. Sismember (key, member): whether member is an element of set named key

7. Sinter (key1, key2, … Key N): find the intersection

8. Sinterstore (dstkey, (keys)): find the intersection and save the intersection to the collection of dstkey

9. Sunion (key1, (keys)): join set

10. Sunionstore (dstkey, (keys)): join and save the union to the collection of dstkey

11. Sdiff (key1, (keys)): difference set

12. Sdiffstore (dstkey, (keys)): find the difference set and save the difference set to the set of dstkey

13. Smembers (key): returns all elements of the set named key

14. Srandmember (key): randomly returns an element of set with the name key

Commands for Hash operations

1. Hset (key, field, value): add the element field to the hash named key

2. Hget (key, field): returns the value corresponding to field in the hash named key

3. Hmget (key, (fields)): returns the value corresponding to field I in the hash named key

4. Hmset (key, (fields)): add the element field to the hash named key

5. Hincrby (key, field, integer): add integer to the value of field in the hash named key

6. Hexists (key, field): whether there is a domain with the key field in the hash named key

7. Hdel (key, field): delete the domain whose key is field in the hash named key

8. Hlen (key): returns the number of elements in the hash named key

9. Hkeys (key): returns all keys in the hash named key

10. Hvals (key): returns the value of all keys in the hash named key

11. Hgetall (key): returns all keys (field) and their corresponding value in the hash named key

Example

Query online analysis

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

Monitor the commands that are being requested

Execute monitor under cli, use it cautiously in production environment.

Simulated oom

Redis-cli debug oom

Simulated downtime

Redis-cli debug segfault

Simulated hang

Redis-cli-p 6379 DEBUG sleep 30

Get slow query

SLOWLOG GET 10

The result is that the query ID, occurrence time, run time and the original command defaults to 10 milliseconds, and only the last 128 entries are retained by default. Under the single-threaded model, it is a big deal for a request to take up 10 milliseconds. Note that the unit of setting and display is microseconds, and note that this time does not include network latency.

Slowlog get acquires slow query log

Slowlog len acquires the number of slow query log entries

Slowlog reset empties slow query

Configuration:

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

These are the details of the summary and function of the redis command. Have you gained anything after reading it? If you want to know more about it, welcome to the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report