Jedis连接搭建在阿里云服务器上的Redis,基于Linux(CentOS7)
现象
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 127.0.0.1:6379
at redis.clients.jedis.Connection.connect(Connection.java:204)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
at redis.clients.jedis.Connection.sendCommand(Connection.java:125)
at redis.clients.jedis.Connection.sendCommand(Connection.java:120)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)
at com.yang.test.TestPing.main(TestPing.java:10)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:181)
... 6 more
产生原因
- 机器之间网络没连通
- ip和端口号不正确
- 虚拟机中防火墙的原因
- redis.conf 中bind 127.0.0.1 未用#注释掉
注:由于redis本身默认处于保护模式,redis.conf注释掉bind 127.0.0.1,会产生安全问题
解决:
- 将protected-mode yes中yes改为no
- 设置连接密码,以下采用的是第二种
解决方法
1. 将Jedis连接的IP地址改为服务器公网IP
2. 修改redis.conf配置文件,设置redis访问密码
3. 退出redis-server
3. 打开redis.conf,注释掉 bind 127.0.0.1,意味着所有客户端访问都可访问,相当于 bind 0.0.0.0
4. 重启redis-server
5. 连接redis-cli ,ping测试是否,连接成功
6. 阿里云服务器配置安全组
7. 防火墙开放6379端口,不然无法访问