MySQL优化 - MySQL的最大内存使用率非常高,但仍然建议增加

时间:2014-04-11 04:39:22

标签: mysql

我有一个带12 GB RAM的cPanel / CentOS 6.5虚拟机。

我运行了MySQLTuner脚本,并按照他们给出的建议,但它仍然建议增加,同时告诉我MySQL的最大内存使用率非常高。

我在my.cnf文件中的当前配置是:

[mysqld]
local-infile=0
query_cache_size=32M
innodb_file_per_table=1
open_files_limit=6740
default-storage-engine=MyISAM
tmpdir=/mysqltmp
group_concat_max_len=102400

tmp_table_size=64M
max_connections=500
interactive_timeout=30
wait_timeout=30
thread_cache_size = 16
thread_cache_size=16
table_cache=2000
join_buffer_size=32M
query_cache_limit=512M
key_buffer_size=128M
max_heap_table_size=32M

我不久前重启了MySQL,以应用我最近的更改。

当我运行脚本时,这就是我得到的:

      >>  MySQLTuner 1.3.0 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
[OK] Currently running supported MySQL version 5.5.36-cll
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 551M (Tables: 467)
[--] Data in InnoDB tables: 49M (Tables: 908)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[--] Data in MEMORY tables: 0B (Tables: 37)
[!!] Total fragmented tables: 17

-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 2h 6m 33s (379K q [49.933 qps], 2K conn, TX: 390M, RX: 115M)
[--] Reads / Writes: 51% / 49%
[--] Total buffers: 336.0M global + 34.6M per thread (500 max threads)
[!!] Maximum possible memory usage: 17.2G (148% of installed RAM)
[OK] Slow queries: 0% (0/379K)
[OK] Highest usage of available connections: 1% (5/500)
[OK] Key buffer size / total MyISAM indexes: 128.0M/125.7M
[OK] Key buffer hit rate: 100.0% (4M cached / 1K reads)
[OK] Query cache efficiency: 58.0% (145K cached / 251K selects)
[!!] Query cache prunes per day: 892777
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 10K sorts)
[!!] Joins performed without indexes: 3054
[!!] Temporary tables created on disk: 29% (6K on disk / 20K total)
[OK] Thread cache hit rate: 99% (5 created / 2K connections)
[OK] Table cache hit rate: 95% (1K open / 1K opened)
[OK] Open file limit used: 14% (999/6K)
[OK] Table locks acquired immediately: 99% (261K immediate / 261K locks)
[OK] InnoDB buffer pool / data size: 128.0M/49.1M
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Enable the slow query log to troubleshoot bad queries
    Adjust your join queries to always utilize indexes
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries without LIMIT clauses
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (> 32M)
    join_buffer_size (> 32.0M, or always use indexes with joins)
    tmp_table_size (> 64M)
    max_heap_table_size (> 32M)

如果我增加这些变量,我的内存使用量会更高。

有没有人对现在该做什么有任何建议?

编辑:以下是上次MySQL重启后14小时的结果:

 >>  MySQLTuner 1.3.0 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
[OK] Currently running supported MySQL version 5.5.36-cll
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 552M (Tables: 467)
[--] Data in InnoDB tables: 49M (Tables: 908)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[--] Data in MEMORY tables: 0B (Tables: 37)
[!!] Total fragmented tables: 21

-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 14h 2m 3s (1M q [20.830 qps], 9K conn, TX: 1B, RX: 281M)
[--] Reads / Writes: 56% / 44%
[--] Total buffers: 336.0M global + 34.6M per thread (500 max threads)
[!!] Maximum possible memory usage: 17.2G (148% of installed RAM)
[OK] Slow queries: 0% (0/1M)
[OK] Highest usage of available connections: 1% (5/500)
[OK] Key buffer size / total MyISAM indexes: 128.0M/125.9M
[OK] Key buffer hit rate: 100.0% (15M cached / 1K reads)
[OK] Query cache efficiency: 76.3% (625K cached / 820K selects)
[!!] Query cache prunes per day: 250933
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 35K sorts)
[!!] Joins performed without indexes: 8623
[!!] Temporary tables created on disk: 31% (26K on disk / 82K total)
[OK] Thread cache hit rate: 99% (5 created / 9K connections)
[OK] Table cache hit rate: 58% (2K open / 3K opened)
[OK] Open file limit used: 17% (1K/6K)
[OK] Table locks acquired immediately: 99% (469K immediate / 469K locks)
[OK] InnoDB buffer pool / data size: 128.0M/49.1M
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Enable the slow query log to troubleshoot bad queries
    Adjust your join queries to always utilize indexes
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries without LIMIT clauses
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (> 32M)
    join_buffer_size (> 32.0M, or always use indexes with joins)
    tmp_table_size (> 64M)
    max_heap_table_size (> 32M)

非常感谢!

2 个答案:

答案 0 :(得分:4)

根据以下建议更改配置文件并检查 -

[mysqld]
local-infile=0

query_cache_size=32M
innodb_file_per_table  -- Just update innodb_file_per_table instead of =1
open_files_limit=6740 -- only 14% is using, so you can reduce it to 1500, to use your RAM in better way.
default-storage-engine=MyISAM
tmpdir=/mysqltmp
group_concat_max_len=102400

tmp_table_size=64M
max_connections=500 -- as per your error report, only 3 are in use so you can set it to 100. As it will use memory un-neccesorily.
interactive_timeout=30
wait_timeout=30
thread_cache_size = 16
table_cache=2000
join_buffer_size=32M
query_cache_limit=512M -- It should be less then query_cache_size, so keep it max. 10M
key_buffer_size=128M --  keep it 25% of your total RAM but not greater than 3 GB for Myisam engine, so in your case set is 3G.
max_heap_table_size=32M -- your server will use total memory for temp tables which ever will be lesser out of tmp_table_size, max_

答案 1 :(得分:3)

您的服务器真的需要500个多重连接吗?如果没有,您可以将此设置更改为正确的值。

该脚本基本上将 max_connections 与一些设置相乘。线程需要为自己分配一些内存(1个连接= 1个线程)。每个线程使用的一些设置:thread_stack,net_buffer_length,...

更多信息:https://dev.mysql.com/doc/refman/5.6/en/memory-use.html