当前位置: 首页 > news >正文

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

转载于:https://www.cnblogs.com/marility/p/7359475.html

相关文章:

  • 天天爱跑步NOIP
  • 白话经典之String字符串详解
  • C++ STL疑惑知识点
  • Python基础
  • 理解浏览器关键的渲染路径
  • Android RecyclerView 水平滚动+自动循环轮播
  • GBDT和随机森林的区别
  • C#使用Xamarin开发可移植移动应用(3.Xamarin.Views控件)附源码
  • 在客户端先通过JS验证后再将表单提交到服务器
  • hihocoder 1320 压缩字符串(字符串+dp)
  • 一元线性回归模型的基本假设
  • marathon小知识点分享之如何远程调试marathon
  • AJAX的get和post请求原生编写方法
  • C/C++ 数据范围
  • Linux操作系统目录和Linux常用的文件和目录管理命令
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • CSS中外联样式表代表的含义
  • css属性的继承、初识值、计算值、当前值、应用值
  • Docker入门(二) - Dockerfile
  • ES6 学习笔记(一)let,const和解构赋值
  • HashMap剖析之内部结构
  • Java 内存分配及垃圾回收机制初探
  • JAVA并发编程--1.基础概念
  • Linux gpio口使用方法
  • mysql中InnoDB引擎中页的概念
  • storm drpc实例
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • vue的全局变量和全局拦截请求器
  • 爱情 北京女病人
  • 编写符合Python风格的对象
  • 初识MongoDB分片
  • 大快搜索数据爬虫技术实例安装教学篇
  • 马上搞懂 GeoJSON
  • 前端之React实战:创建跨平台的项目架构
  • 入手阿里云新服务器的部署NODE
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (C++17) std算法之执行策略 execution
  • (定时器/计数器)中断系统(详解与使用)
  • (二)springcloud实战之config配置中心
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (七)c52学习之旅-中断
  • (十八)三元表达式和列表解析
  • .Family_物联网
  • .md即markdown文件的基本常用编写语法
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • [.net] 如何在mail的加入正文显示图片
  • [AIGC] Spring Interceptor 拦截器详解
  • [Docker]十一.Docker Swarm集群raft算法,Docker Swarm Web管理工具
  • [error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
  • [flink总结]什么是flink背压 ,有什么危害? 如何解决flink背压?flink如何保证端到端一致性?
  • [HCTF 2018]WarmUp (代码审计)