Ulimits不生效导致数据库启动失败和相关设置说明

1. 问题描述

在某客户的生产环境GreatSQL数据库紧急重启过程中,发现启动失败

-- 正常启动中
2022-07-16T09:30:27.428609+08:000[Note][MY-010252][Server] Server hostname (bind-address):'127.0.0.1'; port:33062
2022-07-16T09:30:27.429134+08:000[Note][MY-010264][Server]-'127.0.0.1' resolves to '127.0.0.1';
2022-07-16T09:30:27.429792+08:000[Note][MY-010251][Server] Server socket created on IP:'127.0.0.1'.
2022-07-16T09:30:27.430296+08:000[Note][MY-010252][Server] Server hostname (bind-address):'*'; port:3306
2022-07-16T09:30:27.430816+08:000[Note][MY-010254][Server] IPv6 isnot available.
2022-07-16T09:30:27.431308+08:000[Note][MY-010264][Server]-'0.0.0.0' resolves to '0.0.0.0';
2022-07-16T09:30:27.431991+08:000[ERROR][MY-010250][Server] Failed to create a socket for IPv4 '0.0.0.0': errno:24.
2022-07-16T09:30:27.432466+08:000[ERROR][MY-010255][Server] Can't create IP socket: Too many open files
-- 报错Can't create IP socket: Too many open files
2022-07-16T09:30:27.433711+08:000[ERROR][MY-010119][Server] Aborting
2022-07-16T09:30:27.435690+08:000[Note][MY-012330][InnoDB] FTS optimize thread exiting.
2022-07-16T09:30:28.164281+08:000[Note][MY-010120][Server] Binlog end
2022-07-16T09:30:28.165714+08:000[Note][MY-000000][Server] Plugin GreatSQL reported:'Gdb_job_thread stopped!'
2022-07-16T09:30:28.165960+08:000[Note][MY-000000][Server] Plugin GreatSQL reported:'Job manager local thread stopped!'
-- 接下来开始走shutdown流程

上面的错误日志非常清晰的指向了 open files 相关设置,于是查看 ulimit 信息

[GreatSQL@GDB02-DB01 ~]$ ulimit -a
...
open files (-n)1024
...
[GreatSQL@GDB02-DB01 ~]$

但是运维人员确认/etc/security/limits.conf中设置的限制用户使用的最大文件数是正常的65535

[GreatSQL@GDB02-DB01 ~]$ tail -10/etc/security/limits.conf
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4

* soft nofile 65535
* hard nofile 65535


# End of file
[GreatSQL@GDB02-DB01 ~]$

尽管堡垒机登录GreatSQL用户不正常,但是由root用户再切换回GreatSQL普通用户后,open files就变回正常的65535

-- 堡垒机直接登录GreatSQL用户,有open files未修改成功的提示信息
-bash: ulimit: open files: cannot modify limit: Operation not permitted
-- 此时的open files配置确实没生效
[GreatSQL@GDB02-DB01 ~]$ ulimit -a |grep 'open files'
open files (-n)1024
-- su切换到root,root的open files是正常的
[GreatSQL@GDB02-DB01 ~]$ sudo su - root
Last login: Tue Jul 1911:40:45 CST 2022on pts/5
[root@GDB02-DB01 ~]# ulimit -a |grep 'open files'
open files (-n)65535
-- su切换到GreatSQL,open files也是正常的
[root@GDB02-DB01 ~]# su - GreatSQL
Last login: Tue Jul 2614:23:56 CST 2022from XXXXXX on pts/8
[GreatSQL@GDB02-DB01 ~]$ ulimit -a |grep 'open files'
open files (-n)65535
[GreatSQL@GDB02-DB01 ~]$

为了尽快恢复业务,先建议运维人员由root用户切换回GreatSQL普通用户后再启动数据库,此时启动成功,业务和相关监控 (监控里限制必须由GreatSQL用户启动数据库) 恢复正常。

2. ulimits不生效的问题分析

在同批次备机上进行问题复现分析时,运维人员发现了更多的信息。

(1)堡垒机直接登录GreatSQL普通用户执行ulimit命令报错

[GreatSQL@GDB02-DB02 ~]$ ulimit -n 1026
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[GreatSQL@GDB02-DB02 ~]$ ulimit -Hn
1024-- 可以发现这里使用的硬件资源限制1024

(2)堡垒机直接登录GreatSQL用户,也有相关报错信息(之前被忽略了)

Connecting to XXXXXX...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Tue Jul 2614:32:322022from XXXXXX

Prepare to login to the target device, Please wait a second.

Last login: Tue Jul 2614:31:212022from XXXXXX
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[GreatSQL@GDB02-DB01 ~]$ ulimit -a

根据上面信息的堡垒机ssh登录ulimits异常,结合su到同样用户ulimits正常,于是检查了下ssh配置文件,发现UsePAM为默认的no

cat /etc/ssh/sshd_config
.......
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# UsePAM no
......

