使用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
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 保存设置:
因篇幅问题不能全部显示,请点此查看更多更全内容