Redis CLI basic commands¶
The Redis command-line interface (CLI) serves as the primary tool for interacting with a Redis server directly from a terminal.^[600-developer__big-data__redis__redis-01-install.md] The default binary for this interaction is redis-cli.^[600-developer__big-data__redis__redis-01-install.md]
To begin an interactive session, you must first ensure the Redis server is running and then launch the client tool.1
cd src
./redis-cli
Once inside the interactive shell, the user prompt typically changes to redis>, indicating a connection has been established.^[600-developer__big-data__redis__redis-01-install.md] From this prompt, you can execute commands to manipulate data or test the connection status.
Connection Testing¶
A common first step in CLI usage is verifying the connection to the server using the PING command.^[600-developer__big-data__redis__redis-01-install.md]
redis> ping
PONG
If the server is operating correctly, it will respond with PONG.^[600-developer__big-data__redis__redis-01-install.md]
Key-Value Operations¶
Redis is a key-value store, and the CLI allows for the immediate setting and retrieval of string data using the SET and GET commands.^[600-developer__big-data__redis__redis-01-install.md]
For example, to assign the value "bar" to a key named "foo":
redis> set foo bar
OK
To retrieve that value later:
redis> get foo
"bar"
The server replies with OK upon a successful assignment, and returns the specific value string when requested.^[600-developer__big-data__redis__redis-01-install.md]
Numeric Operations¶
The CLI provides commands for atomic manipulation of numerical values stored in strings.^[600-developer__big-data__redis__redis-01-install.md] The INCR command increments the value of a key by 1.
redis> incr mycounter
(integer) 1
redis> incr mycounter
(integer) 2
If the key does not exist, it is assumed to be 0 before the operation, effectively setting it to 1 after the first increment.^[600-developer__big-data__redis__redis-01-install.md]
Database Management¶
Redis instances support multiple logical databases, which can be managed via specific commands.
- Switching Databases: You can switch between different database instances using the
SELECTcommand followed by an index number (e.g.,SELECT 1).^[600-developer__big-data__redis__redis-01-install.md] - Viewing Size: The
DBSIZEcommand returns the number of keys stored in the currently selected database.^[600-developer__big-data__redis__redis-01-install.md] - Clearing Data:
FLUSHDBremoves all keys from the current database.^[600-developer__big-data__redis__redis-01-install.md]FLUSHALLremoves all keys from all databases on the server.^[600-developer__big-data__redis__redis-01-install.md]
Related Concepts¶
- [[Redis]]
- [[Data types]]
Sources¶
^[600-developer__big-data__redis__redis-01-install.md]
-
While the source provides the commands for checking server status (e.g.,
ps,netstat) and running the server (./redis-server), the logical workflow of starting the server before launching the client is inferred. ↩