使用到mongo,redis等数据库的时候,往往会遇到这样一个坑。某mongo跑着跑着突然没了,某redis启动先给个warning,仔细一看log说最大文件打开数不够..

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

mongo 官方也给教程说你修改下 ulimit 就可以。

但实际上,若该用户是普通用户

$ ulimit -n 65535
-bash: ulimit: open files: cannot modify limit: Operation not permitted

若你用 root 当然可以, 但你真的想用 root 跑 db 么?

正确的打开方式其实应该是修改 limit.

打开 /etc/security/limits.conf, 添加内容如下:

your_user_name           soft    nofile          65535
your_user_name           hard    nofile          65535

或者若想要一加一个组

@your_group_name           soft    nofile          65535
@your_group_name           hard    nofile          65535

然后保存。

检查下 /etc/pam.d/sudo 或者 /etc/pam.d/common-session-noninteractive ,若没有发现如下这行信息,请自行加上

session    required     pam_limits.so

然后普通用户需要重新登录即可生效。若没有生效,这时普通用户应该也可以自行 ulimit -n 65535 了。

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *