MySQL 5.7 High Memory usage Killed by OOM

memorymy.cnfMySQLmysqltuner

i upgrade my WHM server / Cent-OS 7 from MySQL 5.6 to 5.7. after that upgrade every few hours my server memory full by MySQL which after hours of MySQL restart it exceed 80% of whole memory. i increase my server ram from 13GB to 17.5GB and still get same problem also i create swap for 3GB and still same problem. i change in MySQL configuration without any luck. so if anyone can help with that.

i try also mysqltuner but the problem that MySQL restarting every few hours and they said that MySQL must be running more than 24 hour at lease for recommendation to be accurate.

this is the configuration of the etc/my.cnf file:

[mysqld]
default-storage-engine=INNODB
innodb_file_per_table=1
max_allowed_packet=268435456
open_files_limit=80000


collation_server=utf8_unicode_ci
character_set_server=utf8
slow_query_log=1
long_query_time=3
max_connections = 200
sort_buffer_size=16M
join_buffer_size=1M
innodb_sort_buffer_size=16M
read_buffer_size=1M
read_rnd_buffer_size=2M
max_heap_table_size=64M
tmp_table_size=64M
key_buffer_size=128M
#event_scheduler=on

innodb_log_file_size=384M
query_cache_type=0
query_cache_size=0
table_open_cache=78000
table_definition_cache=39400
innodb_buffer_pool_instances=3
innodb_buffer_pool_size=3G

and the result of mysql tuner i don't know if it can help as MySQL not running from a long time:

>>  MySQLTuner 1.7.19 - 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.7.31-log
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[OK] Log file /var/lib/mysql/bilal.hosting.err exists
[--] Log file: /var/lib/mysql/bilal.hosting.err(10K)
[OK] Log file /var/lib/mysql/bilal.hosting.err is readable.
[OK] Log file /var/lib/mysql/bilal.hosting.err is not empty
[OK] Log file /var/lib/mysql/bilal.hosting.err is smaller than 32 Mb
[!!] /var/lib/mysql/bilal.hosting.err contains 16 warning(s).
[!!] /var/lib/mysql/bilal.hosting.err contains 2 error(s).
[--] 2 start(s) detected in /var/lib/mysql/bilal.hosting.err
[--] 1) 2020-09-12T11:38:30.571913Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 2) 2020-09-12T05:40:13.253650Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 0 shutdown(s) detected in /var/lib/mysql/bilal.hosting.err

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 10.0M (Tables: 51)
[--] Data in InnoDB tables: 5.4G (Tables: 30538)
[OK] Total fragmented tables: 0

