raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)

redis.exceptions.ConnectionError: Connection closed by server. 报错完美解决

问题:刚刚安装的linux redis,使用 .bin/redis-server 启动后 使用任何位置的客户端 redis-cli  (linux本机或者win本机客户端都可以链接) ,但是使用 python代码确不能链接 

redis.exceptions.ConnectionError: Connection closed by server._java

import redis

kwargs = {
    'host': '172.168.137.2',
    'port': 6379,
    'decode_responses': True,
    'retry_on_timeout': 3,
    'max_connections': 1024  # 默认2^31
}

pool = redis.ConnectionPool(**kwargs)
r = redis.Redis(connection_pool=pool)

r.set('book', '西游记')
print(r.get('book'))  
Traceback (most recent call last):
  File "D:\pyProject\slowly\orgent\redis_connect.py", line 14, in <module>
    r.set('book', '西游记')
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\commands\core.py", line 2341, in set
    return self.execute_command("SET", *pieces, **options)
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\client.py", line 533, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\connection.py", line 1086, in get_connection
    connection.connect()
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\connection.py", line 276, in connect
    self.on_connect()
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\connection.py", line 386, in on_connect
    self.read_response()
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\connection.py", line 500, in read_response
    response = self._parser.read_response(disable_decoding=disable_decoding)
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\_parsers\resp2.py", line 15, in read_response
    result = self._read_response(disable_decoding=disable_decoding)
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\_parsers\resp2.py", line 25, in _read_response
    raw = self._buffer.readline()
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\_parsers\socket.py", line 115, in readline
    self._read_from_socket()
  File "D:\pyProject\slowly\venv\lib\site-packages\redis\_parsers\socket.py", line 68, in _read_from_socket
    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.

尝试解决:

当使用 java代码链接时:发现答案

Exception in thread "main" 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.

 

import redis.clients.jedis.Jedis;

public class LocalRedisConnect {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("172.168.137.2");
        jedis.set("a","name");

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

    }
}
Exception in thread "main" 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:128)
	at redis.clients.jedis.Protocol.process(Protocol.java:162)
	at redis.clients.jedis.Protocol.read(Protocol.java:216)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:434)
	at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:353)
	at redis.clients.jedis.Connection.connect(Connection.java:230)
	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:90)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:132)
	at redis.clients.jedis.BinaryClient.set(BinaryClient.java:107)
	at redis.clients.jedis.Client.set(Client.java:41)
	at redis.clients.jedis.Jedis.set(Jedis.java:83)
	at test.LocalRedisConnect.main(LocalRedisConnect.java:8)


简单test方法修改redis 启动命令  ./bin/redis-server  --protected-mode no

代码运行正常

redis.exceptions.ConnectionError: Connection closed by server._java_02