1. 程式人生 > >首次用java連線redis出現的問題

首次用java連線redis出現的問題

1.連線用java連線redis時報如下錯誤,連線超時

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: recv failed
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.hy.redis_maven.App.main(App.java:17)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196)
... 7 more

出現這個問題的原因是redis.conf檔案中bind 127.0.0.1,這導致redis只對映本機

解決辦法,將bind 127.0.0.1註釋就行:如下圖


2.連線redis時碰到的第二個問題,沒有設定密碼,但是傳送了驗證,錯誤如下圖:

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:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
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.hy.redis_maven.App.main(App.java:17)

大概說的就是沒有設定密碼,但傳送了密碼驗證

解決方法:在redis.conf檔案中設定requirepass password,將註釋去掉,如下圖:


熱後在啟動redis伺服器,需要指定配置檔案

src/redis-server   redis.conf

這樣就解決了密碼驗證問題,客戶端程式碼如下:

public class App {
public static void main(String[] args) {


Jedis jedis = new Jedis("192.168.190.120", 6379);
jedis.auth("zhoupengyong");
jedis.set("name", "zhoupengyong");


System.out.println(jedis.get("name"));
}
}