无法使用 jedis 连接到 redis [英] Cannot connect to redis using jedis

查看:91
本文介绍了无法使用 jedis 连接到 redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 版本:3.2.0绝地版本:2.8.1

Redis version: 3.2.0 Jedis version: 2.8.1

下面是我连接redis的java代码:

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();
    }
}

}

我在安装了 redis 的机器上运行这个程序.本机的ip地址是192.168.1.57

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

如果我提供 host="localhost" 和 port="6379" 作为参数,则与 redis 的连接成功建立.

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

但是,如果我在参数中给出 host="192.168.1.57" 和 port = "6379",我会得到以下异常:

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

请帮忙...

推荐答案

有一些设置会影响这个:bindprotected-mode.他们共同努力为新安装提供安全基准.

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.

在您的 redis.conf 文件中找到以下内容并注释掉:

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

或者,如果您不想将其注释掉,也可以将您的 eth0/em1 接口的 IP 添加到其中,如下所示:

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

致:

protected-mode no

确保您阅读相关文档并了解这两项更改的安全含义.

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

进行这些更改后,重新启动 redis.

After making these changes, restart redis.

这篇关于无法使用 jedis 连接到 redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