Code tutorial for Redis remote connection in Linux, linuxredis

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.