redis-cli

Learn how to use redis-cli to connect to and manage your Redis server. This guide covers connecting, running commands like INFO, CONFIG, SELECT, FLUSHDB, FLUSHALL, KEYS, SET, and GET.

Redis CLI Guide

Redis Server Setup

To get started with Redis, you can utilize the provided docker-compose.yaml file. Simply run the following command to launch the Redis server in detached mode:

docker-compose up -d

Connecting with Redis CLI

If you have redis-cli installed locally, you can connect to the Redis server running on localhost:6379. For convenience, this guide demonstrates using a Redis container's redis-cli to connect to your Redis server, which is assumed to be accessible at redis-server:6379 within the Docker network.

To connect using a Docker container, execute:

docker run -it --network app redis:7.0 redis-cli -h redis-server -p 6379

Upon successful connection, you will see the following prompt:

redis-server:6379>

Once connected, you can directly issue commands like INFO without prefixing them with redis-cli. If you were running this command from within a container, the full command would look like: docker run -it --network app redis:7.0 redis-cli -h redis-server -p 6379 INFO.

Here are a few ways to execute commands:

  1. From your local workstation with redis-cli installed: redis-cli INFO
  2. Using a container and attaching to its shell: docker run -it --network app redis:7.0 bash, then run redis-cli -h redis-server INFO
  3. Using a container and attaching directly to the CLI: docker run -it --network app redis:7.0 redis-cli -h redis-server -p 6379
  4. Using a container to run a command in one step: docker run -it --network app redis:7.0 redis-cli -h redis-server -p 6379 redis-cli INFO

This guide will primarily use the method of attaching directly to the Redis CLI within a container:

docker run -it --network app redis:7.0 redis-cli -h redis-server

Common Redis CLI Commands

Retrieve server information:

redis-server:6379> INFO

# Server
redis_version:7.0.5
# Memory
used_memory:945592
used_memory_human:923.43K
# Persistence
loading:0
async_loading:0
# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
...

View the current Redis configuration:

redis-server:6379> CONFIG GET *

Switch to a different database (default is database 0):

redis-server:6379> SELECT 1

To check the number of available databases, you can query the server info:

docker run -it --network app redis:7.0 bash
redis-cli -h redis-server INFO | grep ^db

Delete the currently selected database:

redis-server:6379> FLUSHDB

Delete all databases:

redis-server:6379> FLUSHALL

List all keys in the current database:

redis-server:6379> keys *

Set a key-value pair in the cache:

redis-server:6379> set name ruan

Retrieve a value from the cache using its key:

redis-server:6379> get name

External Resources