Redis CLI Tutorial
Introduction to Redis CLI
Redis CLI is the command-line interface tool used to interact with the Redis database. It allows you to run commands, query data, and manage the Redis server directly from the terminal.
Installing Redis CLI
To use Redis CLI, you need to have Redis installed on your machine. Follow these steps to install Redis:
sudo apt update
sudo apt install redis-server
Once installed, you can start the Redis server with:
sudo service redis-server start
Starting Redis CLI
To start the Redis CLI, simply open your terminal and type:
redis-cli
You should see the Redis prompt:
127.0.0.1:6379>
Basic Commands
Here are some basic commands to get you started with Redis CLI:
SET and GET
To set a value in Redis:
SET mykey "Hello"
To retrieve the value:
GET mykey
"Hello"
DEL
To delete a key:
DEL mykey
Trying to get the value of a deleted key:
GET mykey
(nil)
Working with Data Structures
Redis supports various data structures such as lists, sets, and hashes. Here are some examples:
Lists
To add elements to a list:
LPUSH mylist "World"
LPUSH mylist "Hello"
To retrieve elements from the list:
LRANGE mylist 0 -1
1) "Hello"
2) "World"
Sets
To add elements to a set:
SADD myset "Hello"
SADD myset "World"
To retrieve elements from the set:
SMEMBERS myset
1) "World"
2) "Hello"
Hashes
To set fields in a hash:
HSET myhash field1 "Hello"
HSET myhash field2 "World"
To retrieve fields from the hash:
HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "World"
Server Commands
Redis CLI also allows you to run server commands to manage the Redis instance.
INFO
To get information and statistics about the server:
INFO
CONFIG
To get or set server configuration parameters:
CONFIG GET maxmemory
1) "maxmemory"
2) "0"
CONFIG SET maxmemory 256mb
Conclusion
Redis CLI is a powerful tool that allows you to interact with your Redis server efficiently. By understanding and using the basic commands and data structures, you can manage your Redis instance effectively.