How to implement remote connection for Redis under Linux

How to implement remote connection for Redis under Linux

After installing Redis on Linux, use Java to connect. 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");
 
    // Get the stored data and output System.out.println("The string stored in redis is: " + jedis.get("runoobkey"));
  }
}

Report the following error:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset
	at 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 reset
	at 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 and find that the port is blocked.

After searching and analyzing online, the reason is: Redis only allows local connections by default, and does not allow connections from other machines. The following modifications need to be made:

(1) Modify the redis.conf file and comment out the line bind 127.0.0.1 ::1.

(2) Modify the redis.conf file and set protected-mode to no

(3) When starting, you need to specify the redis.conf file and execute the ./src/redis-server redis.conf command

After setting up, restart the test and find that the Java code can connect normally.

The above article on how to allow remote connection for Redis under Linux is all I have to share with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to allow remote hosts to access the redis server
  • Detailed explanation of Redis remote login connection
  • Detailed steps to enable remote access in Redis 3.2
  • How to install redis and redis extension on Linux platform

<<:  mysql8.0.11 winx64 manual installation and configuration tutorial

>>:  WeChat applet silent login and maintenance of custom login state detailed explanation

Recommend

A brief discussion on the understanding of TypeScript index signatures

Table of contents 1. What is an index signature? ...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

How to build ssh service based on golang image in docker

The following is the code for building an ssh ser...

How to use React slots

Table of contents need Core Idea Two ways to impl...

How to use CSS to pull down a small image to view a large image and information

Today I will talk about a CSS special effect of h...

Docker image access to local elasticsearch port operation

Using the image service deployed by docker stack,...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

Vue.js performance optimization N tips (worth collecting)

Table of contents Functionalcomponents Childcompo...

Detailed explanation of Docker working mode and principle

As shown in the following figure: When we use vir...