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

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

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

解决过程回放

进入mysql

查看正在执行的sql

  1. mysql> use information_schema;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select * from PROCESSLIST where info is not null;
  6. +-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
  7. | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
  8. +-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
  9. | 291 | root | localhost | information_schema | Query | 0 | executing | select * from PROCESSLIST where info is not null |
  10. +-----+------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
  11. 1 row in set (0.00 sec)
  12. mysql>

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

然后查看tmp_table_size的大小

  1. mysql> show variables like '%table_size%';
  2. +---------------------+-----------+
  3. | Variable_name | Value |
  4. +---------------------+-----------+
  5. | max_heap_table_size | 16777216 |
  6. | tmp_table_size | 16777216 |
  7. +---------------------+-----------+
  8. 2 rows in set (0.00 sec)

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

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

  1. [root@iZbp16s0cap5fnfk6bjvw1Z ~]# grep -v ^# /etc/my.cnf | grep -v ^$
  2. [mysqld]
  3. datadir=/var/lib/mysql
  4. socket=/var/lib/mysql/mysql.sock
  5. tmp_table_size=200M
  6. 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的官方文档中是这样写的

  1. Storage Engines Used for Temporary Tables
  2. 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.
  3. 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

  1. mysql> show global status like "%created_tmp%";
  2. +-------------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------------+-------+
  5. | Created_tmp_disk_tables | 1654 |
  6. | Created_tmp_files | 6 |
  7. | Created_tmp_tables | 1791 |
  8. +-------------------------+-------+
  9. 3 rows in set (0.00 sec)

查看临时tables的实时数量

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

mysql 100%占用的解决的更多相关文章

  1. mysql cpu 100% 满 优化方案 解决MySQL CPU占用100%的经验总结

    下面是一些经验 供参考 解决MySQL CPU占用100%的经验总结 - karl_han的专栏 - CSDN博客 https://blog.csdn.net/karl_han/article/det ...

  2. 解决 MYSQL CPU 占用 100% 的经验总结

    朋友主机(Windows 2003 + IIS + PHP + MYSQL )近来 MySQL 服务进程 (mysqld-nt.exe) CPU 占用率总为 100% 高居不下.此主机有10个左右的 ...

  3. Win10安装后必做的优化,解决磁盘100%占用

    Win10安装后必做的优化,解决磁盘100%占用 01关闭家庭组 控制面板–管理工具–服务– HomeGroup Listener和HomeGroup Provider禁用. 02关闭磁盘碎片整理.自 ...

  4. paip.mysql备份慢的解决

    paip.mysql备份慢的解决.txt 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  5. Atitit.软件GUIbutton与仪表盘--db数据库区--导入mysql sql错误的解决之道

    Atitit.软件GUIbutton与仪表盘--db数据库区--导入mysql sql错误的解决之道 Keyword::截取文本文件后部分 查看提示max_allowed_packet限制 Targe ...

  6. Mysql CPU占用高的问题解决方法小结

    通过以前对mysql的操作经验,先将mysql的配置问题排除了,查看msyql是否运行正常,通过查看mysql data目录里面的*.err文件(将扩展名改为.txt)记事本查看即可.如果过大不建议用 ...

  7. Atitit.软件GUI按钮与仪表盘--db数据库区--导入mysql sql错误的解决之道

    Atitit.软件GUI按钮与仪表盘--db数据库区--导入mysql sql错误的解决之道 Keyword::截取文本文件后部分 查看提示max_allowed_packet限制 Target Se ...

  8. MYSQL转换编码的解决方法

    MYSQL转换编码的解决方法 一.在utf8的mysql下 得到中文‘游客’的gbk下的16进制编码 mysql> SELECT hex(CONVERT( '游客' USING gbk )); ...

  9. 设置height:100%无效的解决方法

    设置height:100%无效的解决方法 刚接触网页排版的新手,常出现这种情况:设置table和div的高height="100%"无效,使用CSS来设置height:" ...

随机推荐

  1. 前端学习(二十六)移动端s(笔记)

    ===================================================弹性布局rem布局---------------------------------------- ...

  2. maven3常用命令、java项目搭建、web项目搭建详细图解(转)

     转自:http://blog.csdn.net/edward0830ly/article/details/8748986 maven3常用命令.java项目搭建.web项目搭建详细图解 2013-0 ...

  3. Mac上无法使用蓝牙时的7个修复程序

    蓝牙是一个很普遍的技术,除非它出现问题,你才会发现它.例如,你听歌很顺利时,直到AirPods突然断连.大多数时候,这是一个相对容易的修复,但有时可能会有些棘手. https://www.macdow ...

  4. Qt的信号和槽机制

    一.信号和槽机制 信号和槽用于两个对象之间的通信,我们希望任何对象都可以和其他对象进行通信.     当一个特殊的事情发生时便可以发射一个信号,而槽就是一个函数,它在信号发射后被调用来相应这个信号.( ...

  5. Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)

    在将自己写的工具打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源. /** * 反射得到组件的id号 */ public static ...

  6. 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox   因为写的很好很全,所以转载过来 引言:接上一篇文章, ...

  7. mybatis plus CRUD

    首先我们的项目建立之后我们要建立一个实体类来对应我们的数据裤中的信息 employee import com.baomidou.mybatisplus.annotation.IdType; impor ...

  8. Java学习之接口

    接口作用:为类提供额外功能(方法) 一.接口定义 interface IDemo { ;//可以简写:int NUM=4; public abstract void show();//可以简写:voi ...

  9. Dubbo入门到精通学习笔记(二十):MyCat在MySQL主从复制的基础上实现读写分离、MyCat 集群部署(HAProxy + MyCat)、MyCat 高可用负载均衡集群Keepalived

    文章目录 MyCat在MySQL主从复制的基础上实现读写分离 一.环境 二.依赖课程 三.MyCat 介绍 ( MyCat 官网:http://mycat.org.cn/ ) 四.MyCat 的安装 ...

  10. LeetCode N皇后 & N皇后 II

    题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...