使用Jedis连接云服务器
学习使用Jedis时,因为连接的是阿里云的服务器。然后一直连接失败。
Jedis jedis = new Jedis(主机地址,6379);
刚开始觉得是阿里云的安全组里面没有开启6379,于是我去打开了端口,还是报错

于是,我觉得可能是主机的防火墙没有开启这个端口,输入ufw status发现防火墙压根就没开。
然后打开Redis的配置文件看到protected-mode 默认是yes,改成no
然后把绑定端口哪里注释掉默认的 bind 127.0.0.1,他就会监听所有的可用的地址。也可以用bind绑定你要监听地址。
启动redis
redis-server config/config
查看配置是否生效,改了配置文件需要重新启动redis
root@iZwz95hcen4u1xgrsl8iyaZ:/usr/local/bin# netstat -lnp |grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1972/redis-server *
tcp6 0 0 :::6379 :::* LISTEN 1972/redis-server *
我看到注释是这样解释的
By default, if no “bind” configuration directive is specified, Redis listens
for connections from all the network interfaces available on the server.
It is possible to listen to just one or multiple selected interfaces using
the “bind” configuration directive, followed by one or more IP addresses.
System.out.println(jedis.ping()); //输出pong
参考 https://blog.csdn.net/xc121566/article/details/96967155
版权声明:本文为weixin_44779854原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。