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:

  1. 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
  2. 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.
  3. 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 -g

  • cat /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 status and 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 status and 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:

  1. 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的更多相关文章

  1. 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 ...

  2. MySQL运行内存不足时应采取的措施?

    排除故障指南:MySQL运行内存不足时应采取的措施? 天一阁@ 老叶茶馆 1周前 导读 排除故障指南:MySQL运行内存不足时应采取的措施? 翻译团队:知数堂藏经阁项目 - 天一阁 团队成员:天一阁- ...

  3. MySQL运行内存不足时应采取的措施

    导读 排除故障指南:MySQL运行内存不足时应采取的措施? 原文出处:<What To Do When MySQL Runs Out of Memory: Troubleshooting Gui ...

  4. MySQL存储引擎之InnoDB

    一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...

  5. Mysql中autocommit的用法

    定义 Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. ...

  6. 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 ...

  7. 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 ...

  8. [MySQL Reference Manual]15. 其他存储引擎

    15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...

  9. MySql索引总结

    索引概念 B+树索引分为聚集索引和非聚集索引(辅助索引),但是两者的数据结构都和B+树一样,区别是存放的内容. 可以说数据库必须有索引,没有索引则检索过程变成了顺序查找,O(n)的时间复杂度几乎是不能 ...

随机推荐

  1. 一个关于margin-top的问题

    两个 此时内部div的样式为 当我把margin选中 如图所示: 我想要的效果是子div离父div有一个20px的间隙,但显然现在不是我想要的结果, 然后就开始查资料: 这个“问题”……它是CSS2. ...

  2. 自己动手实现java数据结构(三) 栈

    1.栈的介绍 在许多算法设计中都需要一种"先进后出(First Input Last Output)"的数据结构,因而一种被称为"栈"的数据结构被抽象了出来. ...

  3. ionic的学习-01搭建App的起步准备

    Part1  搭建App的起步准备(建立项目文件夹,配置开发环境) 第一步 初始化npm npm init 文件夹变化 第二步 使用npm安装git npm install git --save 文件 ...

  4. js节流函数和js防止重复提交的N种方法

    应用情景 经典使用情景:js的一些事件,比如:onresize.scroll.mousemove.mousehover等: 还比如:手抖.手误.服务器没有响应之前的重复点击: 这些都是没有意义的,重复 ...

  5. 鸟哥的Linux私房菜:基础学习篇 —— 第六章笔记

    1.下面这些就是比较特殊的目录,得要用力的记下来才行: . 代表此层目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表“目前使用者身份”所在的主文件夹 ~account 代表 account ...

  6. SQL 两个时间获取相差秒数

    SELECT DATEDIFF(SECOND, '2005-12-31 23:59:00', '2006-01-01 00:00:00');

  7. WPF 使用 Direct2D1 画图入门

    本文来告诉大家如何在 WPF 使用 D2D 画图. 本文是一个系列 WPF 使用 Direct2D1 画图入门 WPF 使用 Direct2D1 画图 绘制基本图形 WPF 使用 SharpDX WP ...

  8. ajax+ashx:实现文件的批量导出

    背景: 最近公司有一个需求,就是实现excle的批量导出(一次性导出多个excle). 实现方式: 想到的实现方式: 1.发起一个导出请求,然后批量生产需要导出的excle文件,最后将文件生成一个压缩 ...

  9. T-SQL:排除阻塞(十六)

    当一个事务持有事务的资源锁,并且另一个事务请求同一资源的不兼容锁时,请求被阻塞并且请求者进入等待状态,直到锁定者释放干扰锁. 长时间运行事务会导致锁被长时间持有,所以只对要开启事务的表操作代码开启事务 ...

  10. UML速记

    依赖:虚线箭头 关联:实线箭头 接口:虚线三角 父类:实线三角 聚合:空心菱形 组合:实心菱形 顺着箭头方向: 依赖于和什么关联是什么的子类是什么的接口的实现是什么的聚合是什么的组合