Java连接虚拟机的redis报错问题解决办法
来源:互联网 发布:网络教育 档案 公考 编辑:程序博客网 时间:2024/06/06 10:31
直奔主题,Java连接虚拟机报错,代码如下:
public class TestPing {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.201.128", 6379);
System.out.println(jedis.ping());
}
}
运行报错,控制台信息如下:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
借鉴一些网友的解决方案:
关闭虚拟机的防火墙
1)暂时关闭防火墙:/etc/init.d/iptables stop
2) 重启虚拟机生效:chkconfig iptables off 或者 /sbin/chkconfig --level 2345 iptables off
楼主使用的是第二种方法,如下图所示:
重启虚拟机之后,在查看防火墙状态,如下图所示
继续运行上述代码,报错如下:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
解决方案,修改redis.conf配置文件,将端口号127.0.0.1注释掉,这样任何IP都能访问,如下图所示:
保存退出后,运行代码,发现报错依旧,因为尚未启动redis服务,启动redis服务,如下图所示:
运行代码,此时报错如下:
<span style="color:#ff0000;">Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: </span>
<span style="color:#ff0000;">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: </span>
<span style="color:#ff0000;">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. </span>
<span style="color:#ff0000;">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. </span>
<span style="color:#ff0000;">3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. </span>
<span style="color:#ff0000;">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.</span>
上述exception大致意思是:拒绝redis在保护模式下运行的,没有绑定IP地址,没有授权密码,如果你想从外部电脑连接redis的话,你必须要采纳以下其中的某一个解决方法
楼主采用的第二种方法,修改redis.conf配置文件,将protected-mode yes 改为no,如下图所示
重启虚拟机,启动redis服务,如下图所示
运行代码,控制台输出PONG,如下图所示:
到此,问题解决。
题外话:我有个疑问就是防火墙和redis.conf配置文件里已经把IP注释了,为何会报红色的错误,经过网上查阅资料,发现其他网友也有类似问题,不过大部分人用的红色报错的第四种解决办法,在这里给出链接
- Java连接虚拟机的redis报错问题解决办法
- Java连接linux虚拟机的redis报错问题解决办法
- Java连接虚拟机的redis报错问题解决办法
- Java连接虚拟机的redis报错问题解决办法
- Java连接虚拟机的redis报错问题解决办法
- java连接Redis报错
- RedisDesktopManager无法连接虚拟机中启动的redis服务问题解决
- Java连接虚拟机linux环境下的redis时报错
- Java连接虚拟机的redis异常
- Java 连接虚拟机上的 redis
- 安装Ecshop首页出现报错常见的问题解决办法
- cocopods 一直报错找不到头文件的问题解决办法
- Log4j的知识点以及相关报错问题解决办法
- Genymotion无法启动虚拟机的问题解决办法
- java https连接以及乱码问题解决办法
- redis 连接报错
- ora-01536报错问题解决办法
- 调试xcode项目报错问题解决办法
- codeforces 598D dfs
- 如何建立动态库
- 从苏宁电器到卡巴斯基第33篇:番外篇——晒一晒病毒分析师的桌面(下)
- Java.sql.Date精确度问题处理
- X-Frame-Options配置
- Java连接虚拟机的redis报错问题解决办法
- crowd windows
- 遇到ORA-01940: cannot drop a user that is currently
- 公钥和私钥联系和区别
- Java中的容器类List、Set、Map的对比
- 用WINHEX合并两个或多个BIN文件
- CMake构建CUDA项目
- 很不错的网页收集,受用了
- ubuntu系统安装git软件