JAVA访问REDIS
1. 先决条件是telnet可以ping通到linux;
telnet不同的条件 , 可以到控制面板进行设置 , 关闭防火墙 , 最重要是关闭linux的防火墙 , 看代码
CenterOS 下
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
【JAVA访问REDIS】保存修改/etc/rc.d/init.d/iptables save成功之后 , 输入ping会出来PONG前面的命令不对 , 不用管哦 。 代码 Redis的value存储中文后 , get之后显示16进制的字符串"\xe4\xb8\xad\xe5\x9b\xbd" , 如何解决?127.0.0.1:6379> set China 中国OK127.0.0.1:6379> get China"\xe4\xb8\xad\xe5\x9b\xbd" 127.0.0.1:6379> exit启动redis-cli时 , 在其后面加上--raw即可 , 汉字即可显示正常 。 [root@localhost redis]# ./bin/redis-cli --raw 加入数据 , 清空等代码 问题汇总:2、Connection refused 拒绝访问Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connectat 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:121)at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)at practice.RedisJava.main(RedisJava.java:13)Caused by: java.net.ConnectException: Connection refused: connectat 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)... 6 more修改redis.conf配置文件 , 使用vim命令打开 , 找到bind那行修改后 , wq保存退出 , 重启redis-server 。 redis.conf编辑后编辑后如上图 , 用#号注释 。 3、DENIED Redis is running in protected mode (保护模式)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:117)at redis.clients.jedis.Protocol.process(Protocol.java:142)at redis.clients.jedis.Protocol.read(Protocol.java:196)at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:207)at redis.clients.jedis.Connection.getBulkReply(Connection.java:196)at redis.clients.jedis.Jedis.get(Jedis.java:98)at com.zyao89.jedis.JedisTest.test1(JedisTest.java:18)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)报错信息很长 , 但是主要是说redis开启了protected mode , 这也是Redis3.2加入的新特性 , 开启保护模式的redis只允许本机登录 , 同样设置在配置文件redis.conf中 , 如图redis.conf编辑后这里原来是yes代表开启了保护模式 , 后面可以填密码也可以填no代表关闭 , 我们这里选择关闭保护模式 , wq保存退出后再重启redis-server 。
推荐阅读
- 计算机专业大一下学期,该选择学习Java还是Python
- 未来想进入AI领域,该学习Python还是Java大数据开发
- 学习大数据是否需要学习JavaEE
- T-Mobile证实黑客访问了用户电话记录
- 从事Java开发时发现基础差,是否应该选择辞职自学一段时间
- 2021年Java和Python的应用趋势会有什么变化?
- 普通大学计算机专业的本科生,该选择主攻前端还是Java
- Java语言会不会随着容器的兴起而衰落
- 大一有考研计算机专业的打算,该学习C++还是Java
- 微软统一Edge工具栏体验:方便用户快速访问收藏夹、历史和集锦