在数据库运行过程中,我们经常会遇到mysql服务器cpu占用率100%的情况,下面就是遇到这种情况时的排查方法和具体步骤。
1、通过top查看pid
找到对应的mysql进程和其对应的PID,根据cpu占用率排序后的结果
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16965 mysql 20 0 32.0g 2.8g 16592 S 1566 8.8 49:40.99 mysqld
比如这里看到的PID是16965
2、查看mysql进程
查看mysql的具体进程,找到和上一步top命令中查到的pid对应的信息
ps -ef | grep -i mysql
显示的信息如下
avahi 5451 1 0 2020 ? 00:00:01 avahi-daemon: running [zhyjmysql1.local]
mysql 16965 1 99 21:28 ? 01:05:09 /usr/sbin/mysqld
root 17362 10112 0 21:33 pts/1 00:00:00 grep --color=auto -i mysql
3、通过top -p mysqlpid -H 可以找到thread_os_id
top -p 16965 -H
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
17053 mysql 20 0 32.0g 2.8g 16668 R 62.7 9.0 4:47.45 mysqld
17032 mysql 20 0 32.0g 2.8g 16668 R 61.7 9.0 5:03.16 mysqld
17028 mysql 20 0 32.0g 2.8g 16668 R 61.3 9.0 4:38.47 mysqld
17033 mysql 20 0 32.0g 2.8g 16668 R 61.0 9.0 4:56.53 mysqld
17054 mysql 20 0 32.0g 2.8g 16668 R 59.0 9.0 5:17.14 mysqld
17064 mysql 20 0 32.0g 2.8g 16668 R 59.0 9.0 4:57.18 mysqld
17057 mysql 20 0 32.0g 2.8g 16668 R 57.0 9.0 4:47.68 mysqld
17065 mysql 20 0 32.0g 2.8g 16668 R 56.7 9.0 5:00.24 mysqld
17041 mysql 20 0 32.0g 2.8g 16668 R 56.3 9.0 4:38.04 mysqld
17034 mysql 20 0 32.0g 2.8g 16668 R 46.7 9.0 5:05.70 mysqld
17039 mysql 20 0 32.0g 2.8g 16668 R 46.7 9.0 4:48.71 mysqld
17052 mysql 20 0 32.0g 2.8g 16668 R 46.7 9.0 4:49.51 mysqld
17048 mysql 20 0 32.0g 2.8g 16668 R 42.7 9.0 5:06.26 mysqld
17035 mysql 20 0 32.0g 2.8g 16668 R 40.7 9.0 4:21.71 mysqld
17061 mysql 20 0 32.0g 2.8g 16668 R 40.0 9.0 4:40.52 mysqld
17045 mysql 20 0 32.0g 2.8g 16668 R 39.3 9.0 4:47.58 mysqld
17037 mysql 20 0 32.0g 2.8g 16668 R 39.0 9.0 4:38.30 mysqld
17047 mysql 20 0 32.0g 2.8g 16668 R 39.0 9.0 4:48.43 mysqld
17060 mysql 20 0 32.0g 2.8g 16668 R 38.0 9.0 4:44.66 mysqld
17069 mysql 20 0 32.0g 2.8g 16668 R 35.3 9.0 2:30.98 mysqld
17031 mysql 20 0 32.0g 2.8g 16668 R 33.7 9.0 5:04.40 mysqld
17050 mysql 20 0 32.0g 2.8g 16668 R 33.0 9.0 4:48.70 mysqld
17036 mysql 20 0 32.0g 2.8g 16668 R 32.3 9.0 4:45.65 mysqld
17038 mysql 20 0 32.0g 2.8g 16668 R 31.7 9.0 4:53.28 mysqld
17042 mysql 20 0 32.0g 2.8g 16668 R 31.3 9.0 4:54.65 mysqld
17051 mysql 20 0 32.0g 2.8g 16668 R 31.0 9.0 4:52.69 mysqld
17056 mysql 20 0 32.0g 2.8g 16668 R 29.7 9.0 4:43.77 mysqld
17030 mysql 20 0 32.0g 2.8g 16668 R 28.3 9.0 4:37.87 mysqld
17029 mysql 20 0 32.0g 2.8g 16668 R 28.0 9.0 4:49.72 mysqld
17026 mysql 20 0 32.0g 2.8g 16668 R 27.3 9.0 4:55.32 mysqld
17040 mysql 20 0 32.0g 2.8g 16668 R 27.3 9.0 4:57.71 mysqld
17044 mysql 20 0 32.0g 2.8g 16668 R 27.3 9.0 4:50.06 mysqld
4、可以看到占用最高的是PID=17053
4.1 通过 performance_schema.threads.thread_os_id=17053 可以找到对应的thread_id,processlist_id ;
mysql> select thread_id,name ,PROCESSLIST_ID,THREAD_OS_ID from performance_schema.threads where thread_os_id = 17053;
+-----------+---------------------------+----------------+--------------+
| thread_id | name | PROCESSLIST_ID | THREAD_OS_ID |
+-----------+---------------------------+----------------+--------------+
| 143 | thread/sql/one_connection | 97 | 17053 |
+-----------+---------------------------+----------------+--------------+
1 row in set (0.00 sec)
4.2 通过performance_schema.events_statements_current.thread_id = 143 可以找到当前占用cpu的SQL:
select DIGEST_TEXT from performance_schema.events_statements_current where thread_id = 143 ;
+---------------------------------------------------------------------+
| DIGEST_TEXT
SELECT * FROM t xxx ……
有时候发现DIGEST_TEXT 内容显示不全,可以通过以下方法处理:
DIGEST_TEXT 的长度由变量 max_digest_length 控制:
show variables like 'max_digest_length' ;
然后,拿到对应的sql后,可以根据要求对sql进行优化处理。