There is probably a command built for every operation that you can think of performing in Redis. This makes Redis a popular choice for a plethora of functions.
This article will introduce the Redis CLI, starting from installation to primary command usage.
Installing Redis
The first step is to ensure that Redis is installed on your system. For this guide, we will illustrate how to install Redis on Ubuntu.
Step 1: Update the system packages:
$ sudo apt-get upgrade
Step 2: Install Redis as:
Step 3: Enable and start the redis service:
$ sudo systemctl start redis-server
Using the Redis CLI
Once Redis is installed and running, open the CLI interface by running the command:
This should log you in into the Redis CLI interface with the prompt as shown below:
The prompt comprises the IP address and the port under which the Redis server is running.
Test if Server is Up
Once in the Redis CLI, you can run all supported Redis commands on your databases. One such command is ping. It allows you to test if the server is up by returning PONG if true.
Example usage is as shown:
PONG
Connect to Redis CLI on Custom Port
The Redis server may be running on a custom port in some instances. Unless specified, the Redis CLI will attempt to the default Redis port on 6379.
To connect using a custom port, run:
The -p option allows you to specify a custom port to the Redis server.
If you are connecting to a remote host, use the -h option as shown:
Redis Switch Database
Redis provides you with 16 databases starting from index 0 to index 15. You can switch to a database using the select command as:
OK
127.0.0.1:6379[15]>
The command above will switch from database 1 to database 16. Note that the currently selected database is shown in the prompt.
Redis Login as User
If your Redis server is secured with a password, you must authenticate before running commands.
For that, you can use the auth command as shown:
OK
Note: Password, in this case, refers to the password of the target Redis username.
Check our tutorial on Redis ACL to learn more.
You can also use the -a option to authenticate. The example command usage is as shown:
Redis List connected clients
To view the connected clients on your Redis server, run the command:
127.0.0.1:6379> client list
This should return information about the connected clients, as shown below:
Conclusion
This article covered the fundamentals of using the Redis CLI to run commands on the Redis server. Check most important redis commands to learn more.