windows下jedis连接虚拟机中的redis

为了不会出现下面两个异常

  1. Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketT 异常
  2. DENIED Redis is running in protected mode because protected mode is enabled 异常
    首先在虚拟机端要对redis进行一些配置
    打开redis.config文件
    修改几处:
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1                不是注释这个

#bind 127.0.0.1                     这里要注释掉

接下来要设置redis密码

config get requirepass     #查看是否已经设置密码
config set requirepass "密码"   
auth 密码                  #访问

如何不设置密码,就修改redis.config中的配置(不推荐)

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes     #这里修改为   no

最后就是java中写连接代码

  @Test
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.1.105",6379);
        jedis.auth("123456");   //这里写自己的密码
        System.out.println(jedis.ping());
    }

要给这个代码加入两个依赖jar包
windows下jedis连接虚拟机中的redis_第1张图片
我的代码最后会在console控制台打印 PONG

至此!

你可能感兴趣的:(Redis)