我可以减少MySQL内存使用量,还是需要添加更多内存?
作者:互联网
我有一个2.25GB内存的VPS. MySQL现在使用1.4GB,我的网站上只有四个用户.即使没有用户在线,它似乎也会使用此数量.我的服务器的可用内存经常下降到100MB以下,这是令人不安的.
昨天MySQL因OOM错误而崩溃并导致数据库损坏,所以这才真正促使我调查这个问题.
我正在使用Percona配置向导生成的以下配置(稍加修改):
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5_20120208
# MODIFIED SLIGHTLY
[mysql]
# CLIENT #
port = 3306
socket = "/var/lib/mysql/mysql.sock"
[mysqld]
slow-query-log=1
long-query-time=1
# GENERAL #
user = mysql
default_storage_engine = InnoDB
socket = "/var/lib/mysql/mysql.sock"
pid_file = "/var/lib/mysql/mysql.pid"
# MyISAM #
key_buffer_size = 32M
myisam_recover = FORCE,BACKUP
# SAFETY #
max_allowed_packet = 256M # Percona said 16M, but I need it larger to avoid errors
max_connect_errors = 1000000
# DATA STORAGE #
datadir = "/var/lib/mysql/"
######### removed the following because the logs were taking up TONS Of space:
# BINARY LOGGING #
#log_bin = /var/lib/mysql/mysql_bin
#expire_logs_days = 14
#sync_binlog = 1
# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0 # was previously 1
query_cache_size = 0 # was previously 32M
max_connections = 500
thread_cache_size = 50
open_files_limit = 65535 # was previously 4822
table_definition_cache = 1024
table_open_cache = 2048 # was previously 1024
# INNODB #
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_log_file_size = 128M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 1456M
# LOGGING #
log_error = "/var/lib/mysql/mysql_error.log"
log_queries_not_using_indexes = 1
如您所见,我增加了max_allowed_packet,open_files_limit和table_open_cache.我做了更改,因为我收到错误(我忘记了现在导致它们的确切原因)并且进行这些更改导致错误消失.
另外,我注意到innodb_buffer_pool_size设置接近于MySQL正在使用多少RAM(这可能只是巧合),但是正如this answer建议我运行以下内容:
SELECT CEILING(Total_InnoDB_Bytes*1.6/POWER(1024,3)) RIBPS FROM
(SELECT SUM(data_length+index_length) Total_InnoDB_Bytes
FROM information_schema.tables WHERE engine='InnoDB') A;
输出为2,我相信推荐的innodb_buffer_pool_size是2GB.如果这是正确的,那么我当前的设置甚至低于建议的数量.
我真的不知道自己在做什么,也不愿意盲目地改变my.cnf.我可以做些什么来减少内存使用量,还是只需要升级我的VPS计划?
解决方法:
严格来说,上面推荐的缓冲池大小的计算是不正确的.它假设InnoDB主动使用所有数据.在实践中,InnoDB只能触及数据库的一小部分.
检查Innodb_buffer_pool_reads和Innodb_buffer_pool_read_requests如何随时间变化.
mysqladmin -r -i 1 ext | grep -e Innodb_buffer_pool_read_requests -e Innodb_buffer_pool_reads
如果Innodb_buffer_pool_reads变化不大,那么所有工作集(数据库InnoDB的一部分主动使用)都适合缓冲池.
理想情况下,缓冲池应该尽可能大,因此每秒Innodb_buffer_pool_reads为零.
您可以使缓冲池更小,然后从磁盘而不是从内存中提供一些读取.
标签:linux,mysql,centos,configuration,my-cnf 来源: https://codeday.me/bug/20190807/1607086.html