MySQL consuming memory, seeking advice on how to configure it

configurationmemoryMySQL

I have a small VPS that handles my 3 personal WordPress websites.

At a certain time MySQL started consuming more and more memory and actually (for months) the server has been running at 99% RAM usage with MySQL eating more than 2 GB alone.

My VPS is an Ubuntu 14.04.05 with MySQL 5.7.23 (I upgraded to avoid the memory eating bug), 2 amd64 CPU@3.9GHZ and 3 GB of RAM.

The webserver is configured by Virtualmin, with little to no modifications applied by myself (read after).

I tried MySQLTuner, and some other scripts but nothing helps.

I have only these 3 websites and every time the traffic spikes (these are personal websites, so "spikes" are maybe 3 users at the same time) the VPS crashes.

Can you please help me configure it to use lesser RAM or diagnose if there is an issue?
I'm thinking about switching to MariaDB, but without changing the configuration I don't think that anything will change.

All the relevant informations are in this pastebin (posted Aug 7, 2018) for readability:

https://pastebin.com/UasHBcH1

According to MySQL Calculator it should only require 293 MB and I don't have so much visitors to justify this usage.

Best Answer

Suggestions to consider for your my.cnf [mysqld] section

thread_cache_size=20  # from 8  mysqld takes 8 minimum, keep breathing room
read_buffer_size=1M  # from 256K to reduce handler_read_next count
table_open_cache=500  # from 80  to support 327 opened since startup
innodb_buffer_pool_size=1G # from 128M for effective 750M for growth over time
innodb_log_file_size=64M  # from 8M to be larger than innodb_log_buffer_size
innodb_io_read_threads=16  # from 3 to speed up reads
innodb_io_write_threads=16  # from 3 to speed up writes
innodb_io_capacity=1000  # from 200 to increase iops limit
max_heap_table_size=16M  # from 12M to raise RAM capacity
tmp_table_size=16M  # from 12M should be same size and will reduce created_tmp_disk_tables
innodb_buffer_pool_dump_pct=90  # from 25 for reduced WARM up time
query_cache_limit=0  # from 525312 conserve RAM QC not being used

for additional suggestions, view profile, Network Profile for contact info, including Skype ID.