How Install Redis on CentOS 7

How to Install Redis on CentOS 7
How to Install Redis on CentOS 7

Install Redis on CentOS 7

Redis is a in memory key-value data structure store mainly used as a database, message broker or as a cache. Redis supports wide languages with flexibility and high performance. It supports different data structures like strings, lists, sets, maps, spatial indexes, and bitmaps. In this tutorial, you are going to learn How to Install Redis on CentOS 7.

Prerequisites

Before you start to install Redis on CentOS 7. You must have a non-root user account on your server with sudo privileges and IPv6 should be enabled on your server otherwise Redis service won’t start.

Install Redis

Redis package is not available in CentOS 7 core repositories. So here we will install Redis from another repository.

First, install yum-utils and epel-release packages using below command:

sudo yum install epel-release yum-utils

Add Remi repository in CentOS running below command:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

After adding repository enable it by running below command:

sudo yum-config-manager --enable remi

Redis package is available in default repository in CentOS 7. So to install Redis run below command:

sudo yum install redis

After installing Redis, it will not start automatically so to start redis service run below command:

sudo systemctl start redis

And enable Redis service so it will automatically start after boot running below command:

sudo systemctl enable redis

Check the status of Redis service by running below command:

sudo systemctl status redis-server

The output should look like:

● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
        └─limit.conf
Active: active (running) since Sat 2019-03-21 15:12:03 PST; 39s ago
Main PID: 2157 (redis-server)
CGroup: /system.slice/redis.service
        └─2157 /usr/bin/redis-server 127.0.0.1:6379

Binding Redis

By default, you can not access Redis from another host because its by default bound to localhost only. To confirm its bound to localhost(127.0.0.1), follow below instructions:

First, run below command and open Redis configuration file:

sudo nano /etc/redis.conf

Now uncomment bind 127.0.0.1 line by removing # from the beginning. Now it’s bound to localhost only.

bind 127.0.0.1

If you want to access Redis from remote host then you can replace YOUR_IP_ADDRESS with your servers IP Address:

bind 127.0.0.1 YOUR_IP_ADDRESS

Now save the and close it by pressing CTRL+x.

To take changes effect, restart Redis server by running below command:

sudo systemctl restart redis-server

Now run below command to confirm above changes:

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      14222/redis-server  
tcp6       0      0 153.168.93.106:6379           *:*               LISTEN      14222/redis-server  

Next, we will setup FirewallD for accessing Redis from a remote host:

Create a zone names redis by using below command:

sudo firewall-cmd --new-zone=redis --permanent

Now open port 6379 permanently using below command:

sudo firewall-cmd --zone=redis --add-port=6379/tcp --permanent

Run the following command by replacing YOUR_CLIENT_IP_ADDRESS with IP Address of the machine from which you want to access Redis:

sudo firewall-cmd --zone=redis --add-source=YOUR_CLIENT_IP_ADDRESS --permanent

Finally, reload FirewallD to tale changes effect:

sudo firewall-cmd --reload

Now to check everything is ok, start redis-cli using below command:

redis-cli

Above command will start redis-cli shell. So to check everything works fine run below command:

ping

You should see exact below output:

PONG

Exit the redis-cli shell using below command:

exit

Testing Using redis-client

To start testing Redis, run redis-cli using below command:

redis-cli

It will open redis-cli shell, Now store a value with the key myname with value John run below command inside it:

set myname "John"

The output should be:

OK

Now run below command to check the value of myname:

get myname

You should see the following output:

John

Exit the redis-cli shell using below command:

exit

Conclusion

You have successfully learned how to install Redis on CentOS 7. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here