8.5.14. Redis CLI
Redis CLI is a utility for working with Redis via a command line interface. A detailed description of all features is available in official documentation.
Built-in documentation
View brief documentation:
redis-cli -h
Connect
Using a connection string:
redis-cli -u redis://example.redis.tools:10000
Instead of redis://example.redis.tools:10000, use the connection string to the desired Redis instance copied from the control panel.
Using host and port:
redis-cli -h example.redis.tools -p 10000
Instead of example.redis.tools and 10000, use the host and port from the connection string to the desired Redis instance in the control panel.
Authorization after connecting:
AUTH login "password"
Instead of login and password, use the Redis user data. If no login is specified, authorization will be under the default user.
For hosting If Redis is ordered as a extra service, you must use a Unix socket to connect:
redis-cli -s ~/.system/redis.sock
Use
Managing data
Output all recorded keys:
keys '*'
Output all recorded keys with key_ in the name:
keys key_*
Symbols can be used in a pattern:
*— any number of any character.?— one of any character.
Write example_data with the key key_example:
set key_example "example_data"
Get the data previously written with the key_example key:
get key_example
Get the type of data stored in the key_example key:
type key_example
Rename the key from key_example to key_another_example:
rename key_example key_another_example
Check the existence of the key_example key:
exists key_example
If the key exists, a 1 will be output, if not, a 0 will be output.
Delete the data and the key_example key:
del key_example
Get the lifetime of the key key_example:
ttl key_example
The default output is -1, which means there is no lifetime limit.
Set the lifetime of the key_example key and the data recorded in it, after which the key will be deleted:
expire key_example
Command execution queue
Activate the mode of writing commands to the queue:
multi
This mode is useful for alternately executing multiple commands.
Execute all commands queued after multi:
exec
If an error occurs while specifying commands, all entries in the queue will be reset.
Operations with numbers
Increment, increment by one the value of data with key key_example:
incr key_example
Increment, increase the value of data with key key_example by the number specified instead of increment:
incrby key_example increment
Decrement, decrement by one the value of data with key key_example:
decr key_example
Decrement, increment the value of data with key key_example by the number specified instead of decrement:
decrby key_example decrement
Operations with strings
Add the text test_text to the end of the data string:
append key_example "test_text"
The length of the written string of data in the key:
strlen key_example
Get a specific character range of a string:
getrange key_example start end
The start and end of the range are specified instead of start and end. For example, for the string 1234567890 the command getrange key_example 2 6 will return 4567. The characters are counted from 0.
Replace the data written in the key starting with the character numbered start:
setrange key_example start "text"
For example, the string 1234567890 will be changed to 1212367890 by the command setrange key_example 2 "123".
Working with lists
Add a new item with the text test to the end of the list:
rpush key_example "test"
Output the list items starting from the item numbered start and ending with the number stop:
lrange key_example start stop
Get the number of list items:
llen key_example
Delete the first list item and get the data of the next one:
lpop key_example