Jedis鏈接遠程服務的redis一些問題

首先是打開遠程redis,我使用的是虛擬機centOS 6.4java

而後下載jedis的jar包redis

鏈接:https://mvnrepository.com/artifact/redis.clients/jediswindows

測試代碼爲:服務器

import redis.clients.jedis.Jedis;

public class MyJedis {
    public static void main(String[] args) {
        Jedis jedis = new         Jedis("192.168.184.129",6379,10000);
        //Jedis jedis = new Jedis("localhost");
        System.out.println("鏈接成功");
        jedis.set("ylh", "ylh");
        System.out.println(jedis.get("ylh"));
    }
}

出現了以下錯誤:Caused by: java.net.SocketTimeoutException: connect timed outide

緣由是cenOS安裝默認打開防火牆oop

解決方案:測試

1. 在windows上ping Linux虛擬機的ip,測試鏈接

 2. 檢查Linux下的防火牆是否關閉

       用/ect/init.d/iptables status 查看防火牆狀態

       用/etc/init.d/iptables stop  關閉防火牆

 

以後執行代碼出現以下錯誤this

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)

解決方案:spa

vi redis.conf    
1、修改redis服務器的配置文件  

註釋如下綁定的主機地址  
# bind 127.0.0.1  
  
   
2、修改redis服務器的參數配置  
  
修改redis的守護進程爲no ,不啓用  
daemonize no

  
修改redis的保護模式爲no,不啓用  
protected-mode no
相關文章
相關標籤/搜索