redis-cli

Master Redis CLI commands for efficient data management. Learn to connect, execute commands, list keys, delete data, and more with this essential Redis command-line tool.

Redis CLI

Understanding Redis CLI

The redis-cli is the command-line interface for Redis, a powerful in-memory data structure store. It allows developers to interact with a Redis server directly from the terminal, executing commands, managing data, and monitoring server status. Mastering redis-cli is essential for efficient Redis administration and development.

Connecting to Redis Servers

Establishing a connection to your Redis instance is the first step. redis-cli provides flexible options for connecting to local or remote servers, including specifying the host, port, and authentication credentials.

# Connect to the local Redis server (default port 6379):
redis-cli

# Connect to a remote server on a specific host:
redis-cli -h your_redis_host

# Connect to a remote server with a specified port:
redis-cli -h your_redis_host -p your_redis_port

# Connect with password authentication:
redis-cli -a your_redis_password

# Connect to a remote server with host, port, and password:
redis-cli -h your_redis_host -p your_redis_port -a your_redis_password

Executing Redis Commands

Once connected, you can execute any valid Redis command directly. This is useful for quick data retrieval, manipulation, or testing.

# Execute a single Redis command:
redis-cli redis_command

# Example: Get the value of a key named 'mykey'
redis-cli GET mykey

# Example: Set a key-value pair
redis-cli SET mykey "myvalue"

Managing Redis Keys

redis-cli offers commands to manage your keys effectively, such as listing keys that match a pattern or deleting specific keys.

# List all keys that start with 'user:'
redis-cli KEYS "user:*"

# List all keys in the database
redis-cli KEYS "*"

# Delete a specific key named 'old_data'
redis-cli DEL old_data

# Delete multiple keys
redis-cli DEL key1 key2 key3

Advanced Redis CLI Usage

Beyond basic commands, redis-cli supports various options for debugging, performance monitoring, and scripting. Refer to the official Redis documentation for a comprehensive list of commands and options.

For more in-depth information on Redis commands and their usage, consult the official Redis Commands documentation.