mysql 100%占用的解决

早上客户反应,其网站无法访问,无限转圈

上服务器,查看磁盘空间df -h,内存使用率free -m,网络流量iftop均正常

然后使用top查看时,发现mysql的cpu使用率上升到200%。

解决过程回放

进入mysql

查看正在执行的sql

mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from PROCESSLIST where info is not null;
+-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
| ID  | USER | HOST      | DB                 | COMMAND | TIME | STATE     | INFO                                             |
+-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
| 291 | root | localhost | information_schema | Query   |    0 | executing | select * from PROCESSLIST where info is not null |
+-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

并没有发现有任何的异样,没有出现锁表状况

然后查看tmp_table_size的大小

mysql> show variables like '%table_size%';
+---------------------+-----------+
| Variable_name       | Value     |
+---------------------+-----------+
| max_heap_table_size | 16777216 |
| tmp_table_size      | 16777216 |
+---------------------+-----------+
2 rows in set (0.00 sec)

确认两个值大小均为16M(安装的是mariadb 5.5)

查看free -m还有4G大小的内存,此处显得过小,将其一个值提升到500M,一个值提升至200M

[root@iZbp16s0cap5fnfk6bjvw1Z ~]# grep -v ^# /etc/my.cnf | grep -v ^$
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
tmp_table_size=200M
max_heap_table_size=500M

然后重启mysql

发现top的中mysql的cpu占用率使用已经大大下降,已经恢复至20%左右

事后总结

mysql cpu占用率很高,很有可能是因为查询时死表,或者说大量多表查询,导致cpu飚高。

另外也有可能是因为tmp_table_size过大,超出了mysql的内存大小使用设定,mysql会将一些table写入到磁盘中,这样也会大大引起cpu的使用率增大

select * from PROCESSLIST where info is not null 中没有发现异样时,即可以推断另外一种的可能。

在mysql的官方文档中是这样写的

Storage Engines Used for Temporary Tables

An internal temporary table can be held in memory and processed by the MEMORY storage engine, or stored on disk and processed by the MyISAM storage engine.

If an internal temporary table is created as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table. The maximum size for in-memory temporary tables is determined from whichever of the values of tmp_table_size and max_heap_table_size is smaller. This differs from MEMORY tables explicitly created with CREATE TABLE: For such tables, only the max_heap_table_size system variable determines how large the table is permitted to grow and there is no conversion to on-disk format.

翻译过来的大意是,当tmp_table变得越来越大的时候,msql tmp_table使用内存最大值为tmp_table_sizemax_heap_table_size两者中较小的值。

而最后一句话特别的重要,当create table的时候(mysql临时表使用内存肯定会增加),max_heap_table_size才是决定临时表能创建多少的值。

所以一般max_heap_table_size要大于tmp_table_size

mysql> show global status like "%created_tmp%";
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Created_tmp_disk_tables | 1654  |
| Created_tmp_files       | 6     |
| Created_tmp_tables      | 1791  |
+-------------------------+-------+
3 rows in set (0.00 sec)

查看临时tables的实时数量

每次创建临时表,Created_tmp_tables增加,如果临时表大小超过tmp_table_size,则是在磁盘上创建临时表,但是其大小不能超过max_heap_table_size

posted @ 2017-08-14 18:13  marility  阅读(6450)  评论(0编辑  收藏  举报