一,在redis开发中遇到下面的错误
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host XXXX: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.multi(BinaryClient.java:523) at redis.clients.jedis.BinaryJedis.multi(BinaryJedis.java:1877) at com.redis.TestTX.main(TestTX.java:19) 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
二,解决办法
1)修改配置文件
注释掉bind,protected-mode为no
2)查看远程服务器的防火墙状态
systemctl status firewalld
防火墙为开启状态;
3)按照步骤执行一下命令
//查看防火墙状态
firewall-cmd --state
//开放6379端口
firewall-cmd --permanent --add-port=6379/tcp
//查看端口,执行
firewall-cmd --permanent --query-port=6379/tcp
firewall-cmd --permanent --list-ports
//重启防火墙
firewall-cmd --reload
4)重启redis
redis-cli -p 6379 shutdown
redis-server /opt/redis/back_config/redis.conf
注释:back_config是我存放redis.conf的地方,请根据自己的实际情况执行
5)测试一下连接是否成功
Jedis jedis = new Jedis("IP",6379); System.out.println(jedis.ping());
问题解决。