redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException:Connection refuse

在Java访问虚拟机中的redis时可能会出现以下错误:

redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException:Connection refuse

redis拒绝访问。

解决方案:首先找到redis的配置文件redis.conf,该配置文件就在redis的安装包中。

如果配置文件中存在bind 127.0.0.1语句,该条语句就是指定redis只能本机由能访问,外部机器无法访问。

因此,修改redis.conf配置文件,将bind 127.0.0.1语句注释掉(该语句前面加#注释),这样任何IP都能访问,如图:

如果bind 127.0.0.1本身已被注释,就不需要再修改了。

修改完毕,重启虚拟机,重启redis,maven项目clean和install,install的过程中可能会报如下错误,导致install失败。

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.

 这段错误的大致意思就是:redis拒绝在保护模式下运行,没有绑定IP地址,没有授权密码,如果想从外部电脑连接redis的话,通常有两种解决方法可以选择:

(1)修改redis.conf配置文件,将protected-mode yes 改为no。

这时可能你的redis.conf配置文件中没有protected-mode yes语句,如果我们强行添加的话,就会报类似“不好的或者错误的执行语句:protected-mode yes(行号)”之类的错误,那我们又不得不把它移出。

(2)这时,可以使用第二种方法,设置密码。

直接修改redis.conf配置文件:在配置文件中添加"requirepass  访问密码"的字段。(该方法未尝试)

或者,在redis启动时设置:

[root@itcast01 redis-3.2.1]# ./redis-cli
127.0.0.1:6379> config set requirepass 123456
OK
127.0.0.1:6379> 

最后,再重新启动虚拟机,重启redis,就可以通过Java的Jedis访问到设置的数据。

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class TestJedis {

    @Test
    public void testJedis(){
        //创建jedis对象,需要指定Redis服务的IP和端口号
        Jedis jedis = new Jedis("172.16.242.60", 6379);
        //直接操作数据库
        jedis.set("jedis-key", "hello jedis!");
        //获取数据
        String result = jedis.get("jedis-key");
        System.out.println(result);
        //关闭jedis
        jedis.close();
    }

    @Test
    public void testJedisPool(){
        //创建一个数据库连接池对象(单例,即一个系统共用一个连接池),需要指定服务的IP和端口号
        JedisPool jedisPool = new JedisPool("172.16.242.60", 6379);
        //从连接池中获得连接
        Jedis jedis = jedisPool.getResource();
        //使用jedis操作数据库(方法级别,就是说只是在该方法中使用,用完就关闭)
        String result = jedis.get("jedis-key");
        System.out.println(result);
        //用完之后关闭jedis连接
        jedis.close();
        //系统关闭前先关闭数据库连接池
        jedisPool.close();
    }

}

在maven install的时候返回数据,install成功。 

或者通过main( )方法访问:

public class Test {
    public static void main(String[] args){
        TestJedis testJedis =new TestJedis();
        testJedis.testJedis();
        testJedis.testJedisPool();
    }
}

返回结果:

注意,设置密码后一定要重新启动虚拟机和redis,否则可能会出现以下错误: 

redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required. 

如果出现这样的错误的话,重启虚拟机和redis,运行的maven项目重新clean和install一下就可以正常访问了。

参考:(1)https://blog.csdn.net/oxinliang12/article/details/52279143

           (2)https://blog.csdn.net/yulei_qq/article/details/51954960

           (3)https://blog.csdn.net/bug_moving/article/details/60462846

猜你喜欢

转载自blog.csdn.net/kongmin_123/article/details/81675170