虚拟机中运行redis-server,通过物理机Java代码或Redis桌面管理客户端连接失败解决方法

在虚拟机上的CentOS 7系统上运行Redis服务,防火墙开放了6379端口,redis.conf 配置文件中也将bind 27.0.0.1 注释掉了(默认开启,表示只能通过运行server的本机,也就是centos7内部连接)。但在物理机上通过Another Redis DeskTop Manager客户端还是连接不上,编写Java测试代码使用Jetdis尝试连接抛出以下错误:

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.

第一次见到这么详细的报错信息,可以说完全是教程了。Redis给出了这个问题的4种解决方法:

  1. 通过在运行服务的同主机下连接到Redis并执行CONFIG SET protected-mode no 命令即可。但这样做的前提是要确保Redis不是从外网公开访问的。再执行CONFIG REWRITE 命令可以使其永久生效。
  2. 通过编辑redis.conf配置文件,将protected-mode yes 改为protected-mode no ,然后通过配置文件重启服务即可。
  3. 如果只是测试手动启动服务器,可以再启动服务的时候加上--protected-mode no 参数。
  4. 设置绑定地址或认证密码

注意:以上几种方法中,使用其中一种就可以解决外部连接问题。

你可能感兴趣的:(Java学习小问题,java,redis,数据库)