为什么redis无法设置最大打开文件

时间:2016-04-27 04:28:05

标签: redis

1167:M 26 Apr 13:00:34.666 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
1167:M 26 Apr 13:00:34.667 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
1167:M 26 Apr 13:00:34.667 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
1167:M 26 Apr 13:00:34.685 # Creating Server TCP listening socket 192.34.62.56​​:6379: Name or service not known
1135:M 26 Apr 20:34:24.308 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
1135:M 26 Apr 20:34:24.309 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
1135:M 26 Apr 20:34:24.309 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
1135:M 26 Apr 20:34:24.330 # Creating Server TCP listening socket 192.34.62.56​​:6379: Name or service not known

3 个答案:

答案 0 :(得分:24)

嗯,这篇文章有点晚了,但是因为我花了很多时间(整晚)在ubuntu 16.04上配置一个新的redis服务器3.0.6。我想我应该写下我是怎么做的,这样其他人就不必浪费时间......

对于新安装的redis服务器,您可能会在redis日志文件中看到以下问题: /var/log/redis/redis-server.log

最大打开文件

3917:M 16 Sep 21:59:47.834 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
3917:M 16 Sep 21:59:47.834 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
3917:M 16 Sep 21:59:47.834 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.

我看过很多帖子告诉你修改

/etc/security/limits.conf
redis soft nofile 10000
redis hard nofile 10000

/etc/sysctl.conf
fs.file-max = 100000

这可能在ubuntu 14.04中有效,但它肯定不适用于ubuntu 16.04。我想这与从upstart更改为systemd有关,但我不是linux内核的专家! / p>

要解决此问题,您必须使用 systemd 方式

/etc/systemd/system/redis.service
[Service]
...
User=redis
Group=redis
# should be fine as long as you add it under [Service] block
LimitNOFILE=65536
...

然后您必须守护进程重新加载并重新启动服务

sudo systemctl daemon-reload
sudo systemctl restart redis.service

要检查它是否有效,请尝试cat proc limits

cat /run/redis/redis-server.pid
cat /proc/PID/limits

你会看到

Max open files            65536                65536                files     
Max locked memory         65536                65536                bytes   

在此阶段,解决了最大打开文件。

套接字最大连接数

2222:M 16 Sep 20:38:44.637 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

内存过量使用

2222:M 16 Sep 20:38:44.637 # Server started, Redis version 3.0.6
2222:M 16 Sep 20:38:44.637 # 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.

由于这两者是相关的,我们将立即解决。

sudo vi /etc/sysctl.conf

# Add at the bottom of file
vm.overcommit_memory = 1
net.core.somaxconn=1024

现在要使这些配置正常工作,您需要重新加载配置

sudo sysctl -p

透明的大页面

1565:M 16 Sep 22:48:00.993 # 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.

要永久解决此问题,请按照日志的建议操作,然后修改rc.local

sudo vi /etc/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

这需要您重新启动,备份您的数据或在您实际执行之前执行任何操作!!

sudo reboot

现在再次检查redis日志,你应该有一台没有任何错误或警告的redis服务器。

答案 1 :(得分:5)

Redis永远不会更改最大打开文件。

这是一个操作系统配置,也可以基于每个用户进行配置。该错误具有描述性,并告诉您:"增加' ulimit -n'"

您可以参考此博客文章,了解如何增加最大打开文件描述符: http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/

答案 2 :(得分:0)

您只需在控制台中使用此命令:

sudo ulimit -n 65535