9

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 on the machine where redis is installed. The public IP address is 192.168.1.57

If I provide host="localhost" and port = "6379" as arguments, then I can successfully connect with redis.

However, If I give host="192.168.1.57" and port = "6379" in arguments, I end up with the following 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
  • In my case, I had to open an SSH tunnel into the remote host to resolve the issue Commented May 10, 2023 at 22:49

1 Answer 1

8

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 of 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.

Sign up to request clarification or add additional context in comments.

1 Comment

I implemented all of these suggestions to no avail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.