在Ubuntu上安装Redis客户端
$ sudo apt install redis-tools
安装后,我们将能够使用“redis-cli命令”来打开RediS终端到远程服务器。
我们还使用ping命令来验证连接。
$ redis-cli -h redis-ubuntu redis-ubuntu:6379> ping PONG redis-ubuntu:6379>
如果redis服务器未使用默认端口,则可以在“redis-cli命令”中使用-p
选项指定端口,如下所示:
$ redis-cli -h redis-ubuntu -p 1234
如果我们获得“连接拒绝”错误消息,检查redis服务是否启动,是否打开了防火墙。
Could not connect to Redis at redis-ubuntu:6379: Connection refused
在Ubuntu上安装Redis Server
将同时自动安装Redis客户端包。
$ sudo apt install redis-server
检查已安装redis的版本:
$ redis-server -v Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923
查看redis监听端口:
$ ss -nlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 511 127.0.0.1:6379 0.0.0.0:* LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 5 [::1]:631 [::]:* LISTEN 0 511 [::1]:6379 [::]:*
默认情况下,在重启系统时,redis服务器将自动启动。
我们可以使用systemd的systemctl命令更改此行为。
我们还可以使用systemctl命令来检查Redis的当前状态。
$ sudo systemctl disable redis-server #disable Redis from starting up automatically $ sudo systemctl enable redis-server #enable Redis to start up automatically $ systemctl status redis-server #check the current status of Redis server
默认情况下,redis服务器只会侦听本地环回接口127.0.0.1
,所以无法远程连接。
修改配置文件
$ sudo nano /etc/redis/redis.conf
将下面这行注释(前面添加#号),使Redis侦听所有网络接口:
bind 127.0.0.1 ::1
将保护模式关闭:
protected-mode yes 改成: protected-mode no
重新启动REDIS服务以使更改生效:
$ sudo systemctl restart redis-server
查看现在的监听地址:
$ ss -nlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 5 [::1]:631 [::]:* LISTEN 0 511 [::]:6379 [::]:*
设置UFW防火墙,允许端口6379
。
$ sudo ufw allow from any to any port 6379 proto tcp Rules updated Rules updated (v6)
Redis是一种开源软件,用作存储在内存中的数据库和缓存,具有优异的性能。
日期:2020-07-07 20:55:29 来源:oir作者:oir