如果我們什麼都不修改,redis啓動時會有三個警告
警告1 : WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128
解釋:上面寫的很清晰,意思是配置 /proc/sys/net/core/somaxconn的值是128,雖然redis.conf中配置的是511,但是linux內核會以無提示的方式將其截斷爲128。在一個高併發的環境下,128是遠遠不夠的,所以我們要改大一些
辦法:將net.core.somaxconn = 1024
添加到/etc/sysctl.conf
中,然後執行sysctl -p
生效配置,如下圖
警告2: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
解釋:overcommit_memory 表內存分配策略,可選值:0、1、2
0, 表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,並把錯誤返回給應用進程。
1, 表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。
2, 表示內核允許分配超過所有物理內存和交換空間總和的內存
辦法:將vm.overcommit_memory = 1
添加到/etc/sysctl.conf
中,然後執行sysctl -p
生效配置,如下圖(由於剛執行了第一個警告的修復,所以兩條命令都有)
警告3:WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled
解釋:THP(透明大頁)的知識請參考這篇博客 https://www.cnblogs.com/kerrycode/p/4670931.html
redis建議我們關掉THP,還給出的具體的操作辦法,注意必須使用root來操作,否則會失敗的
辦法:執行命令 echo never > /sys/kernel/mm/transparent_hugepage/enabled
並把命令 echo never > /sys/kernel/mm/transparent_hugepage/enabled 寫入到 /etc/rc.local 中
執行命令 source /etc/rc.local
全部修復後,啓動redis,已經不報任何警告了,如下圖