1. 程式人生 > >關於Jedis連線Linux上的redis出現 DENIED Redis is running in protected mode問題的解決方案

關於Jedis連線Linux上的redis出現 DENIED Redis is running in protected mode問題的解決方案

  最近有段時間沒有寫部落格了,今天抽出時間寫寫,之前開發的時候redis部署在Linux是其他人搞得,我沒怎麼參與,於是閒著無聊在本地的虛擬機器上安裝了個redis進行測試,沒想到在進行連線時報了下面這麼一個錯,fuck,Linux上的redis還真是麻煩,哪像windows上的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.BinaryJedis.ping(BinaryJedis.java:196)
    at com.example.redis.JedisTest.main(JedisTest.java:24)
 
   
 
   
  經過我英語八級的翻譯,上面一共提供了四種解決方案,但是綜合考慮為了簡單起見只需要執行以下幾步即可:

  
  1.修改redis.conf配置檔案,將繫結的ip地址埠號給註釋見下圖
  這裡寫圖片描述

  2.由於Linux上的redis處於安全保護模式,這就讓你無法從虛擬機器外部去輕鬆建立連線,這裡就有兩種解決方法,一種是在redis.conf中設定保護模式為no,見下圖
  這裡寫圖片描述

  3.另外一種方式是加上安全認證,即redis預設是沒有密碼的可以直接登入,這裡加上密碼,配置見下圖
  這裡寫圖片描述

  4.好吧,此時你以為輕鬆解決問題了,於是再次在Linux啟動redis伺服器後,在外部連線還是報錯,真是too young too native,起初我以為是配置沒有生效,因為明明配置都寫好了,於是殺掉redis的相關程序重啟服務,結果發現還是沒卵用。最後研究發現問題所在:之前啟動redis-server並沒有指定配置檔案,而Linux上的redis比較操蛋的一點就是如果你不指定配置檔案去啟動,那麼你做的修改就沒有用,會讀取預設配置(PS:至於這個預設配置在哪我也不清楚),於是用下面這種方式啟動就可以使修改的配置檔案生效,至於config rewrite命令我測試了並沒有什麼卵用。
  這裡寫圖片描述

  5.再次測試,在windows上進行jedis連線測試,出現下面pong(),惱人的錯誤終於消失了,比起windows版本的redis還真是讓人龜毛啊,如果配置了密碼認證,呼叫Jedis的auth方法輸入密碼即可
   這裡寫圖片描述
  
   6. 解決了這個問題就可以進行各種有趣的