安装Redis
sudo apt update
sudo apt install redis-server
查看运行状态
使用systemctl工具查询
sudo systemctl status redis-server
输出类似如下所示:
redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-12-05 14:15:23 PST; 27s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 2024 (redis-server)
Tasks: 4 (limit: 2359)
Memory: 6.9M
CGroup: /system.slice/redis-server.service
└─2024 /usr/bin/redis-server 127.0.0.1:6379
如果禁用了IPv6,则Redis服务将无法启动。
配置Redis远程访问
默认情况下,Redis配置为仅在本地主机上侦听,且不设置密码。
开启远程访问。编辑配置文件/etc/redis/redis.conf
,搜索以开头的行bind 127.0.0.1 ::1
并对其进行注释:
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
#
# bind 127.0.0.1 ::1
添加访问密码。编辑配置文件/etc/redis/redis.conf
,新增requirepass
设置访问密码。
requirepass test123;
重新启动Redis服务以使更改生效:
sudo systemctl restart redis-server
使用ss
查看端口监听情况
ss -an | grep 6379
or
netstat -lntp|grep 6379
输出
tcp LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
tcp LISTEN 0 128 [::]:6379 [::]:*
防火墙放通远程访问
iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
完成后,使用该redis-cli
实用程序通过从远程计算机ping Redis服务器来测试连接:
redis-cli -h <REDIS_IP_ADDRESS> -a <REDIS_PASS> ping
返回以下响应PONG
:
PONG