跑路哥 httpd 进程迷之阻塞,strace + lsof 快速定位问题

梦康 2021-06-16 00:00:00 631

跑路哥的祖传的迷之服务器,遇到了新问题:所有的 httpd 进程都进入睡眠
为什么所有的 httpd 进程都选择睡眠,我用这个命令帮跑路哥退了回家的车票

经过之前的沟通,跑路哥使用strace命令发现,进程阻塞在 read 13 fd

$ strace -p 28027
strace: Process 28027 attached
read(13,

虽然他说数据连接是没问题的,我还是不信呢,我让跑路哥给开了新的权限,我随便找个进程看下

$ sudo ls -al /proc/28025/fd/
总用量 0
dr-x------ 2 root   root    0 6月  14 19:33 .
dr-xr-xr-x 9 apache apache  0 6月  14 16:38 ..
lr-x------ 1 root   root   64 6月  14 19:33 0 -> /dev/null
lrwx------ 1 root   root   64 6月  14 19:33 1 -> socket:[212360475]
l-wx------ 1 root   root   64 6月  14 19:33 10 -> /var/log/httpd/access_log
lrwx------ 1 root   root   64 6月  14 19:33 11 -> anon_inode:[eventpoll]
lrwx------ 1 root   root   64 6月  14 19:33 12 -> socket:[212419859]
lrwx------ 1 root   root   64 6月  14 19:33 13 -> socket:[212419866]
l-wx------ 1 root   root   64 6月  14 19:33 2 -> /var/log/httpd/error_log
lrwx------ 1 root   root   64 6月  14 19:33 3 -> socket:[212358866]
lr-x------ 1 root   root   64 6月  14 19:33 4 -> pipe:[212358886]
l-wx------ 1 root   root   64 6月  14 19:33 5 -> pipe:[212358886]
l-wx------ 1 root   root   64 6月  14 19:33 6 -> /var/log/httpd/dev_error_log
l-wx------ 1 root   root   64 6月  14 19:33 7 -> /var/log/httpd/access_log
l-wx------ 1 root   root   64 6月  14 19:33 8 -> /var/log/httpd/dev_access_log
l-wx------ 1 root   root   64 6月  14 19:33 9 -> /var/log/httpd/access_log

可以看到13这个文件描述符是一个socket连接
然后根据lsof查看进程打开的文件链接,取交集,发现还是数据库连接

$ sudo lsof -p 28025|grep 212419866
httpd   28025 apache   13u     IPv4          212419866       0t0       TCP localhost:38830->localhost:mysql (ESTABLISHED)

然后通过ps找到mysql进程错误日志路径,tail 发现是有一些表坏了。

跑路哥之所以能做一些全局性的查询,是整个数据库是好的,正好只有个别表坏了。
跑路哥是出了名的数据库专家,三下五除二,就通过

mysqlcheck xxx_db_name xx_table_name  -uroot -p

把数据库就修复了,我请教了下跑路哥啥情况会出现这种表坏的情况,我还没遇到过,跑路哥告诉我服务器非正常关机,或者突然干掉mysql进程,这两种情况坏表的概率大些。推荐大家千万别自己搭建数据库服务了,使用云服务数据库

数据库恢复之后,httpd进程也都恢复了忙碌的工作。跑路哥打开了12306默默退掉了回家的站票。