SoFunction
Updated on 2025-03-08

Summary of the use of redis-cli command line tool

The redis-cli command line tool is a powerful Redis client that allows users to interact and manage with the Redis database.

Here are some common parameters:

Basic connection parameters

  • -h, --host <hostname>: Specify the host name or IP address of the Redis server to which you want to connect to. If not specified, the default is 127.0.0.1 (i.e. localhost).
  • -p, --port <port>: Specify the port number of the Redis server to which you want to connect to. If not specified, the default is 6379, which is the default port for Redis.
  • -a, --password <password>: Specify the password required to connect to the Redis server. If the Redis server has password protection set, you need to use this parameter to provide the password.
  • -n, --db <db>: Specify the Redis database number to connect to. Redis provides 16 databases by default (numbers from 0 to 15). This parameter allows you to select the database to connect to.

Basic connection method

If you run the Redis server locally and use the default port (6379), you can directly enter redis-cli in the terminal to connect.

For example:

$ redis-cli
127.0.0.1:6379>

Connect to a remote server

If the Redis server is on a remote host, you need to specify the host name or IP address and port number.

For example, suppose the IP address of the remote Redis server is 192.168.1.100 and the port is 6379, you can connect using the following command:

$ redis-cli -h 192.168.1.100 -p 6379
192.168.1.100:6379>

in-hParameters are used to specify the host,-pParameters are used to specify the port.

Connect with password

If the Redis server has password protection set, you can use the -a parameter to provide the password. For example, if the password is mypassword, the connection command is as follows:

$ redis-cli -h 192.168.1.100 -p 6379 -a mypassword
192.168.1.100:6379>

However, this method will display the password in the command line history, which poses certain security risks.

A safer approach is to connect without a password first, and then authenticate using the AUTH command. For example:

$ redis - cli -h 192.168.1.100 -p 6379
192.168.1.100:6379> AUTH mypassword
OK

Operation and format parameters

  • -r, --repeat <count>: Specify the number of times the command is to be executed repeatedly. This parameter can be used with the -i parameter to implement the function of repeating commands at a time.
  • -i, --interval <seconds>: Set the interval time (in seconds) for command execution. This parameter is usually used with the -r parameter to control the repeated execution rate of the command.
  • -x: Read the last parameter from standard input (stdin). This allows the user to read data from a pipeline or other input source and pass it to redis-cli as a parameter of the command.
  • --raw / --no-raw: Controls the format of command output. Use the –raw parameter to print the return result of Redis in the original format, including data types and values. --no-raw returns to the default output format.
  • --csv: Print the return result in CSV (comma-separated value) format. This helps import Redis's output into spreadsheets or other applications that support CSV format.

-r parameter repeat command

Meaning: The -r parameter is used to specify the number of times the command is executed. This is useful when you need to execute the same command multiple times, such as stress testing for an operation or batch insertion of data.

Example: Suppose you want to set the value of the key my_r to an autoincremental number and repeat it 5 times. The following commands can be used:

$ redis-cli -r 5 incr my_r
(integer) 1
(integer) 2
(integer) 3
(integer) 4
(integer) 5

-i parameter specifies the command interval time

Meaning: The -i parameter is used to specify the interval time between each command execution, in seconds. It is usually used with the -r parameter and can be used when commands need to be executed multiple times at certain time intervals.

Example: The following command will be executed every 1 secondincr my_iThe command is executed 5 times in total.

$ redis-cli -r 5 -i 1 incr my_i
(integer) 1
(integer) 2
(integer) 3
(integer) 4
(integer) 5

-x parameter read standard input

Meaning: Used to read data from standard input (stdin) as the value of the last parameter. This is useful when dealing with relatively long or complex data (such as large strings, binary data, etc.), making it possible to pipe or otherwise pass data to the redis-cli command instead of entering verbose data directly on the command line.

$ echo hello | redis-cli -x set hi
OK
$ redis-cli get hi
"hello\n"

echo will output hello to standard output, and the content of the standard output is as the value of the set command (due to the existence of the -x parameter), and the set command will store this value in the key hi.

–raw/–no-raw control command output format

Meaning: used to display data in raw format. By default, some data types (such as hashes, lists, collections, etc.) are formatted for easy reading. However, in some cases, you may want to get the exact byte representation of the data, or use the data directly for script processing, and you can use the –raw option.

$ redis-cli hgetall student_scores
1) "Alice"
2) "95"
3) "Bob"
4) "85"
5) "Charlie"
6) "100"
$ redis-cli --raw hgetall student_scores
Alice
95
Bob
85
Charlie
100
$ redis-cli --no-raw hgetall student_scores
1) "Alice"
2) "95"
3) "Bob"
4) "85"
5) "Charlie"
6) "100"

–csv prints the return result in CSV format

Meaning: Format the output of the command into CSV (comma-separated value) format. CSV is a commonly used data exchange format, especially suitable for importing and exporting data between spreadsheet software (such as Microsoft Excel, Google Sheets) or database systems.

$ redis-cli --csv hgetall student_scores
"Alice","95","Bob","85","Charlie","100"

Advanced functional parameters

  • --scan <pattern>: Execute the SCAN command to iterate through all keys in Redis. The number of each iteration can be controlled with the COUNT parameter. Compared to the KEYS command, the SCAN command is incremental and does not block the Redis server.
  • --bigkeys: Execute SCAN command on the Redis server to find the big key. This helps analyze Redis's memory usage and identify keys that consume a lot of memory.
  • --rdb <filename>: Specify the RDB file of the Redis database for export. This allows the user to back up the Redis database.
  • --slave: Connect to the Redis server in slave node mode. This is usually used to set up Redis replication and slave management.
  • --pipe: Send Redis commands using pipeline mode. This can speed up the execution of large batches of commands, especially for scenarios such as data migration or batch updates.
  • --eval <script> <keys> [args]: Execute the specified Lua script. This parameter allows users to run custom Lua scripts on the Redis server to implement complex logical operations and data processing.
  • --latency: Execute the PING command on the Redis server to obtain the delay information. This helps monitor the performance and response time of the Redis server.
  • --stat: Print statistics of Redis server. This includes key indicators such as memory usage and command processing rate, which helps users understand the operating status of the Redis server.

Migrate redis script by key:

#!/bin/bash
source_host="127.0.0.1"
source_port=6379
source_db=0
destination_host="127.0.0.1"
destination_port=6379
destination_db=1
redis-cli -h $source_host -p $source_port -n $source_db keys "*" | while read key
do
t=`redis-cli -h $source_host -p $source_port -n $source_db --raw ttl $key`
if test $t -eq -1
then
    t=0
fi
redis-cli -h $source_host -p $source_port -n $source_db --raw -D dump $key | redis-cli -h $destination_host -p $destination_port -n $destination_db -x restore $key $t
echo "migrate key $key"
done

use--rdbBackup redis:

$ redis-cli --rdb 
sending REPLCONF capa eof
sending REPLCONF rdb-only 1
SYNC sent to master, writing 205 bytes to ''
Transfer finished with success.

This is the end of this article about the use of the redis-cli command line tool. For more related content on the use of redis-cli, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!