SysAdmin’s guide to REDIS CLI Commands
Redis is an open source in-memory data structure store that is mainly used as a caching server but can also be used as a database or as a message broker. We have already discussed about the Redis server & its installation as well as setting up Master Slave setup for Redis server in our previous tutorials.
Recommended Read : A Gitlab tutorial — Things to do after installation
In this tutorial, we will learn about some of the redis cli commands, mostly used by SysAdmins for managing or monitoring redis servers. But first let’s start with the installation of redis cli utility.
Redis CLI installation
When we install Redis server, redis cli utility is installed along with it. But we don’t need install Redis server on all the system that will be connecting to another redis server, we can just install redis-cli only.
Let’ s start with the installation on CentOS/RHEL systems first,
First install system dependencies,
# yum install wget gcc make -y
Now download & extract the redis source packages,
# tar -xvzf redis-stable.tar.gz
Now we will make redis dependencies & compile it,
# cd redis-stable/deps
# make hiredis jemalloc linenoise lua geohash-int
# cd ..
& laslty will copy the redis-cli file to /usr/bin to make it accessible system wide,
# cp src/redis-cli /usr/bin
Installation of redis cli is pretty simple on Ubuntu. We just need to install package named ‘redis-tools’ ,
$ sudo apt-get install redis-tools
NOTE:- If you don’t have redis cli installed on the system, you can also use telnet to connect to the redis server,
$ telnet REDIS_SERVER_IP REDIS_PORT
$ telnet 192.168.1.1 6379
We will now discuss some of the useful redis cli commands.
1- Connecting to a local redis instance
If we are connecting to redis-instance on local server, we can run the following command,
Or if we have multiple instance of redis server on same machine, than we can also define the port number to connect,
$ redis-cli -p 6379
2- Connecting to remote redis instance
To connect to redis instance that is on a remote server, run the following command,
$ redis-cli -h 192.168.1.1 -p 6379
Note:- Once connected we will not be prompted for password, we need to execute command ‘AUTH’ followed by password.
We can also mention the password with same command so that we dont have to enter it,
$ redis-cli -h 192.168.1.1 -p 6379 -a MYPASSWORD
3- Check if a redis instance is up or not
To check whether a redis instance is up or not, we can execute the following command,
$ redis-cli -h 192.168.1.1 -p 6379 ping
& we should get the output as ‘PONG’ in reply. We can also set up continous PING response monitoring by mentioning a time interval with option ‘i’ in above mentioned command,
$ redis-cli -h 192.168.1.1 -p 6379 ping -i 2
here, 2 is delay in seconds.
4- Checking some basic server stats
To the check some stats of the redis server instace, we will use option ‘–stat’,
$ redis-cli -h 192.168.1.1 -p 6379 –stat
This redis cli command provides the following information,
– total number of keys on the server,
– total number of clients connected to server,
– currentl total number of connections,
– memory usage
– Number of requests served by the server.
5- Detailed server statistics & information
To check the detailed server statistics & information, we will use the command ‘INFO’. Connect to redis server instance,
$ redis-cli -h 192.168.1.1 -p 6379
& than type the following command,
It provides the following information,
server: General information about the Redis server
clients: Client connections section
memory: Memory consumption related information
persistence: RDB and AOF related information
stats: General statistics
replication: Master/slave replication information
cpu: CPU consumption statistics
commandstats: Redis command statistics
cluster: Redis Cluster section
keyspace: Database related statistics
6- Monitor all the commands executed on redis
To check what and all commands are running on the redis-instance, we can use command monitor,
$ redis-cli monitor
7– Create a backup of redis instance
To create backup of the redis instance, connect to the redis server instance,
$ redis-cli -h 192.168.1.1 -p 6379 -a PASSWORD
& than run the command “SAVE” to create a backup,
This will create a file named ‘dump.rdb’ into the current redis instance directory.
Note:- There is another command called ‘BGSAVE’ that we can use to take backup. Only difference is that BGSAVE takes the backup while running is background.
8 — Restore a backup
To restore a backup, we only need to copy the file ‘dump.rdb’ into the redis installation directory & need to restart the redis service.
9- Create a Keys
To create a new key, connect to the redis instance & than run the following command,
redis> SET test-key-1redis> redis-cli –scan–pattern ‘test*’
here, test-key-1 is the name of the key.
10- Delete a key
To delete a key, connect to the redis instance & than run DEL command followed by the name of the key,
redis> DEL test-key-1
11- Delete all keys
To delete all the keys from the redis server, we can use the command ‘flushall’,
$ redis-cli flushall
12- List all keyshttps://redis.io/commands
To list all keys, connect to redis instance & than run
redis> redis-cli –scan
This will produce list of all the keys, to search for a particular pattern, run
redis> redis-cli –scan–pattern ‘test*’
These were some of the redis cli commands that we normally use, for the complete list of the redis cli commands & their detailed usage, you should refer HERE. This is it for this tutorial, please do leave your suggestions or queries in the comment box below.
If you think we have helped you or just want to support us, please consider these :-
Donate us some of your hard earned money:
Linux TechLab is thankful for your continued support.
Originally published at https://linuxtechlab.com/sysadmins-guide-to-redis-cli-commands/ on September 5, 2018.