Mysql – thesqld is terminated because the system is low on memory; what to do

crashmemoryMySQLmysqldperformancequery-performance

I did recently a site migration; during that I had to erase all plugins and the reinstalling only the minimum as the site was causing the server to crash (A Vps with 1gb of ram), the most problematic seemed to be related to caching plugins (the site's running on wordpress). So now the sites is on, but once in a while I get a "Out of memory: The process “mysqld” was terminated because the system is low on memory." , actually every day, although not at a determined hour (I don't have any cron jobs on). I tried running Mysqltuner, and go with their suggestions: tmp_table_size and max_heap_table_size from 16M to 32M; innodb_buffer_pool_size from 250M to 800 (80% of the total ram); innodb_buffer_pool_instances=1 and key_buffer_size from 12.8M to 25M.

Now I ran mysqltuner again, today (after relizing that mysqld was killed again this morning)– Of course I know that the statistical sake of this result is not very accurate (as the mysql serer's been running for less than 24h) but it would be unlikely to get the proper results as mysql keeps restarting every day due to the crashing– :

    >>  MySQLTuner 1.6.13 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.6.30
[OK] Operating on 32-bit architecture with less than 2GB RAM

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA 
[--] Data in MyISAM tables: 30M (Tables: 33)
[--] Data in InnoDB tables: 249M (Tables: 127)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[!!] User 'leechprotect@localhost' has no password set.
[!!] There is no basic password file list!

-------- CVE Security Recommendations --------------------------------------------------------------
[--] Skipped due to --cvefile option undefined

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 13h 13m 58s (2M q [54.533 qps], 40K conn, TX: 74G, RX: 280M)
[--] Reads / Writes: 99% / 1%
[--] Binary logging is disabled
[--] Physical Memory     : 1006.3M
[--] Max MySQL memory    : 1.0G
[--] Other process memory: 104.1M
[--] Total buffers: 874.0M global + 1.1M per thread (151 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 901.6M (89.60% of installed RAM)
[!!] Maximum possible memory usage: 1.0G (102.80% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/2M)
[OK] Highest usage of available connections: 17% (26/151)
[OK] Aborted connections: 0.04%  (17/40146)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (34 temp sorts / 219K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 82% (76K on disk / 92K total)
[OK] Table cache hit rate: 97% (273 open / 281 opened)
[OK] Open file limit used: 1% (113/10K)
[OK] Table locks acquired immediately: 100% (2M immediate / 2M locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is disabled.

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 12.4% (3M used / 26M cache)
[OK] Key buffer size / total MyISAM indexes: 25.0M/12.8M
[OK] Read Key buffer hit rate: 98.0% (10K cached / 211 reads)
[!!] Write Key buffer hit rate: 44.7% (2K cached / 1K writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is disabled.

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[OK] InnoDB buffer pool / data size: 800.0M/249.5M
[OK] InnoDB buffer pool instances: 1
[!!] InnoDB Used buffer: 9.87% (5055 used/ 51200 total)
[OK] InnoDB Read buffer efficiency: 100.00% (182843753 hits/ 182848469 total)
[!!] InnoDB Write Log efficiency: 50.11% (4188 hits/ 8357 total)
[OK] InnoDB log waits: 0.00% (0 waits / 12545 writes)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] This is a standalone server.

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Set up a Password for user with the following SQL statement ( SET PASSWORD FOR 'user'@'SpecificDNSorIp' = PASSWORD('secure_password'); )
    MySQL started within last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    tmp_table_size (> 32M)
    max_heap_table_size (> 32M)

So I have obviously to reduce the memory usage of MySQL. The VPS is only running 1 site, which barely has plugins now and not many visits either, so I think (and also hope) that the problem is not the RAM per se, but the my disastrous attempt of site migration.
Thanks in advance for any suggestion.

Best Answer

In 1GB of RAM, you may be able to set innodb_buffer_pool_size to 50M, maybe 100M. But definitely not 800M. The 80% advice is predicated on having at least 4GB of RAM, and 80% is too high even for that.

Having other things on the same machine (Apache?) adds to the cramped quarters. If you have Apache, lower MaxClients to 10.

Other things to keep out of trouble on that tiny machine:

max_connections = 25
table_open_cache = 300
key_buffer_size = 4M
tmp_table_size = 4M
max_heap_table_size = 4M

And look at the queries -- they are taking far more resources than they should ("Temporary tables created on disk: 82%")