關於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. 解決了這個問題就可以進行各種有趣的測試了哈哈

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章