What To Do When MySQL Runs Out of Memory: Troubleshooting Guide
In this article, I will show you how to use the new version of MySQL (5.7+) and how to troubleshoot MySQL memory allocation more easily.
Troubleshooting crashes is never a fun task, especially if MySQL does not report the cause of the crash. For example, when MySQL runs out of memory. Peter Zaitsev wrote a blog post in 2012: Troubleshooting MySQL Memory Usage with a lot of useful tips. With the new versions of MySQL (5.7+) and performance_schema, we have the ability to troubleshoot MySQL memory allocation much more easily.
In this article, I will show you how to use it.
First of all, there are 3 major cases when MySQL will crash due to running out of memory:
- MySQL tries to allocate more memory than available because we specifically told it to do so. For example: you did not set innodb_buffer_pool_size correctly. This is very easy to fix
- There is some other process(es) on the server that allocates RAM. It can be the application (Java, Python, PHP), web server or even the backup (i.e. mysqldump). When the source of the problem is identified, it is straightforward to fix.
- Memory leaks in MySQL. This is a worst-case scenario, and we need to troubleshoot.
Where to Start Troubleshooting MySQL Memory Leaks
Here is what we can start with (assuming it is a Linux server):
Part 1: Linux OS and Config Check
1. Identify the crash by checking MySQL error log and Linux log file (i.e. /var/log/messages or /var/log/syslog). You may see an entry saying that OOM Killer killed MySQL. Whenever MySQL has been killed by OOM "dmesg" also shows details about the circumstances surrounding it.
2. Check the available RAM:
free -gcat /proc/meminfo
3. Check what applications are using RAM: “top” or “htop” (see the resident vs virtual memory)
4. Check MySQL configuration: check /etc/my.cnf or in general /etc/my* (including /etc/mysql/* and other files). MySQL may be running with the different my.cnf ( run ps ax| grep mysql )
5. Run vmstat 5 5 to see if the system is reading/writing via virtual memory and if it is swapping
6. For non-production environments, we can use other tools (like Valgrind, gdb, etc) to examine MySQL usage
Part 2: Checks Inside MySQL
Now we can check things inside MySQL to look for potential MySQL memory leaks.
MySQL allocates memory in tons of places, especially:
- Table cache
- Performance_schema (run:
show engine performance_schema statusand look at the last line). That may be the cause for the systems with a small amount of RAM, i.e. 1G or less - InnoDB (run
show engine innodb statusand check the buffer pool section, memory allocated for buffer_pool and related caches) - Temporary tables in RAM (find all in-memory tables by running:
select * from information_schema.tables where engine='MEMORY') - Prepared statements, when it is not deallocated (check the number of prepared commands via deallocate command by running show global status like
'Com_prepare_sql';show global status like 'Com_dealloc_sql')
The good news is, starting with MySQL 5.7, we have memory allocation in performance_schema. Here is how we can use it:
First, we need to enable collecting memory metrics. Run:
UPDATE setup_instruments SET ENABLED = 'YES'
WHERE NAME LIKE 'memory/%';
2. Run the report from sys schema:
select event_name, current_alloc, high_alloc
from sys.memory_global_by_current_bytes
where current_count > 0;
3. Usually, this will give you the place in code when memory is allocated. It is usually self-explanatory. In some cases, we can search for bugs or we might need to check the MySQL source code.
For example, for the bug where memory was over-allocated in triggers ( https://bugs.mysql.com/bug.php?id=86821) the select shows:
mysql> select event_name, current_alloc, high_alloc from memory_global_by_current_bytes where current_count > 0;
+--------------------------------------------------------------------------------+---------------+-------------+
| event_name | current_alloc | high_alloc |
+--------------------------------------------------------------------------------+---------------+-------------+
| memory/innodb/buf_buf_pool | 7.29 GiB | 7.29 GiB |
| memory/sql/sp_head::main_mem_root | 3.21 GiB | 3.62 GiB |
...
The largest chunk of RAM is usually the buffer pool but ~3G in stored procedures seems to be too high.
According to the MySQL source code documentation, sp_head represents one instance of a stored program, which might be of any type (stored procedure, function, trigger, event). In the above case, we have a potential memory leak.
In addition, we can get a total report for each higher level event if we want to see from the bird's eye what is eating memory:
mysql> select substring_index(
-> substring_index(event_name, '/', 2),
-> '/',
-> -1
-> ) as event_type,
-> round(sum(CURRENT_NUMBER_OF_BYTES_USED)/1024/1024, 2) as MB_CURRENTLY_USED
-> from performance_schema.memory_summary_global_by_event_name
-> group by event_type
-> having MB_CURRENTLY_USED>0;
+--------------------+-------------------+
| event_type | MB_CURRENTLY_USED |
+--------------------+-------------------+
| innodb | 0.61 |
| memory | 0.21 |
| performance_schema | 106.26 |
| sql | 0.79 |
+--------------------+-------------------+
4 rows in set (0.00 sec)
I hope these simple steps can help troubleshoot MySQL crashes due to running out of memory.
What To Do When MySQL Runs Out of Memory: Troubleshooting Guide的更多相关文章
- MySQL:cannot allocate the memory for the buffer pool
InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins Inno ...
- MySQL运行内存不足时应采取的措施?
排除故障指南:MySQL运行内存不足时应采取的措施? 天一阁@ 老叶茶馆 1周前 导读 排除故障指南:MySQL运行内存不足时应采取的措施? 翻译团队:知数堂藏经阁项目 - 天一阁 团队成员:天一阁- ...
- MySQL运行内存不足时应采取的措施
导读 排除故障指南:MySQL运行内存不足时应采取的措施? 原文出处:<What To Do When MySQL Runs Out of Memory: Troubleshooting Gui ...
- MySQL存储引擎之InnoDB
一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...
- Mysql中autocommit的用法
定义 Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. ...
- MySQL 5.6 Reference Manual-14.7 InnoDB Table Compression
14.7 InnoDB Table Compression 14.7.1 Overview of Table Compression 14.7.2 Enabling Compression for a ...
- MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...
- [MySQL Reference Manual]15. 其他存储引擎
15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...
- MySql索引总结
索引概念 B+树索引分为聚集索引和非聚集索引(辅助索引),但是两者的数据结构都和B+树一样,区别是存放的内容. 可以说数据库必须有索引,没有索引则检索过程变成了顺序查找,O(n)的时间复杂度几乎是不能 ...
随机推荐
- PCA历程详细python代码(原创)
#PCA主成分分析,原文为文末的链接,代码为自己亲自手码 def cov_out1(dx,dy): #第一步:求解x,y各自的均值 mean_x=0 mean_y=0 for i in range(l ...
- 基于httpclient的效率优化
1.背景 我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别.使用了httpclient来完成业务.之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里. 先 ...
- 用eclipse导入打war包的maven项目
最近遇到Maven管理下的Spring MVC项目,组内某位将项目代码扔过来,一脸懵逼(囧),查阅了一些资料后终于将此项目运行通了(>_<),特此记录下来与各位分享. 通俗的来说,Mave ...
- 全网最详细的IDEA里如何正确新建普通的Java web项目并发布到Tomcat上运行成功【博主强烈推荐】(类似eclipse里同一个workspace下【一个子项目】并存)(图文详解)
不多说,直接上干货! 首先,大家要明确,IDEA.Eclipse和MyEclipse等编辑器之间的新建和运行手法是不一样的. 如果是在Myeclipse里,则是File -> new -> ...
- windows下安装Jenkins
1.在机子上安装jdk8 2.下载jenkins.war在里面可以选择任意版本的war包,lastest为最新的,推荐下载! 官网地址:https://jenkins.io/download/ 3.打 ...
- 前后端分离demo 旅馆管理系统
模型设计 旅馆管理系统,主要涉及到登记入住,退房以及客房和客人信息管理:经过分析抽像出涉及到的实体以及各实体之间的关系: 可以看出整个业务以客房为中心,入住,退房,定价,收费都是以客房为基本单 ...
- vi/vim使用
移动光标上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向前 ...
- Anaconda 安装、使用
一.下载: 清华镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 二.安装: 默认就行,安装路径最好换一下 三.配置环境变量: 控制面 ...
- 为什么要先装IIS后装.Net Framework?
1.动态页面和静态页面的区别 动态页面(动态网站):通过C#代码(或别的语言)与服务器的交互的实现(比如新建一个ashx一般处理程序中的C#代码就可以和服务器实现交互,修改数据库,上传图片等都属于和服 ...
- [android] 手机卫士保存安全号码
调用ListView对象的setOnItemClickListener()方法,设置条目的点击事件,参数:OnItemClickListener对象 使用匿名内部类实现,重写onClick()方法,传 ...