SoFunction
Updated on 2025-04-20

Detailed explanation of the commonly used redis-cli commands

1 redis-cli connects to redis service

1.1 Local login without password

redis-cli

redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG

1.2 Specify ip, port, and password

redis-cli -h [ip] -p [port] -a [pwd]

1.3 Specify ip, port, password and clean the redis cache

redis-cli -h [ip] -p [port] -a [pwd] flushall

1.4 Specify ip, port, password, and database

redis-cli -h [ip] -p [port] -a [pwd] -n [db_number]

1.5 shell connection redis

#!/bin/bash
Num=`seq 1 1000`
for i in ${Num};do 
	redis-cli -h 127.0.0.1 set key-${i} value-${i}
done
echo "1000 key-values ​​have been written to redis"



//After the script is executed, you can view itredis-cli
>>get key-100

Supplement: Common commands for redis operation and maintenance

View Redis version information

# is equivalent to /usr/local/redis/src/redis-server -v[root@iZ8vbdcrmm49bxv7sirrv3Z ~]# /usr/local/redis/src/redis-server --version
Redis server v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=c72a455fc0d699b

Or client input: info server

127.0.0.1:6379> info server
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c72a4523c0d699b
redis_mode:standalone
os:Linux 3.10.0-957.21.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:2006
run_id:0e16bcb89d8eb05b95d2b127d4b98178e76c86fd
tcp_port:7501
uptime_in_seconds:6317102
uptime_in_days:73
hz:10
configured_hz:10
lru_clock:16174420
executable:/usr/local/redis/src/redis-server
config_file:/usr/local/redis/src/
......

View all [partial] information of the service

useinfo [section]Check

  • View all information
127.0.0.1:6379> info
  • View specific information
Server information
127.0.0.1:6379> info server
Persistence information
127.0.0.1:6379> info persistence

2 Redis library related commands

Switch to1Number library;redishave16Initialization library,serial number0arrive15,Used by default0Number library
select 1

If verification is required
auth [password]

View the current librarykeyNumber of
dbsize

Delete all data in the current library
flushdb

删除所have库的全部数据
flushall

3 Redis key related commands

Commands that operate on keys have unified return values ​​of 1 or 0, success is 1, failure is 0

View all current librarykey
keys *

Judge a certainkeyDoes it exist,Result1,不Result0
exists [key]

View a certainkeyTypes of,ifkeyDoes not exist,Then returnnone
type [key]

Delete the specifiedkeydata,Return successfully1,Failed to return0
del [key]

according tovalueSelect non-blocking deletion,Onlykeysfromkeyspace元data中删除,After real deletion, follow-up asynchronous operations
unlink [key]

View a certainkeyExpiration time,unit(Second),-1It means never expires,-2It means it has expired
ttl [key]

Give to the specifiedkeySet expiration time,unit(Second)
expire [key] 10

4 redis string related commands

  • set <key> <value>: Add a data such as set k1 v1, add data with key k1 and value v1
  • get <key>: Get the data of a certain key, for example, get k1, get the value of key k1
  • append <key> <value>: Append the given value to the end of the original value of a certain key, and return the added character length
  • strlen <key>: Query the length of a key's value
  • setnx <key> <value>: Only when the key does not exist can it be added successfully. When the key does not exist, it cannot be added
  • incr <key>: Increase the value of a key and the number is 1, which only works for the number. If it is empty, the new value is 1
  • decr <key>: Decrease the value of a key by 1, which only works for the number. If it is empty, the new value is -1
  • incrby/decrby <key> <step size>: Increase and decrease the stored numeric values ​​in the key, customize the step size
  • mset <key1> <value1> <key2> <value2>: Add in batches
  • mget <key1> <key2>: Batch value
  • msetnx <key1> <value1> <key2> <value2>: Add batches if and only if all keys do not exist (because of atomicity, one fails if it fails)
  • getrange <key> <start position> <end position>: Get the range of a key value, getrange k1 0 2, element containing 0 position, and element with 2 position
  • setex <key> <expiration time> <value>: Set the expiration time (units of seconds) while setting the key value
  • getset <key> <value>: Set the new value and return the old value

5 redis list related commands

  • lpush/rpush <key> <value1> <value2> <value3>: Add data from the left or right
  • lpop/rpop <key>: Take out a value from the left or right, and the value in the list will not exist
  • rpoplpush <key1> <key2>: Take a value from the right side of key1 and add it to the left side of key2
  • lrange  <key> <start position> <end position>: Obtain the element according to the index corner mark (from left to right). When the end is -1, it means that there are many, and this value is still in the list.
  • lindex <key> <index>: Obtain the corresponding value from left to right according to the index
  • llen <key>: Obtain the list length
  • linsert <key> before/after <value> <newVlaue>: Add a new value before/after a value under a key
  • lrem <key> <n> <value>: Delete a key and delete n data with value
  • lset <key> <index> <value>: Change the value of a lower corner under a certain key to the given value

6 Redis collection related commands

  • sadd <key> <value1> <value2> <value3>: Add data, if the value already exists, ignore the value
  • smembers <key>: Get all values ​​in the set
  • sismember <key> <value>: determine whether there is a value in a key, it returns 1, and it does not exist. Scrat  <key>: Returns the number of elements in the set
  • srem <key> <value1> <value2>: Delete certain elements in a key
  • spop <key>: Take a random value from the key. If the value is gone, then the key will be gone.
  • srandmember <key> <n>: Take out n values ​​from a key, and will not be deleted from the collection
  • smove <key1>  <key2> <value>: Move one value in the set to another
  • sinter <key1>  <key2>: Take the intersection of two sets
  • sunion <key1>  <key2>: Take the union of two sets
  • sdiff <key1>  <key2>: Take the difference between two sets (in key1, not in key2)

7 redis hash related commands

  • hset  <key> <field> <value>: Copy the field key in a key hash table as value
  • hget <key> <field>: Get the value of a field in a key hash table
  • hmset <key> <field1> <value1> <field2> <value2>: Add data in batches
  • hexists <key> <field1>: Whether a field exists in a key hash table
  • hkeys <key>: View all fields in a key hash table
  • hvals <key>: View all values ​​in a key hash table
  • hincrby  <key> <field> <increment>: Add the increment of the response to the value of a field key in the hash table of a key
  • hsetnx  <key> <field> <value>: The field key in a key hash table is copied to value, and succeeds if and only if the key does not exist

8 Redis Ordered Set (Zset) related commands

  • zadd  <key> <score1> <value1><score2> <value2>: Add a score with multiple elements, score as the score, the set is sorted from low to high, and the score can be repeated
  • zrange <key> <start> <end> \[withscores\]: Check elements of a certain range. When the end is -1, query all, add them with scores and find them together.
  • zrangebyscore <key> <min> <max> \[withscores\]: Query data with score scores within a certain range, sorted from small to large
  • zrevrangebyscore  <key> <max> <min> \[withscores\]: Query data with score scores within a certain range, sorting from large to small
  • zincrby  <key> <increment> <value>: Add the specified increment to the score of the element
  • zrem <key> <value>: Delete data
  • zcount <key> <min> <max>: Statistics the number of data with score scores within a certain range
  • zrank  <key> <value>: Returns the sort in the collection, the sort starts from 0

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.