Steps to Setup with Examples Redis Server on Ubuntu 20.04 LTS

Redis is a free & open source in-memory key-value data store. Redis can be used as a database, cache & message broker. It supports various data structures and wide language & it provides high availability via Redis Sentinel.

There are some steps to setup Redis server on ubuntu:

Step 1: Update the System.

apt-get update

Step 2: Install the Redis on system.

apt install redis-server

  • Check the Redis version.

redis-server --version

  • Here is the command output.

root@ip-172-31-39-198:/home/ubuntu# redis-server --version
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

  • To allow 6379 port number in firewall.

ufw allow 6379

  • Here is the command output.

root@ip-172-31-39-198:/home/ubuntu# ufw allow 6379
Rules updated
Rules updated (v6)

Step 3: Configure the Redis file.

vim /etc/redis/redis.conf

  • Set the following value.To declare an init system to manage Redis as a service.

supervised systemd

  • Here is the command output.

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd

  • Restart the Redis.

systemctl restart redis.service

Step 4:Run the following command to connect the server.

redis-cli

  • Here is the command output.

root@ip-172-31-39-198:/home/ubuntu# redis-cli
127.0.0.1:6379>

  • To check connectivity,Run ping command.

127.0.0.1:6379> ping

  • Here is the command output.

PONG

  • To Set key value.

Syntax: set key value [expiration EX seconds|PX milliseconds] [NX|XX]
127.0.0.1:6379> set test "example"

  • To Retrieve the value.

get test

  • Here is the command output.

127.0.0.1:6379> set test "example"
OK
127.0.0.1:6379> get test
"example"

  • To exit the Redis prompt.

127.0.0.1:6379> exit

Step 5: To Set Redis password.

  • Open the Redis configuration file.

vim /etc/redis/redis.conf

  • Add the following values:

requirepass redis_password
requirepass admin

  • Restart the Redis.

systemctl restart redis.service

  • Again run redis-cli command to connect the server.

redis-cli

  • To check the Redis password is working or not.Run the key value set command.

set key 10

  • Here is the command output.Now we are getting authentication error.

root@ip-172-31-39-198:/home/ubuntu# redis-cli
127.0.0.1:6379> set key 10
(error) NOAUTH Authentication required.

  • Run the auth command for authentication.

auth redis_password
auth admin

  • Here is the command output.

127.0.0.1:6379> auth admin
OK

  • After successfully authentication,set & retrieve the key value.

set key 10
&
get key

  • Here is the command output.

127.0.0.1:6379> set key 10
OK
127.0.0.1:6379> get key
"10"

Leave a Reply