Redis windows端连接虚拟机内Redis各种问题

 

https://blog.csdn.net/waei08/article/details/78899007

虚拟机ip地址:192.168.8.156 端口6379

使用jedis连接redis

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.8.156",6379);
        System.out.println(jedis.ping());
    }

报错:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.8.156:6379
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:97)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.BinaryClient.keys(BinaryClient.java:160)
    at redis.clients.jedis.Client.keys(Client.java:102)
    at redis.clients.jedis.Jedis.keys(Jedis.java:283)
    at com.redis.test.redisTest.main(redisTest.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more

解决: 
1.开启bind 127.0.0.1 这个配置时:redis 只接收来自于该 IP 地址列表的请求,如果不进行设置,那么将处理所有请求。 
于是禁止这个配置,重启redis。 
2.重启之后发现eclipse使用jedis连接没有问题了, 但是继续报错

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:130)
    at redis.clients.jedis.Protocol.process(Protocol.java:164)
    at redis.clients.jedis.Protocol.read(Protocol.java:218)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:341)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:240)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:199)
    at com.redis.test.redisTest.main(redisTest.java:12)

解决: 
发现3.2后新增protected-mode配置,默认是yes,即开启。解决方法分为两种:1、关闭protected-mode模式 2、配置bind或者设置密码 
尝试方法1关闭protected-mode模式 
最终如下图:

这里写图片描述

 

 

此时你以为轻松解决问题了,于是再次在Linux启动redis服务器后,在外部连接还是报错,真是too young too native,起初我以为是配置没有生效,因为明明配置都写好了,于是杀掉redis的相关进程重启服务,结果发现还是没卵用。最后研究发现问题所在:之前启动redis-server并没有指定配置文件,而Linux上的redis比较操蛋的一点就是如果你不指定配置文件去启动,那么你做的修改就没有用,会读取默认配置(PS:至于这个默认配置在哪我也不清楚),于是用下面这种方式启动就可以使修改的配置文件生效,至于config rewrite命令我测试了并没有什么卵用。 
 如下的启动方式,redis启动加载了 redis.conf配置文件,配置文件信息有效

   src/redis-server   redis.conf

 

 

个人测试:默认的127.0.0.1是注释掉的,而且在不用设置密码的情况下是可以连上的,所以主要的问题是没有开启6379端口。。在开启6379端口时,我的虚拟机一直用不了firewall的命令,安装Firewall包用yum install firewall命令提示找不到这个包,于是用其他命令去开启端口

 

给所有机器开放6379端口

1 /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
2 保存设置:
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值