Operating Redis on a Linux system is primarily done through the redis-cli
command-line tool, which serves as the main interface for interacting with Redis servers. Below are some basic operation examples to help you efficiently manage and retrieve data from Redis.
Connecting to the Redis Server
- Local Connection Without Password: If your Redis server is running locally and does not require password authentication, simply use:
redis-cli
- Remote or Password-Protected Connection: For scenarios where authentication is required or the server is not running on the default port, connect using:
redis-cli -h <hostname> -p <port> -a <password>
Replace <hostname>
with your server’s address (use 127.0.0.1
for local), <port>
with the Redis service port (default is 6379), and <password>
with the necessary access password.
Retrieving the Value of a Specific Key
- Interactive Mode: Once inside
redis-cli
, use theGET
command to fetch the string value stored under a specific key.
127.0.0.1:6379> GET mykey
"myvalue"
- Non-interactive Mode (Command Line): Directly retrieve the value and exit without entering interactive mode.
redis-cli GET mykey
Batch Retrieval of Multiple Keys’ Values
Use the MGET
command to retrieve values for multiple keys at once, returning a list containing each key’s corresponding value.
redis-cli MGET key1 key2 key3
Retrieving Values from Other Data Structures
- Hashes: To get field values within a hash, use
HGET
orHGETALL
.
redis-cli HGET myhash field
redis-cli HGETALL myhash
- Lists: Retrieve list elements using
LRANGE
.
redis-cli LRANGE mylist 0 -1
- Sets: Get set members using
SMEMBERS
.
redis-cli SMEMBERS myset
- Sorted Sets: Obtain sorted set elements using
ZRANGE
.
redis-cli ZRANGE myzset 0 -1 WITHSCORES
These instructions should provide a solid foundation for managing and utilizing your Redis database more effectively from a Linux environment using redis-cli
.