1. 程式人生 > >遠端連線redis問題集合

遠端連線redis問題集合

在虛擬機器linux上安裝好redis後,使用本地機器進行訪問redis

1:訪問出現Could not connect to Redis at (ip地址):6379: Connection refused

最有可能的原因就是沒有關閉防火牆, 1) 重啟後生效
        開啟: chkconfig iptables on
        關閉: chkconfig iptables off
        2) 即時生效,重啟後失效
        開啟: service iptables start
        關閉: service iptables stop

2:啟動出現以下異常

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.
解決:設定redis.conf引數protected-mode no

3:啟動出現WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

關於vm.overcommit_memory引數的詳解
  0, 表示核心將檢查是否有足夠的可用記憶體供應用程序使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,      並把錯誤返回給應用程序。
  1, 表示核心允許分配所有的實體記憶體,而不管當前的記憶體狀態如何。
  2, 表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體
  編輯/etc/sysctl.conf ,改vm.overcommit_memory=1,然後sysctl -p 使配置檔案生效

4:啟動出現 The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

先看一下redis.conf中關於tcp-backlog引數的解釋

此引數確定了TCP連線中已完成佇列(完成三次握手之後)的長度,當然此值必須不大於Linux系統定義的/proc/sys/net/core/somaxconn值,

預設是511,而Linux的預設引數值是128。當系統併發量大並且客戶端速度緩慢的時候,可以將這二個引數一起參考設定。該核心引數預設值一般是128

對於負載很大的服務程式來說大大的不夠。一般會將它修改為2048或者更大。在/etc/sysctl.conf中新增:net.core.somaxconn = 2048,然後在終端中執行sysctl -p

解決方法註釋中已給出

5:訪問又出現Could not connect to Redis at (ip地址):6379: Connection refused

redis.conf中關於bind引數的解釋 註釋掉本機,區域網內的所有計算機都能訪問。
band localhost   只能本機訪問,區域網內計算機不能訪問。
bind  區域網IP    只能區域網內IP的機器訪問, 本地localhost都無法訪問。
解決辦法就是註釋掉bind配置

6:訪問出現Creating Server TCP listening socket *:6379: unable to bind socket

1. 將IPv6的網絡卡進行關閉(具體方法進行百度,這裡不進行詳細敘述) 
2. 新增在redis.conf中新增 bind 0.0.0.0

注:該配置與解決方法只適用於執行環境下,生產環境下未經過測試