03.jedis连接redis 连接不上

本文介绍了解决使用Jedis客户端连接Redis服务器时遇到的连接异常问题,包括如何正确配置Redis服务以允许外部连接,并禁用受保护模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虚拟机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模式
最终如下图:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值