Redis

How to Use Redis CLI

Redis is a famous, free and open-source in-memory database that stores data in the form of key-value pairs. Thanks to its built-in commands and CLI interface, Redis is straightforward to use and manage.

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 update

$ sudo apt-get upgrade

Step 2: Install Redis as:

$ sudo apt-get install redis-server

Step 3: Enable and start the redis service:

$ sudo systemctl enable redis-server

$ sudo systemctl start redis-server

Using the Redis CLI

Once Redis is installed and running, open the CLI interface by running the command:

$ redis-cli

This should log you in into the Redis CLI interface with the prompt as shown below:

127.0.0.1:6379>

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:

127.0.0.1:6379> PING

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:

$ redis-cli -p 9001

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-cli -h 192.168.0.22 -p 9001

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:

127.0.0.1:6379> select 15

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:

127.0.0.1:6379[15]> auth password

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-cli -p 6379 -a password

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list