使用Jedis连接虚拟机上的Redis数据库时,出现错误,记录下解决方法

错误

redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refus: connect

修改配置文件redis.conf,将bind注释掉,允许外面的机器连接

这里写图片描述

在修改后出现新的问题:

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:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:142)
    at redis.clients.jedis.Protocol.read(Protocol.java:196)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:207)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:196)
    at redis.clients.jedis.Jedis.hget(Jedis.java:606)
    at com.wds.jdemo.JedisTest.test(JedisTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这个问题是由于Redis是在守护状态下运行的,如果不是本地连接,就会报错,解决方法在问题描述中已经给出: 
1)在Redis运行时,本地连接使用命令:CONFIG SET protected-mode no,但是要确保Redis不在公网上运行,使用CONFIG REWRITE是改变永久生效

2)修改配置文件redis.conf,将protected mode 的值改为no,并重新启动redis服务

3) 重新启动服务,在启动命令后加上protected-mode no参数

4)绑定ip地址或设置一个密码

本次采用了第二种解决方案

连接Redis

导包 
这里写图片描述

代码

import org.junit.Test;

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

public class JedisTest {

    /**
     * 单实例连接数据库 
     */
    @Test
    public void test(){
        Jedis jedis = new Jedis("192.168.47.130",6379);
//      System.out.println(jedis.hget("hash", "uname"));
        jedis.hset("hash", "pwd", "123");
    }

    /**
     * 使用连接池
     */
    @Test
    public void test1(){
        //1.设置连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置池中最大连接数
        config.setMaxTotal(50);
        //设置空闲时池中保有的最大连接数
        config.setMaxIdle(10);
        //2.设置连接池对象
        JedisPool pool = new JedisPool(config,"192.168.47.130",6379);
        //3.从池中获取连接对象
        Jedis jedis = pool.getResource();
        System.out.println(jedis.hget("hash", "uname"));
        //4.连接归还池中
        jedis.close();

    }
}

转载地址:https://blog.csdn.net/sinat_36553913/article/details/78697967

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