最近兼职的公司的hk的服务器突然报警内存和cpu已经负载使用过高!第一感觉就是mysql死锁了!
top命令查看后发现负载的显示load average: 10.01, 17.92, 26.23。这可以说已经很恐怖了。网站基本打开要5秒以上,完全影响了客户的体验。top命令的同时,联系开发,开发只使用了show full processlist查看是否死锁命令。自己使用了Rkhunter扫描了服务器的漏洞,没有发现任何问题!
其实问题很明显是mysql的问题了cpu这么高明眼就看的出是死锁或者慢查询了!
mysqlcpu使用过高图.png
随后自己登录服务器,使用了show full processlist;
没有看到time时间过长,也没有看到LOCK关键字

 mysql> show full processlist;
    +------------+------+-----------+-----------------+---------+------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
    | Id         | User | Host      | db              | Command | Time | State               | Info                                                                                                                                            |
    +------------+------+-----------+-----------------+---------+------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
    | 10 | root | ip | NULL            | Query   |    0 | init                | show full processlist                                                                                                                           |
    | 10 | root | ip| aiw | Query   |    0 | Crx | selc limit 0,40 |                    |
    +------------+------+-----------+-----------------+---------+------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
    3 rows in set (0.00 sec)

随后排除mysql死锁问题!只能从日志里面排查问题了!
mysql配置慢查询日志,value是on就是开启off就是关闭!我的是开启的所以是on

mysql> show variables  like '%slow_query_log%';
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| slow_query_log      | ON                                       |
| slow_query_log_file | /usr/local/mysql/var/localhosts-slow.log |
+---------------------+------------------------------------------+
2 rows in set (0.00 sec)

如果要修改。输入下面命令即可!

set global slow_query_log=1;

然后根据slow_query_log_file的值就可以看到慢日志查询的路径!

cat /usr/local/mysql/var/localhosts-slow.log
# Query_time: 30.769761  Lock_time: 0.000129 Rows_sent: 17  Rows_examined: 13613
# Query_time: 24.408612  Lock_time: 0.000092 Rows_sent: 0  Rows_examined: 124539
# Query_time: 24.179152  Lock_time: 0.000087 Rows_sent: 0  Rows_examined: 124539
# Query_time: 31.709510  Lock_time: 0.000134 Rows_sent: 0  Rows_examined: 124539
# Query_time: 30.108347  Lock_time: 0.000065 Rows_sent: 15  Rows_examined: 123307

明显可以看到一些慢查询的语句和查询时间,一条数据查询20多秒!(语句就不暴露出来了,安全问题)
随后联系开发告诉开发有问题的语句,开发在原有的sql语句上新增索引。
top命令后load average: 0.42, 1.12, 3.34马上达到了服务器的正常范围!
我这次排查mysql慢查询导致大量占用服务器cpu负载过高的过程基本就是这个流程!

Last modification:July 30, 2019
如果觉得我的文章对你有用,请随意赞赏