Database
Redis
What is Redis?
Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and sorted sets. Redis is known for its high performance, scalability, and flexibility, making it a popular choice for caching and real-time analytics.
Install
Ubuntu
> apt update
> sudo apt install redis-server
> sudo nano /etc/redis/redis.conf
If you want to install the latest version of Redis, you can use the following commands:
> curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
> echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
> sudo apt-get update
> sudo apt-get install redis
Service
Open /etc/redis/redis.conf
and update the following settings:
# 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 no
Change supervised no
to supervised systemd
, then save and exit the file.
Restart the Redis service:
> sudo systemctl restart redis
Enable Redis to start on boot:
> sudo systemctl enable redis
Remote Access
To allow remote access to Redis, you need to update the bind
setting in the Redis configuration file:
> sudo nano /etc/redis/redis.conf
Comment out the bind
setting:
#bind 127.0.0.1 ::1
Modify the protected-mode
setting to no
:
protected-mode no
Save and exit the file, then restart the Redis service:
> sudo systemctl restart redis
Enable Password Authentication
To enable password authentication in Redis, you need to set a password in the Redis configuration file:
> sudo nano /etc/redis/redis.conf
Uncomment the requirepass
setting and set a password:
requirepass your_password_here
Pay attention to the password length and complexity. Redis has an extremely fast response speed, capable of handling 150,000 brute-force password attempts per second. Therefore, we need to set a very strong password for Redis. We can generate a password using OpenSSL:
> openssl rand 60 | openssl base64 -A
#return
RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE
Use the generated password as the value for requirepass
, then save and exit the file.
Restart the Redis service:
> sudo systemctl restart redis
FAQ
redis-server.service: Can't open PID file /run/redis/redis-server.pid (yet?) after start: Operation not permitted
This error occurs when the Redis service is unable to create the PID file due to permission issues. To resolve this, you need to update the Redis service configuration:
> sudo nano /etc/systemd/system/redis.service
Add the ExecStop
and PIDFile
to the [Service]
section:
[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
PIDFile=/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=2755