How to use the Redis command-line interface (redis-cli) (with examples)
Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. The Redis command-line interface (redis-cli) is a simple yet powerful tool that allows users to interact with a Redis server using commands.
Use case 1: Connect to the local server
Code:
redis-cli
Motivation: You may want to connect to the local Redis server to manage the data stored in it or to execute Redis commands.
Explanation:
The redis-cli
command without any arguments opens a connection to the Redis server running on the default host (localhost
) and port (6379
).
Example output:
127.0.0.1:6379>
Use case 2: Connect to a remote server on the default port
Code:
redis-cli -h host
Motivation: You may need to connect to a Redis server running on a remote machine to manage or retrieve data.
Explanation:
The -h
option specifies the host (IP address or hostname) of the Redis server. By providing the host argument, the redis-cli
command connects to the Redis server running on the specified host and default port (6379
).
Example output:
host:6379>
Use case 3: Connect to a remote server specifying a port number
Code:
redis-cli -h host -p port
Motivation: If the Redis server is running on a non-standard port, you can connect to it by providing the port number.
Explanation:
The -p
option specifies the port number of the Redis server. By providing both the host and port arguments, the redis-cli
command connects to the Redis server running on the specified host and port.
Example output:
host:port>
Use case 4: Connect to a remote server specifying a URI
Code:
redis-cli -u uri
Motivation: Some Redis hosting providers may provide a URI (Uniform Resource Identifier) that can be used to connect to their Redis servers.
Explanation:
The -u
option specifies the URI of the Redis server. By providing the URI argument, the redis-cli
command connects to the Redis server specified in the URI.
Example output:
host:port>
Use case 5: Specify a password
Code:
redis-cli -a password
Motivation: If the Redis server is password-protected, you need to provide the correct password to establish a connection.
Explanation:
The -a
option specifies the password required to authenticate with the Redis server. By providing the password argument, the redis-cli
command connects to the Redis server using the provided password.
Example output:
host:port>
Use case 6: Execute Redis command
Code:
redis-cli redis_command
Motivation:
You can directly execute Redis commands using the redis-cli
command.
Explanation:
Simply provide the desired Redis command as an argument to the redis-cli
command, and it will be executed on the connected Redis server.
Example output:
OK
Use case 7: Connect to the local cluster
Code:
redis-cli -c
Motivation:
If you are connecting to a Redis cluster, using the -c
option enables cluster mode and allows you to execute cluster-specific commands.
Explanation:
The -c
option enables cluster mode in redis-cli
, which means it can connect to a Redis cluster and execute commands that operate on the cluster as a whole.
Example output:
127.0.0.1:6379> CLUSTER INFO
Conclusion:
The redis-cli
command-line interface provides various options for connecting to Redis servers, executing commands, and managing Redis data. Whether you are connecting to a local or remote server, specifying a port number or URI, or executing Redis commands, redis-cli
is a versatile tool that simplifies Redis server management and interaction.