前两天刚把服务器部署上,今天就出现了异常。在 App 上看是服务器直接宕机。原本以为仅仅是服务端挂了,在服务器上使用 ps aux | grep java 发现进程还在,只是不服务了。打开 log 发现 Java run out of memory,导致无法创建新的线程。

使用 top -o %MEM 查看,java 占用近 200M,mysql 占用近 500 M。因为是个人用的小服务器,只有 1G 内存,吃紧得很。既然 mysql 占用了最多的内存,就让它吐出一点好了。

一开始猜测是 InnoDB 缓冲区过大,不过在 /etc/mysql/my.cnf 中发现配置的缓存已经非常小了。后来在网上发现是因为 Mysql 5.6 版本默认 GA 配置过大,导致初始需要近 400M 空间。通过下面配置,直接讲内存降到 100M 以下:

performance_schema_max_table_instances=400
table_definition_cache=400
table_open_cache=256

参数意义:

  • performance_schema_max_table_instances

The maximum number of instrumented table objects

检测的表对象的最大数目。

  • table_definition_cache

The number of table definitions (from .frm files) that can be stored in the definition cache. If you use a large number of tables, you can create a large table definition cache to speed up opening of tables. The table definition cache takes less space and does not use file descriptors, unlike the normal table cache. The minimum and default values are both 400.

缓存 frm 文件

  • table_open_cache The number of open tables for all threads. Increasing this value increases the number of file descriptors that mysqld requires.

table_open_cache 指的是缓存数据文件的描述符(Linux/Unix)相关信息

在 5.6 中的默认配置:

performance_schema_max_table_instances 12500
table_definition_cache 1400
table_open_cache 2000

References: