Code tutorial for Redis remote connection in Linux, linuxredis
After Redis is installed in Linux, connect using Java. the Java code is as follows:
Package com. wzj. demo. test; import redis. clients. jedis. jedis;/*** Created by wzj on 2018/3/29. */public class RedisJava {public static void main (String [] args) {// connect to the local Redis service Jedis jedis = new Jedis ("192.168.3.45"); System. out. println ("connection successful"); // set redis string data jedis. set ("runoobkey", "www.runoob.com"); // obtain the stored data and output the System. out. println ("redis storage string:" + jedis. get ("runoobkey "));}}
The following error is reported:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection resetat redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202)at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40)at redis.clients.jedis.Protocol.process(Protocol.java:151)at redis.clients.jedis.Protocol.read(Protocol.java:215)at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)at redis.clients.jedis.Jedis.set(Jedis.java:121)at com.wzj.demo.test.RedisJava.main(RedisJava.java:17)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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: java.net.SocketException: Connection resetat java.net.SocketInputStream.read(SocketInputStream.java:209)at java.net.SocketInputStream.read(SocketInputStream.java:141)at java.net.SocketInputStream.read(SocketInputStream.java:127)at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196)... 12 more
Use Telnet? 192.168.3.45 6379, the port is disconnected.
The reason for searching and analyzing on the internet is that Redis only allows local connections by default and does not allow connections from other machines. You need to make the following changes:
(1) modify the redis. conf file and comment out the bind 127.0.0.1: 1 line.
(2) modify the redis. conf file and set protected-mode to no.
(3) at startup, you must specify the redis. conf file and execute the./src/redis-server redis. conf command.
After setting, restart the test and find that the Java code can be connected normally.