-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[--] There are 620 basic passwords in the list.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 1h 20m 30s (407K q [84.438 qps], 8K conn, TX: 248M, RX: 48M)
[--] Reads / Writes: 98% / 2%
[--] Binary logging is disabled
[--] Physical Memory     : 17.0G
[--] Max MySQL memory    : 57.2G
[--] Other process memory: 0B
[--] Total buffers: 3.2G global + 276.2M per thread (200 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 5.1G (29.97% of installed RAM)
[!!] Maximum possible memory usage: 57.2G (336.45% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (21/407K)
[OK] Highest usage of available connections: 3% (7/200)
[OK] Aborted connections: 0.01%  (1/8243)
[!!] 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: 3% (872 temp sorts / 25K sorts)
[!!] Joins performed without indexes: 2920
[OK] Temporary tables created on disk: 1% (4K on disk / 414K total)
[OK] Thread cache hit rate: 99% (7 created / 8K connections)
[!!] Table cache hit rate: 4% (39K open / 851K opened)
[!!] table_definition_cache(39400) is lower than number of tables(44053)
[OK] Open file limit used: 0% (312/80K)
[OK] Table locks acquired immediately: 99% (1K immediate / 1K locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 72B
[--] Sys schema is installed.

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

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.3% (24M used / 134M cache)
[OK] Key buffer size / total MyISAM indexes: 128.0M/1.1M
[OK] Read Key buffer hit rate: 98.8% (4K cached / 55 reads)
[!!] Write Key buffer hit rate: 92.5% (40 cached / 37 writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[!!] InnoDB buffer pool / data size: 3.0G/5.4G
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 384.0M * 2/3.0G should be equal to 25%
[OK] InnoDB buffer pool instances: 3
[--] Number of InnoDB Buffer Pool Chunk : 24 for 3 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 99.95% (216374723 hits/ 216488054 total)
[!!] InnoDB Write Log efficiency: 86.23% (10420 hits/ 12084 total)
[OK] InnoDB log waits: 0.00% (0 waits / 1664 writes)

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

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

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

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

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: ROW
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Control warning line(s) into /var/lib/mysql/bilal.hosting.err file
    Control error line(s) into /var/lib/mysql/bilal.hosting.err file
    MySQL was started within the 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
    We will suggest raising the 'join_buffer_size' until JOINs not using indexes are found.
             See https://dev.mysql.com/doc/internals/en/join-buffer-size.html
             (specially the conclusions at the bottom of the page).
    Increase table_open_cache gradually to avoid file descriptor limits
    Read this before increasing table_open_cache over 64: https://bit.ly/2Fulv7r
    Read this before increasing for MariaDB https://mariadb.com/kb/en/library/optimizing-table_open_cache/
    This is MyISAM only table_cache scalability problem, InnoDB not affected.
    See more details here: https://bugs.mysql.com/bug.php?id=49177
    This bug already fixed in MySQL 5.7.9 and newer MySQL versions.
    Beware that open_files_limit (80000) variable
    should be greater than table_open_cache (39895)
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    join_buffer_size (> 1.0M, or always use indexes with JOINs)
    table_open_cache (> 39895)
    table_definition_cache(39400) > 44053 or -1 (autosizing if supported)
    innodb_buffer_pool_size (>= 5.4G) if possible.

note i try to put innodb_buffer_pool_size to 6G as recommended but still the same memory load.

in mysql tuner result they gave result that Maximum possible memory usage: 57.2G (336.45% of installed RAM) is that a problem and how to fix this issue!

also no load on the server or mysql. and on top just the mysql is taking the memory above 70% of it

and this the output of free:

              total        used        free      shared  buff/cache   available
Mem:            16G         13G        323M         14M        3.6G        3.6G
Swap:          3.0G        2.1G        933M
Total:          19G         15G        1.2G

OOM Log Example:

# grep -i "out of memory" /var/log/messages*
/var/log/messages:Sep  7 03:00:23 hosting kernel: Out of memory: Kill process 1969 (mysqld) score 695 or sacrifice child
/var/log/messages:Sep  7 11:41:09 hosting kernel: Out of memory: Kill process 1787 (mysqld) score 756 or sacrifice child
/var/log/messages:Sep  7 13:25:46 hosting kernel: Out of memory: Kill process 8455 (mysqld) score 723 or sacrifice child
/var/log/messages:Sep  7 15:29:27 hosting kernel: Out of memory: Kill process 28063 (mysqld) score 734 or sacrifice child
/var/log/messages:Sep  7 20:56:49 hosting kernel: Out of memory: Kill process 23450 (mysqld) score 727 or sacrifice child
/var/log/messages:Sep  7 20:56:50 hosting kernel: Out of memory: Kill process 2043 (clamd) score 80 or sacrifice child
/var/log/messages:Sep  8 03:05:20 hosting kernel: Out of memory: Kill process 26713 (mysqld) score 681 or sacrifice child
/var/log/messages:Sep  8 05:28:19 hosting kernel: Out of memory: Kill process 3038 (mysqld) score 681 or sacrifice child
/var/log/messages:Sep  8 11:14:35 hosting kernel: Out of memory: Kill process 30391 (mysqld) score 742 or sacrifice child
/var/log/messages:Sep  8 15:02:51 hosting kernel: Out of memory: Kill process 3984 (mysqld) score 742 or sacrifice child
/var/log/messages:Sep  9 12:21:44 hosting kernel: Out of memory: Kill process 1743 (mysqld) score 763 or sacrifice child
/var/log/messages:Sep  9 14:31:21 hosting kernel: Out of memory: Kill process 16796 (mysqld) score 774 or sacrifice child
/var/log/messages:Sep 10 13:01:14 hosting kernel: Out of memory: Kill process 4654 (mysqld) score 788 or sacrifice child
/var/log/messages:Sep 10 13:01:14 hosting kernel: Out of memory: Kill process 2002 (clamd) score 61 or sacrifice child
/var/log/messages:Sep 10 16:31:45 hosting kernel: Out of memory: Kill process 12720 (mysqld) score 816 or sacrifice child

Update:: 15/08/2020

this is the result of show global status:

Variable_name   Value   
Aborted_clients     0
Aborted_connects    6
Binlog_cache_disk_use   0
Binlog_cache_use    0
Binlog_stmt_cache_disk_use  0
Binlog_stmt_cache_use   0
Bytes_received  187149967
Bytes_sent  539231478
Com_admin_commands  3192
Com_assign_to_keycache  0
Com_alter_db    0
Com_alter_db_upgrade    0
Com_alter_event     0
Com_alter_function  0
Com_alter_instance  0
Com_alter_procedure     0
Com_alter_server    0
Com_alter_table     0
Com_alter_tablespace    0
Com_alter_user  0
Com_analyze     0
Com_begin   707
Com_binlog  0
Com_call_procedure  10
Com_change_db   3467
Com_change_master   0
Com_change_repl_filter  0
Com_check   0
Com_checksum    0
Com_commit  752
Com_create_db   0
Com_create_event    0
Com_create_function     0
Com_create_index    0
Com_create_procedure    0
Com_create_server   0
Com_create_table    0
Com_create_trigger  0
Com_create_udf  0
Com_create_user     0
Com_create_view     0
Com_dealloc_sql     0
Com_delete  790
Com_delete_multi    39
Com_do  0
Com_drop_db     0
Com_drop_event  0
Com_drop_function   0
Com_drop_index  0
Com_drop_procedure  0
Com_drop_server     0
Com_drop_table  0
Com_drop_trigger    0
Com_drop_user   1
Com_drop_view   0
Com_empty_query     0
Com_execute_sql     0
Com_explain_other   0
Com_flush   0
Com_get_diagnostics     0
Com_grant   0
Com_ha_close    0
Com_ha_open     0
Com_ha_read     0
Com_help    0
Com_insert  9398
Com_insert_select   0
Com_install_plugin  0
Com_kill    0
Com_load    0
Com_lock_tables     0
Com_optimize    0
Com_preload_keys    0
Com_prepare_sql     0
Com_purge   0
Com_purge_before_date   0
Com_release_savepoint   0
Com_rename_table    0
Com_rename_user     0
Com_repair  0
Com_replace     0
Com_replace_select  0
Com_reset   0
Com_resignal    0
Com_revoke  0
Com_revoke_all  0
Com_rollback    1
Com_rollback_to_savepoint   0
Com_savepoint   0
Com_select  1080660
Com_set_option  47077
Com_signal  0
Com_show_binlog_events  0
Com_show_binlogs    0
Com_show_charsets   0
Com_show_collations     0
Com_show_create_db  0
Com_show_create_event   0
Com_show_create_func    0
Com_show_create_proc    0
Variable_name   Value    
Com_show_create_table   35
Com_show_create_trigger     0
Com_show_databases  0
Com_show_engine_logs    0
Com_show_engine_mutex   0
Com_show_engine_status  0
Com_show_events     0
Com_show_errors     0
Com_show_fields     116
Com_show_function_code  0
Com_show_function_status    0
Com_show_grants     0
Com_show_keys   41
Com_show_master_status  1
Com_show_open_tables    0
Com_show_plugins    1
Com_show_privileges     0
Com_show_procedure_code     0
Com_show_procedure_status   0
Com_show_processlist    151
Com_show_profile    0
Com_show_profiles   0
Com_show_relaylog_events    0
Com_show_slave_hosts    0
Com_show_slave_status   1
Com_show_status     103
Com_show_storage_engines    0
Com_show_table_status   0
Com_show_tables     226
Com_show_triggers   1
Com_show_variables  42
Com_show_warnings   23
Com_show_create_user    0
Com_shutdown    0
Com_slave_start     0
Com_slave_stop  0
Com_group_replication_start     0
Com_group_replication_stop  0
Com_stmt_execute    99
Com_stmt_close  99
Com_stmt_fetch  0
Com_stmt_prepare    99
Com_stmt_reset  0
Com_stmt_send_long_data     0
Com_truncate    0
Com_uninstall_plugin    0
Com_unlock_tables   0
Com_update  2619
Com_update_multi    29
Com_xa_commit   0
Com_xa_end  0
Com_xa_prepare  0
Com_xa_recover  0
Com_xa_rollback     0
Com_xa_start    0
Com_stmt_reprepare  0
Connection_errors_accept    0
Connection_errors_internal  0
Connection_errors_max_connections   0
Connection_errors_peer_address  0
Connection_errors_select    0
Connection_errors_tcpwrap   0
Connections     21796
Created_tmp_disk_tables     7006
Created_tmp_files   5594
Created_tmp_tables  465690
Delayed_errors  0
Delayed_insert_threads  0
Delayed_writes  0
Flush_commands  1
Handler_commit  1092598
Handler_delete  3527
Handler_discover    0
Handler_external_lock   2692797
Handler_mrr_init    0
Handler_prepare     0
Handler_read_first  3291451
Handler_read_key    94909022
Handler_read_last   597
Handler_read_next   584655958
Handler_read_prev   2289457
Handler_read_rnd    22585333
Handler_read_rnd_next   1039026278
Handler_rollback    9
Handler_savepoint   0
Handler_savepoint_rollback  0
Handler_update  3001107
Handler_write   13348212
Innodb_buffer_pool_dump_status  Dumping of buffer pool not started
Innodb_buffer_pool_load_status  Buffer pool(s) load completed at 200915 14:25:20
Innodb_buffer_pool_resize_status    
Innodb_buffer_pool_pages_data   120170
Innodb_buffer_pool_bytes_data   1968865280
Innodb_buffer_pool_pages_dirty  6
Innodb_buffer_pool_bytes_dirty  98304
Innodb_buffer_pool_pages_flushed    190308
Innodb_buffer_pool_pages_free   2047
Innodb_buffer_pool_pages_misc   8839
Innodb_buffer_pool_pages_total  131056
Innodb_buffer_pool_read_ahead_rnd   0
Variable_name   Value    
Innodb_buffer_pool_read_ahead   35380
Innodb_buffer_pool_read_ahead_evicted   842
Innodb_buffer_pool_read_requests    1230004513
Innodb_buffer_pool_reads    156026
Innodb_buffer_pool_wait_free    0
Innodb_buffer_pool_write_requests   9391121
Innodb_data_fsyncs  13083
Innodb_data_pending_fsyncs  0
Innodb_data_pending_reads   0
Innodb_data_pending_writes  0
Innodb_data_read    3170914816
Innodb_data_reads   223898
Innodb_data_writes  197504
Innodb_data_written     3536651776
Innodb_dblwr_pages_written  24887
Innodb_dblwr_writes     2505
Innodb_log_waits    0
Innodb_log_write_requests   14091
Innodb_log_writes   3345
Innodb_os_log_fsyncs    4539
Innodb_os_log_pending_fsyncs    0
Innodb_os_log_pending_writes    0
Innodb_os_log_written   10433536
Innodb_page_size    16384
Innodb_pages_created    1257
Innodb_pages_read   193533
Innodb_pages_written    190308
Innodb_row_lock_current_waits   0
Innodb_row_lock_time    0
Innodb_row_lock_time_avg    0
Innodb_row_lock_time_max    0
Innodb_row_lock_waits   0
Innodb_rows_deleted     3519
Innodb_rows_inserted    20139923
Innodb_rows_read    1686296409
Innodb_rows_updated     4347
Innodb_num_open_files   30356
Innodb_truncated_status_writes  0
Innodb_available_undo_logs  128
Key_blocks_not_flushed  0
Key_blocks_unused   107124
Key_blocks_used     47
Key_read_requests   4732
Key_reads   47
Key_write_requests  20
Key_writes  17
Locked_connects     0
Max_execution_time_exceeded     0
Max_execution_time_set  0
Max_execution_time_set_failed   0
Max_used_connections    12
Max_used_connections_time   2020-09-15 16:00:04
Not_flushed_delayed_rows    0
Ongoing_anonymous_transaction_count     0
Open_files  328
Open_streams    0
Open_table_definitions  17900
Open_tables     31428
Opened_files    2817777
Opened_table_definitions    2090652
Opened_tables   880851
Performance_schema_accounts_lost    0
Performance_schema_cond_classes_lost    0
Performance_schema_cond_instances_lost  0
Performance_schema_digest_lost  0
Performance_schema_file_classes_lost    0
Performance_schema_file_handles_lost    0
Performance_schema_file_instances_lost  0
Performance_schema_hosts_lost   0
Performance_schema_index_stat_lost  0
Performance_schema_locker_lost  0
Performance_schema_memory_classes_lost  0
Performance_schema_metadata_lock_lost   0
Performance_schema_mutex_classes_lost   0
Performance_schema_mutex_instances_lost     0
Performance_schema_nested_statement_lost    0
Performance_schema_prepared_statements_lost     0
Performance_schema_program_lost     0
Performance_schema_rwlock_classes_lost  0
Performance_schema_rwlock_instances_lost    0
Performance_schema_session_connect_attrs_lost   0
Performance_schema_socket_classes_lost  0
Performance_schema_socket_instances_lost    0
Performance_schema_stage_classes_lost   0
Performance_schema_statement_classes_lost   0
Performance_schema_table_handles_lost   0
Performance_schema_table_instances_lost     0
Performance_schema_table_lock_stat_lost     0
Performance_schema_thread_classes_lost  0
Performance_schema_thread_instances_lost    0
Performance_schema_users_lost   0
Prepared_stmt_count     0
Qcache_free_blocks  0
Qcache_free_memory  0
Qcache_hits     0
Qcache_inserts  0
Qcache_lowmem_prunes    0
Qcache_not_cached   0
Qcache_queries_in_cache     0
Qcache_total_blocks     0
Variable_name   Value    
Queries     1668933
Questions   1166498
Rsa_public_key  -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQE...
Select_full_join    8768
Select_full_range_join  91
Select_range    41874
Select_range_check  0
Select_scan     67251
Slave_open_temp_tables  0
Slow_launch_threads     0
Slow_queries    10
Sort_merge_passes   9043
Sort_range  86052
Sort_rows   15315042
Sort_scan   51047
Ssl_accept_renegotiates     0
Ssl_accepts     0
Ssl_callback_cache_hits     0
Ssl_cipher  
Ssl_cipher_list     
Ssl_client_connects     0
Ssl_connect_renegotiates    0
Ssl_ctx_verify_depth    18446744073709551615
Ssl_ctx_verify_mode     5
Ssl_default_timeout     0
Ssl_finished_accepts    0
Ssl_finished_connects   0
Ssl_server_not_after    Sep  4 04:04:27 2030 GMT
Ssl_server_not_before   Sep  6 04:04:27 2020 GMT
Ssl_session_cache_hits  0
Ssl_session_cache_misses    0
Ssl_session_cache_mode  SERVER
Ssl_session_cache_overflows     0
Ssl_session_cache_size  128
Ssl_session_cache_timeouts  0
Ssl_sessions_reused     0
Ssl_used_session_cache_entries  0
Ssl_verify_depth    0
Ssl_verify_mode     0
Ssl_version     
Table_locks_immediate   590
Table_locks_waited  0
Table_open_cache_hits   2872174
Table_open_cache_misses     880851
Table_open_cache_overflows  849416
Tc_log_max_pages_used   0
Tc_log_page_size    0
Tc_log_page_waits   0
Threads_cached  3
Threads_connected   7
Threads_created     12
Threads_running     4
Uptime  7541
Uptime_since_flush_status   7541

below is the link for the output of: SHOW GLOBAL VARIABLES

https://pastebin.com/DFekk1Yv

linux limits: ulimit -a output:

https://pastebin.com/VF3igLHg

output of iostat -xm 5 3:

https://pastebin.com/JBErnUWW

for htop process:

https://pasteboard.co/JriBpd2.png

Thanks,

Best Answer

Rate Per Second = RPS Suggestions to consider for your my.cnf [mysqld] section

sort_buffer_size=2M  # from 8M to conserve RAM - ndx's will reduce requirements
join_buffer_size=128K  # from 1M to conserve RAM - ndx's will reduce requirements
max_write_lock_count=16  # from ~ 4 billion to allow read data after nn lcks
innodb_io_capacity=1900  # from 200 to use more of your SSD IOPS
read_rnd_buffer_size=192K  # from 1M to reduce handler_read_rnd_next RPS of 137,802
innodb_buffer_pool_size=4G  # from 2G to reduce innodb_buffer_pool_reads RPS of 21
net_buffer_length=32K  # from 16K to reduce # packets sent

Observations: Keep innodb_buffer_pool_instances at 2 until you have more RAM and CPU/cores

You should find these changes will REDUCE CPU busy significantly.

For additional assistance, please view my profile, Network profile for free downloadable Utility Scripts to assist with performance tuning.