Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
at practice.RedisJava.main(RedisJava.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 6 more

这可能是两个原因造成的,首先可能是redis的6379端口无法访问,请先在cmd中输入命令 
telnet 127.0.0.1 6379 
看看可不可以访问redis-server 机器的6379端口,如果不能访问,需要在远程机器关掉防火墙或者添加允许通过 
1)使用root用户登录,vi /etc/sysconfig/iptables,添加如图所以一行

# Generated by iptables-save v1.4.21 on Tue Nov 21 12:32:27 2017
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1:156]-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
COMMIT
# Completed on Tue Nov 21 12:32:27 2017

2)输入命令service iptables restart重启防火墙

或者可以直接root用户使用命令service iptables stop关闭防火墙。

防火墙检查完后,如果还是出现上述问题,说明redis还有地方需要配置,redis默认是只有本机可以访问的,想要远程访问需要修改redis.conf配置文件。 
进入redis.conf目录,并使用vi命令打开,找到bind那行修改后,wq保存退出,重启redis-server。

bind 后加的是允许访问的ip 
bind 127.0.0.1代表只有本机可以访问,可以将允许访问的ip加入,也可以直接注释掉这一行,这样所有机器都可以访问

解决上述问题后出现的新问题:DENIED Redis is running in protected mode

报错信息如下:

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:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
at redis.clients.jedis.Protocol.read(Protocol.java:215)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
at redis.clients.jedis.Jedis.set(Jedis.java:121)
at roy.redis.test.Test.init(Test.java:13)
at roy.redis.test.Test.main(Test.java:8)

主要是这里的原因:redis开启了保护模式,我们可以将它设置为no;

当然,另一种安全的做法是

Java 连接操作 Redis 出现错误的更多相关文章

  1. Java 连接使用 Redis

    1. 开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java. 首先你需要下载驱动包,下载 jedis.j ...

  2. Redis入门教程(三)— Java中操作Redis

    在Redis的官网上,我们可以看到Redis的Java客户端众多 其中,Jedis是Redis官方推荐,也是使用用户最多的Java客户端. 开始前的准备 使用jedis使用到的jedis-2.1.0. ...

  3. java代码操作Redis

    1.导入需要的pom依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  4. Redis Java连接操作

    安装 要在Java程序中使用使用操作Redis,需要确保有Redis的Java驱动程序和Java设置在机器上.可以检查看Java教程-学习如何在机器上安装Java.现在,让我们来看看如何设置Redis ...

  5. Java连接远程Redis

    redis-server &  //后台启动redis redis-cli //使用redis   打开redis.conf文件在NETWORK部分有说明   /usr/local/src   ...

  6. Java代码操作Redis的sentinel和Redis的集群Cluster操作

    总共四台机器,crxy99,crxy98分别是主节点和从节点.   crxy97和crxy96是两个监控此主从架构的sentinel节点. 看代码: import org.junit.Test; im ...

  7. 使用java代码操作Redis

    1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...

  8. Python进阶学习_连接操作Redis数据库

    安装导入第三方模块Redis pip3 install redis import redis 操作String类型 """ redis 基本命令 String set(n ...

  9. 20190928-02使用Redis客户端Jedis连接Redis,以及用Java代码操作Redis 000 030

    启动redis package com.yujie.jedis; import java.util.HashMap; import java.util.Map; import java.util.Se ...

随机推荐

  1. Curl https 访问

    如果访问https的网页,出现: curl: (60) SSL certificate problem: unable to get local issuer certificate 将 将 CURL ...

  2. Linux系统——引导过程与服务控制

    一.Linux开机启动原理(十步) (1)开机自检BIOS 开机检测,主板检测 (2)MBR引导 硬盘512字节 (3)GRUB菜单 操作系统菜单 (4)加载内核(kernel) 启动操作系统核心,根 ...

  3. 清晰讲解LSB、MSB和大小端模式及网络字节序

    时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...

  4. Java GC随笔

    最近发生了一些C#程序运行时的一些问题,发现是GC导致的问题,然后稍微研究了一下GC,因为知道Java的GC要比.NET稍微复杂一点,所以我觉得要是能弄懂Java的GC的原理,对.NET的GC的理解也 ...

  5. [2012-12-18 14:59:31]AS3 常用正则表达式的总结-不用google了,我帮收集的很多了

    正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里转摘出了一些常用的正则表达式语句,大家用到的时候就不用自己写了.红色字体为常用正则:下面这个类是我自己写的一个示例,想必大 ...

  6. ::before ::after CSS3中的伪类和伪元素

    ::before和::after伪元素的用法 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:fo ...

  7. 2017年4月16日 一周AnswerOpenCV佳作赏析

    2017年4月16日 一周AnswerOpenCV佳作赏析 1.HelloHow to smooth edge of text in binary image, based on threshold. ...

  8. 20145331 《Java程序设计》第6周学习总结

              20145331 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1.1串流 •Java将输入/输出抽象化为串流,数据有来源及目的地,衔 ...

  9. 如何确保C#的应用程序只被打开一次

    http://stackoverflow.com/questions/184084/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-w ...

  10. Permission denied: mod_fcgid

    [Tue Jun 16 13:29:08 2015] [warn] (13)Permission denied: mod_fcgid: spawn process /var/www/cgi-bin/g ...