Pivotal Knowledge Base

Follow

How to install and use Redis on Linux

Environment

Product Version
Redis 3.0.1 and above
Linux  
Ubuntu  14.04

Purpose

Users can easily install Redis on Linux and start running Redis commands using redis-cli client.

In short, Redis is a high-performance in-memory NoSQL key-value database cache but persistent data on disk and highly scalable. By default, Redis saves snapshots of the dataset on disk in a binary file called dump.rdb at predefined intervals so any changes between these intervals will be lost when the server crashed. 

See Introduction to Redis: http://redis.io/topics/introduction

Resolution

For this demo setup, we are using Ubuntu 14.04 and the latest stable Redis version 3.0.1 available on http://download.redis.io

1)  Download, build and install Redis server on ubuntu
> cd /usr/src
> wget -c http://download.redis.io/redis-stable.tar.gz
> tar xvzf redis-stable.tar.gz
> cd redis-stable
> make && make install

2)  Install the Redis script
> utils/install_server.sh

Welcome to the Redis service installer
This script will help you easily set up a running Redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis_6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/redis_6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!

3) Check and test to make make sure Redis server is running and working
> /etc/init.d/redis_6379 status
Redis is running (27186)

> redis-cli -p 6379 info server
# Server
redis_version:3.0.1
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:12cbc4f9d2e9a54e
redis_mode:standalone
os:Linux 3.13.0-52-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.2
process_id:5636
run_id:cf81f1b40bc435ed8629559fe6930fbf9c9f4c57
tcp_port:6379
uptime_in_seconds:7279
uptime_in_days:0
hz:10
lru_clock:4959700
config_file:/etc/redis/redis_6379.conf

Run Redis commands using redis-cli client

> redis-cli

  • Set multiple hash fields to multiple values

hmset user:1  name kymeng email klim@pivotal.io phone 1234567
OK

  • Get all of the fields and values in a hash

 hgetall user:1
1) "name"
2) "kymeng"
3) "email"
4) "klim@pivotal.io"
5) "phone"
6) "1234567"

  • Get the values of all the given hash fields

hmget user:1 name email phone
1) "kymeng"
2) "klim@pivotal.io"
3) "1234567"

  • Get the value of hash field

hget user:1 email
"klim@pivotal.io"

  • Find all keys matching the given pattern

keys *
1) "user:1"

  • Get all of the fields and values in a hash

hgetall user:1
1) "username"
2) "kymeng"
3) "email"
4) "klim@pivotal.io"

  • Get the value of a configuration parameter

config get *
 1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "unixsocket"
...

  • Remove all keys from the current database

 flushdb
OK

  • Set the string value of a key

set server:redis version:3.0.1
OK

  • Find all keys matching the given pattern

keys *
1) "server:redis"

  • Get the value of a key

get server:redis
"version:3.0.1"

  • Synchronously save the dataset to disk and then shut down the server

shutdown save
not connected>

Additional information

  • For complete list of Redis commands, see http://redis.io/commands
  • For more Redis tutorials, see http://www.tutorialspoint.com/redis/index.htm

Also, check out these other How-to articles

  • How to setup Redis Session Manager on tcServer/Tomcat
  • How to setup and run multiple Redis server instances on a Linux host
  • How to setup Redis Master and Slave replication
  • How to setup Redis Sentinel and HAProxy to provide automatic failover between Redis Master and Slave servers.

Comments

  • Avatar
    Krishna Gangaraju

    very helpfull, can we define dependencies in redis installer ? like gcc need to installed first etc

Powered by Zendesk