Redis启动之后警告信息解决方案集锦

一些有关Redis相关问题的解决方案。

第1个案例:当我们启动了Redis服务器之后,会看到3个警告,如果没看到,那是很好的,但是我看到了。看到了就不能不管,所以就好好的解决一下。我把这三个警告的信息截图了,大家可以有更直接的感觉。效果截图如下:

Redis启动之后警告信息解决方案集锦

1)、第一个警告信息提示:The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128

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.

这两个问题的解决方法很简单,晚上也有类似的解决方案。

解决:

 //针对这两个问题,都要修改/etc/sysctl.conf文件,在文件末尾加入以下两句:

        net.core.somaxconn= 1024
        vm.overcommit_memory = 1

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.

这个问题不容易解决,我搞了好久才搞定的,所以必须记录下来,否则以后想看看都不容易。

大家想了解跟多情况,可以查看该博客 http://docs.Fedoraproject.org/en-US/Fedora/21/html/System_Administrators_Guide/sec-GRUB_2_over_Serial_Console.html#sec-Configuring_GRUB_2 。

第一种方式:

我们开始第三个问题的解决吧,如果你也是grub2的linux系统,请通过以下步骤关闭大内存页面。

步骤一、 编辑 /etc/default/grub,在GRUB_CMDLINE_LINUX加入选项 transparent_hugepage=never

GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    GRUB_DEFAULT=saved
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    GRUB_CMDLINE_LINUX="rd.lvm.lv=fedora/swap rd.lvm.lv=fedora/root rhgb quiet transparent_hugepage=never"
    GRUB_DISABLE_RECOVERY="true"

步骤二、 重新生成grub配置文件

[root@linux ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

步骤三、 重启你的系统

至此大功告成,如果你使用的是grub,请把选项写入grub.conf文件就好了。

步骤四、查询hugepage状态

[root@linux ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
        always madvise [never]

第二种方式:

[root@linux ~]$ grep Huge /proc/meminfo
    AnonHugePages:        0 kB
    HugePages_Total:      0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:      2048 kB

解决了,还是很高兴的。其实这三个警告信息已经提供了解决办法,有的是临时方法,有的是永久方法,只是第三个警告不是那么直接好解决。

第2个案例:Redis启动Sentinel出现警告的解决

Redis 3.0.7版本启动时出现警告的解决办法

7283:M 12 Mar 12:13:33.749 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7283:M 12 Mar 12:13:33.749 # Server started, Redis version 3.0.7
7283:M 12 Mar 12:13:33.749 # 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.
7283:M 12 Mar 12:13:33.749 * The server is now ready to accept connections on port 6379

第1个警告(WARNING: The TCP backlog setting of 511 ......)解决办法
方法1: 临时设置生效: sysctl -w net.core.somaxconn = 1024
方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行
net.core.somaxconn= 1024
然后执行命令
sysctl -p

补充:
net.core.somaxconn是linux中的一个kernel参数,表示socket监听(listen)的backlog上限。
backlog是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。
而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中。
当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝。
所以说net.core.somaxconn限制了接收新 TCP 连接侦听队列的大小。
对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了。大多数环境这个值建议增加到 1024 或者更多。

第2个警告(WARNING overcommit_memory is set to 0! ......)同样也有两个解决办法
方法1: 临时设置生效: sysctl -w vm.overcommit_memory = 1
方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行
vm.overcommit_memory = 1
然后执行命令
sysctl -p

补充:
overcommit_memory参数说明:
设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。

相关推荐