03.jedis连接redis 连接不上

虚拟机ip地址:192.168.8.156 端口6379

使用jedis连接redis

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.8.156",6379);
        System.out.println(jedis.ping());
    }

报错:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.8.156:6379
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:97)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.BinaryClient.keys(BinaryClient.java:160)
    at redis.clients.jedis.Client.keys(Client.java:102)
    at redis.clients.jedis.Jedis.keys(Jedis.java:283)
    at com.redis.test.redisTest.main(redisTest.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
    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.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more

解决:
1.开启bind 127.0.0.1 这个配置时:redis 只接收来自于该 IP 地址列表的请求,如果不进行设置,那么将处理所有请求。
于是禁止这个配置,重启redis。
2.重启之后发现eclipse使用jedis连接没有问题了, 但是继续报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
    at redis.clients.jedis.Protocol.processError(Protocol.java:130)
    at redis.clients.jedis.Protocol.process(Protocol.java:164)
    at redis.clients.jedis.Protocol.read(Protocol.java:218)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:341)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:240)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:199)
    at com.redis.test.redisTest.main(redisTest.java:12)

解决:
发现3.2后新增protected-mode配置,默认是yes,即开启。解决方法分为两种:1、关闭protected-mode模式 2、配置bind或者设置密码
尝试方法1关闭protected-mode模式
最终如下图:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/waei08/article/details/78899007