Redis Notes - How to start a Redis server
Running Redis on Ubuntu 20.04
Install Redis via
apt
tool.sudo apt update sudo apt install redis-server
Checking that the Redis service is running:
sudo systemctl status redis
If it is running without errors, the output will be like:
● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enable> Active: active (running) since Fri 2020-08-07 16:02:26 CST; 6min ago Docs: http://redis.io/documentation, man:redis-server(1) Process: 8982 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=> Main PID: 8983 (redis-server) Tasks: 4 (limit: 3713) Memory: 2.9M CGroup: /system.slice/redis-server.service └─8983 /usr/bin/redis-server 127.0.0.1:6379
Connect to the server using
redis-cli
, the Redis command line interface:redis-cli
Test connection with the
ping
command:127.0.0.1:6379> ping PONG 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> get hello "world"
To shutdown the Redis server
Using
redis-cli
command to shutdown a Redis server is a relatively elegant way, this will save the data into RDB snapshot before the server shutdown, so you can get data back after the server rebooted.redis-cli shutdown [save|nosave]
You will see the log of Redis outputs:
$ redis-cli shutdown # the Redis server log output 74902:M 11 Oct 2020 19:10:06.457 # User requested shutdown... 74902:M 11 Oct 2020 19:10:06.458 * Saving the final RDB snapshot before exiting. 74902:M 11 Oct 2020 19:10:06.460 * DB saved on disk 74902:M 11 Oct 2020 19:10:06.460 # Redis is now ready to exit, bye bye...
Start, Stop and disable a system service in Ubuntu system
$ systemctl start redis
$ systemctl stop redis
$ systemctl restart redis
# Enable/disable the redis service at boot
$ systemctl enable redis
$ systemctl disable redis
Running Redis on MacOS
- Install Redis via
brew
:brew install redis
- To start a Redis server:
Option 1. Run Redis server as service process:
brew services start redis brew services stop redis brew services restart redis
Option 2. Run a Redis server and let output shows on terminal:
redis-server
To shutdown a Redis server with
redis-cli
:redis-cli shutdown
Deploy Redis in Docker
Pull the Redis image from Docker Hub, start a Redis instance based on Alpine Linux in background, and start listening TCP port 6379:
docker run -d --rm --name redis -p 6379:6379 redis:alpine
Test Redis through telnet
command:
telnet 127.0.0.1 6379
The outputs will be like:
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Note: Exit
telnet
interface by type inquit
command.
And the Redis server is ready to go, test the connection by Redis ping
command:
ping
+PONG
Stop the instance:
docker container stop redis