step 9: install redis (redis.io store)

redis logo

Installation of redis on CentOS 6:

Redis (the redis.io key-value store) is available in the remy repo which depends on the epel repo:

If you didn't already previously add the remi repo to your repositories then follow the following step to do so.

I wanted the remi repo "Enterprise Linux 6 (with EPEL)" package, you may have to use another one depending on your OS and version, i used centos6 and therefore choosed to use the following commands:


# rpm --import https://fedoraproject.org/static/0608B895.txt
# rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
# wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

edit the remi repo file using the vi editor to change the line enabled from 0 to 1:

first open the directory with all the repositories files

# cd /etc/yum.repos.d

list all the files in that directory, using the following command

# ls -l

now there should be a file in the list that has the name remi.repo, open the remi repo file with vi

# vi remi.repo

press the "i" key to go into edit mode

change enabled from 0 to 1

press the escape key to exit from the edit mode

type :x! to save and exit

or type :q! to quit without saving

Now that you have access to the remi repo you can use the following yum command to install redis:

# yum install redis

add redis to the startup scripts type this:

# chkconfig --level 345 redis on

To check if redis is correctly set to start on boot use this command:

# chkconfig --list redis

That's it for the installation of redis. On the next reboot of your server redis will get launched automatically.

If you want to launch Redis right away and test if it got successfully installed use the following commands:


# service redis start
# redis-cli ping

The server should answer "pong".

Another way to check if redis is up is to check if the service is running:

# service redis status

You should get an answer like: "redis-server (pid XY) is running...".

Configure redis

You might now want to edit the configuration of redis.

If you have installed Redis using the remi repo the redis.conf file will be in the /etc folder. If you can't find the redis conf, type the following command:


# updatedb
# locate redis.conf

To start configuring redis type this:

# vi /etc/redis.conf

For example, by default redis is bound to "127.0.0.1:6379" to the localhost. If you want to access redis from outside you will have to change this. To make redis listen on your IP address on the port you choose change these two lines to whatever suits your needs, like this:



# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 1111

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
bind 111.222.333.444

And if your redis server is accessible from outside you might want to enable authentication which is disabled by default. As recommended by redis, try to choose a super strong password.



# Require clients to issue AUTH  before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass my_super_SECRET-PaSsWoRd-54654fsdfds6545wtet3211yvx

But if you don't need access from outside you still might want to use the redis socket.



# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /tmp/redis.sock
unixsocketperm 755

For more help about configuration options, check out the official redis configuration documentation.

Multiple redis instances

Make a copy of your original configuration for the second instance:

# cp /etc/redis.conf /etc/redis2.conf

Now you need to edit the new configuration file of your second redis instance:

# vi /etc/redis2.conf

Depending on whenever you use the socket or ip and port to connect to redis either change the following to lines:

port 6379

to

port 6380

and / or

unixsocket /tmp/redis.sock

to

unixsocket /tmp/redis2.sock

To be able to launch the second deamon, make a copy of the initialization scripts:

# cp /etc/init.d/redis /etc/init.d/redis2

Edit the initialization script:

# vi /etc/init.d/redis2

Now change pid and configuration path:

pidfile="/var/run/redis/redis.pid"
to
pidfile="/var/run/redis/redis2.pid"
and
REDIS_CONFIG="/etc/redis.conf"
to
REDIS_CONFIG="/etc/redis2.conf"

Now also add your second redis instance to the startup script:

# chkconfig --level 345 redis2 on

To check if redis is correctly set to start on boot use this command:

# chkconfig --list redis2

If you want to launch your second redis instance right away, type:

# service redis2 start

To check if your second redis instance is up and running, type:

# service redis2 status

You should get an answer like: "redis-server (pid XY) is running...".

Redis command line

You can use redis from the command line by using the redis "command line interface" (cli) utility.

For example to check which version that you are using type:

# redis-cli --version

To use redis-cli just type:

# redis-cli

To exist hold the [ctrl] key and then hit the [c] key or type "exit" while in redis-cli mode.

If you have set a password for redis in the configuration you need to pass the password to redis while connecting to redis-cli, like this:

# redis-cli -a my_Password

Redis supports mutliple databases. Databases in redis have no name, you define in the configuration how much databases you want to use. Databases have zero-based numeric indexes. New connections always use DB 0.

To switch to another database with for example the index 1, using redis-cli type this command while in cli mode, type:

Select 1

To set a key/value, type:

SET mykey "Hello World"

To get the value for a given key, type:

GET mykey

To flush (clear) all the key/values from the entire (current) database, type:

FLUSHDB

To flush (clear) all the key/values from all the databases, type:

FLUSHALL

To get an overview of the available commands type:

# redis-cli --help

For more help about the commands, check out the official redis commands documentation.