至此原因比较清晰了,由于/etc/security/limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,而没有使用 PAM 模块场景下,自然也就没有读取到 /etc/security/limits.conf 的内容。

而 su 进行用户切换时使用的是终端TTY登陆(默认使用PAM模块),导致堡垒机的GreatSQL切换到root、再su GreatSQL后limits相关设置正常。

3. 解决方法

(1)修改ssh配置文件,UsePAM=yes

vi /etc/ssh/sshd_config
.......
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
......

PS:经过与局方确认,局方的机器规范中也是推荐UsePAM=yes,因此本次问题的原因应该是这批机器在投产时没有检查相关配置项导致。

(2)重启sshd服务

[root@GDB02-DB01 ~]# systemctl restart sshd
[root@GDB02-DB01 ~]# systemctl status sshd
● sshd.service- SYSV: OpenSSH server daemon
Loaded: loaded (/etc/rc.d/init.d/sshd; bad; vendor preset: enabled)
Active: active (running) since Tue 2022-07-2610:28:30 CST;2s ago
Docs: man:systemd-sysv-generator(8)
Process:46808 ExecStop=/etc/rc.d/init.d/sshd stop (code=exited, status=0/SUCCESS)
Process:46815 ExecStart=/etc/rc.d/init.d/sshd start (code=exited, status=0/SUCCESS)
Main PID:46823(sshd)
Tasks:14
Memory:85.8M
......
[root@GDB02-DB01 ~]#

(3)验证:堡垒机通过GreatSQL应用用户连接后不再报错,open files也是设置的65535

Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Tue Jul 2610:28:112022from XXXXXX

Prepare to login to the target device, Please wait a second.

Last login: Tue Jul 2610:28:172022from XXXXXX
[GreatSQL@GDB02-DB01 ~]$ ulimit -a
...
open files (-n)65535
...
[GreatSQL@GDB02-DB01 ~]$

4. limits.conf配置文件相关说明

limits.conf限制的是每个用户可以使用的最大文件数、最大线程、最大内存等资源配置,相关的设置如下所示:

* soft nofile 655350  #任何用户可以打开的最大的文件描述符数量,默认1024,这里的数值会限制tcp连接
* hard nofile 655350
* soft nproc 655350 #任何用户可以打开的最大进程数
* hard nproc 650000

@student hard nofile 65535
@student soft nofile 4096
@student hard nproc 50 #学生组中的任何人不能拥有超过50个进程,并且会在拥有30个进程时发出警告
@student soft nproc 30

(1)查看每个用户创建的进程数

$ ps h -Led -o user | sort | uniq -c | sort -n
1 chrony
1 dbus
2 postfix
7 polkitd
129 root
1326 GreatSQL

(2)系统最大打开文件描述符数

-- 查看
$ cat /proc/sys/fs/file-max
6553600
-- 设置
$ vim /etc/sysctl.conf
fs.file-max =6553600

(3)进程最大打开文件句柄数

-- 查看soft limit,ulimit -n默认查看的是soft limit
$ ulimit -n
65535
-- 查看hard limit
$ ulimit -Hn
65535

-- 临时设置
-- 通过ulimit -Sn设置最大打开文件描述符数的soft limit,注意soft limit必须小于hard limit
$ ulimit -Sn 65535
-- 同时设置soft limit和hard limit。对于非root用户只能设置比原来小的hard limit。
$ ulimit -n 65535

永久设置
#root权限下,在/etc/security/limits.conf中添加如下两行,表示所有用户最大打开文件描述符数的soft limit为102400,hard limit为104800。重启生效
* soft nofile 655350
* hard nofile 655350
复制
注意:设置nofile的hard limit还有一点要注意的就是hard limit不能大于/proc/sys/fs/nr_open,假如hard limit大于nr_open,注销后将无法正常登录。

(4)查看当前系统使用的打开文件句柄数

$ cat /proc/sys/fs/file-nr
56640186405
其中第一个数表示当前系统已分配使用的打开文件描述符数,
第二个数为分配后已释放的(目前已不再使用),
第三个数等于file-max。

(5)设置nofile的最大值

使用ulimt -n命令进行测试,如果小于系统允许的最大值,设置成功;大于最大值,系统会报错提示。

$ ulimit -n 1100000
-bash: ulimit: open files: cannot modify limit: Operation not permitted
$ ulimit -n 1048576
$ ulimit -n 1048577
-bash: ulimit: open files: cannot modify limit: Operation not permitted
$ ulimit -n 1048575
$ ulimit -n 1048576

(6)ulimit -a/n/H/S/u的含义

ulimit -a    显示当前所有的资源限制
ulimit -n 设置进程最大打开文件描述符数
ulimit -H 设置硬件资源限制
ulimit -S 设置软件资源限制述符数
ulimit -u 用户最多可开启的程序数目

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/282573.html<

(0)
运维的头像运维
上一篇2025-05-13 12:55
下一篇 2025-05-13 12:56

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注