Cannot connect to redis using jedis

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

Redis version: 3.2.0 Jedis version: 2.8.1

Below is my java code for connecting to redis:

public class TestRedis { public static void main(String[] args) {     String host = args[0];     int port = Integer.parseInt(args[1]);     try (Jedis jedis = new Jedis(host, port)) {         System.out.println("Connected to jedis " + jedis.ping());     } catch(Exception e){         e.printStackTrace();     } } 

}

I am running this program in the machine where redis is installed. This machine's ip address is 192.168.1.57

If I provide host="localhost" and port = "6379" as arguments, connection with redis successfully established.

However, If I give host="192.168.1.57" and port = "6379" in arguments, I end up with below exception:

redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused     at redis.clients.jedis.Connection.connect(Connection.java:164)     at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)     at redis.clients.jedis.Connection.sendCommand(Connection.java:100)     at redis.clients.jedis.Connection.sendCommand(Connection.java:95)     at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:93)     at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:105)     at TestRedis.main(TestRedis.java:14) Caused by: java.net.ConnectException: Connection refused     at java.net.PlainSocketImpl.socketConnect(Native Method)     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)     at java.net.Socket.connect(Socket.java:579)     at redis.clients.jedis.Connection.connect(Connection.java:158)     ... 6 more 

Please help...

回答1:

There are a few settings that would affect this: bind and protected-mode. They work together to provide a baseline of security with new installs.

Find the following in your redis.conf file and comment it out:

bind 127.0.0.1 

By adding a # in front of it:

# bind 127.0.0.1 

Or, if you would rather not comment it out, you can also add the IP if your eth0/em1 interface to it, like this:

bind 127.0.0.1 192.168.1.57 

Also, unless you're using password security, you'll also have to turn off protected mode by changing:

protected-mode yes 

To:

protected-mode no 

Make sure that you read the relevant documentation and understand the security implications of both of these changes.

After making these changes, restart redis.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!