mysql_ping 踩内存_redis_踩坑_Failed connecting to host

刚学习redis就踩个这么大的坑,也是醉了。

问题:

使用java连接Linux服务器上的redis失败

代码:

packagecom.redis.study.test;importorg.junit.Test;importredis.clients.jedis.Jedis;public classRedisTest {

@Testpublic voidtest() {

Jedis jedis= new Jedis("192.168.80.128", 6379);

System.out.println(jedis.get("username"));

}

}

错误日志:

redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.80.128:6379at 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:117)

at redis.clients.jedis.Jedis.get(Jedis.java:152)

at com.redis.study.test.RedisTest.test(RedisTest.java:14)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: java.net.SocketTimeoutException: connect timed out

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)

...27 more

思考:

1、ip地址错误

(1)、 进入服务器查看Linux服务器地址

3a610ba998fa4d433d17631602f49c08.png

唉,好像有两个ip地址 试试另一个

3b246d2ca86ad6db5ed31373e9a6583a.png

吐血。还是不行。

(2)、会不会是本机没法连接服务器,ping一下试试

fb38160bcd64219d72faba1774ade9f9.png

能ping的通啊

(3)、试试telnet

89e3281001638bb0d8bd5b836bafd4af.png

看样子是端口没开放

进入服务器尝试开放该端口,但是发现我连Iptables都没有 只要一个iptabkeconfig

db28ea765980747c552821de2df91261.png

所以应该不是防火墙的问题

(3)、查到说是redis的还需要开放允许远程连接

进入redis的安装目录修改redis.conf

找到bind 将127.0,0.1注释

保存退出

cc7e037dc09d1055a3a09bae37bcf8c3.png

重启redis试试

1b3777e48e31c1ac579c76ea48d1951f.png

连接的问题到此解决但是又出现了新问题

190710742ef9d0f5d37646b9c33ff8b4.png

日志:

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running inprotected 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 forthe 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.getBinaryBulkReply(Connection.java:260)

at redis.clients.jedis.Connection.getBulkReply(Connection.java:249)

at redis.clients.jedis.Jedis.get(Jedis.java:153)

at com.redis.study.test.RedisTest.test(RedisTest.java:12)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code-1

大概意思是说开启了protected mode 也就是保护模式??

还是找到配置文件,打开,查看 发现真的有这么一个mode把它改成no试试

d6acd18f3904fb6a12f7c866189bfe9b.png

重启redis再试试

8eeff21e4260774e2dd05884939a579b.png

终于出结果了

总结:

1、判断是否是服务器地址不对

2、判断端口是否能访问

3、关闭redis的只允许本机访问的限制

4、关闭保护模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值