redis使用過程(連接遠端redis服務器)問題彙總

今天在使用redis做服務器時緩存時,逐步調錯的過程連續出現了以下幾個問題:

1、jedisPool.getResource() 該句出現連接失敗錯誤

     使用ping命令( System.out.println(jedis.ping()) )測試連接時,報如下2和3的錯。

2、redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.152.129:6379

3、Redis is running in protected mode because protected mode is enabled, no bind address was specified

接下來我們分析下以上問題的解決辦法。

首先,我們檢查一下虛擬機防火牆是否關閉:service iptables status

           關閉防火牆:service iptables stop

           關於防火牆的打開關閉可以參考:https://www.cnblogs.com/zhangmingcheng/p/6048043.html

 然後,打開6397端口:iptables -A INPUT -p tcp --dport 8001 -j ACCEPT

然後,我們分析第2、3個錯誤

 1)該錯誤的意思是,redis 連接時只能通過本地localhost (127.0.0.1)這個來鏈接,而不能用網絡ip(192.168..)這個鏈接,如果用網絡ip 鏈接會報以下的錯誤:

(error) 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 lookback 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 --portected-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.

2)我們需要修改配置文件../redis.conf  (路徑:/redis-5.0.3/redis.conf) 

①打開配置文件把下面對應的註釋掉:

# bind 127.0.0.1

②Redis默認不是以守護進程的方式運行,可以通過該配置項修改,使用yes啓用守護進程,設置爲no:

daemonize no

③關閉保護模式:

protected-mode no

④最後就是重啓服務,讓配置文件生效:

./redis-server redis.conf (指定配置文件啓動)

問題:如果端口占用,證明原來的redis服務沒有關閉,先關閉,再重啓即可:

即: ./redis-server shutdown

        ./redis-server redis.conf 

本文借鑑:https://blog.csdn.net/Agly_Clarlie/article/details/52251746

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