Installing and Configuring Redis on Ubuntu
Installing Redis Server
To install Redis on Ubuntu, use the following command:
sudo apt-get install redis-server
After installation, the Redis server will start automatically. Verify that the Redis server is running by checking its process:
ps -aux | grep redis
You should see output similar to:
redis 4162 0.1 0.0 10676 1420 ? Ss 23:24 0:00 /usr/bin/redis-server /etc/redis/redis.conf
user 4172 0.0 0.0 11064 924 pts/0 S+ 23:26 0:00 grep --color=auto redis
Check if Redis is listening on the default port (6379):
netstat -nlt | grep 6379
The output should show Redis listening on port 6379:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
You can also check the service status using:
sudo /etc/init.d/redis-server status
Using Redis Command Line Interface
The Redis CLI is installed automatically with the server. Launch it with:
redis-cli
You'll see the Redis prompt:
redis 127.0.0.1:6379>
Type help to see available commands:
redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in
"help " to get a list of possible help topics
"quit" to exit
Basic Redis Operations
Setting and Getting Values
Set a string value:
redis 127.0.0.1:6379> set user_message "Hello Redis"
OK
Retrieve the value:
redis 127.0.0.1:6379> get user_message
"Hello Redis"
Working with Numeric Values
Set a numeric value:
redis 127.0.0.1:6379> set counter 10
OK
Increment the value:
redis 127.0.0.1:6379> INCR counter
(integer) 11
redis 127.0.0.1:6379> INCR counter
(integer) 12
Check the current value:
redis 127.0.0.1:6379> get counter
"12"
Working with Lists
Add items to a list from the left:
redis 127.0.0.1:6379> LPUSH task_list "Task 1"
(integer) 1
redis 127.0.0.1:6379> LPUSH task_list "Task 2"
(integer) 2
Add an item to the right of the list:
redis 127.0.0.1:6379> RPUSH task_list "Task 3"
(integer) 3
Retrieve all items in the list:
redis 127.0.0.1:6379> LRANGE task_list 0 3
1) "Task 2"
2) "Task 1"
3) "Task 3"
Working with Hashes
Set hash fields:
redis 127.0.0.1:6379> HSET user_profile name "Jane Doe"
(integer) 1
redis 127.0.0.1:6379> HSET user_profile email "jane@example.com"
(integer) 1
Get a specific field:
redis 127.0.0.1:6379> HGET user_profile name
"Jane Doe"
Get all fields and values:
redis 127.0.0.1:6379> HGETALL user_profile
1) "name"
2) "Jane Doe"
3) "email"
4) "jane@example.com"
Set multiple hash fields at once:
redis 127.0.0.1:6379> HMSET product_info sku "XYZ-123" price 29.99 stock 50
OK
Get multiple fields:
redis 127.0.0.1:6379> HMGET product_info sku price
1) "XYZ-123"
2) "29.99"
Deleting Keys
List all keys:
redis 127.0.0.1:6379> keys *
1) "counter"
2) "task_list"
3) "user_profile"
4) "product_info"
5) "user_message"
Delete specific keys:
redis 127.0.0.1:6379> del user_message
(integer) 1
redis 127.0.0.1:6379> del product_info
(integer) 1
Verify deletion:
redis 127.0.0.1:6379> keys *
1) "counter"
2) "task_list"
3) "user_profile"
Configuring Redis Security
Setting a Password
By default, Redis doesn't require a password. To add security, set a password:
sudo vi /etc/redis/redis.conf
Find and uncomment the requirepass line:
requirepass your_secure_password
Restart Redis for changes to take effect:
sudo /etc/init.d/redis-server restart
Without the password, you can connect but can't execute commands:
redis-cli
redis 127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
Connect with the password:
redis-cli -a your_secure_password
redis 127.0.0.1:6379> keys *
1) "counter"
2) "task_list"
3) "user_profile"
Enabling Remote Access
By default, Redis only accepts connections from localhost. To enable remote access:
sudo vi /etc/redis/redis.conf
Comment out the bind line:
# bind 127.0.0.1
Restart Redis:
sudo /etc/init.d/redis-server restart
Verify that Redis is now listening on all interfaces:
netstat -nlt | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
From a remote machine, connect to Redis:
redis-cli -a your_secure_password -h redis_server_ip
Verify you can access the data:
redis redis_server_ip:6379> keys *
1) "counter"
2) "task_list"
3) "user_profile"