【问题标题】:MySQL Optimization recommendationsMySQL 优化建议
【发布时间】:2020-04-14 04:16:39
【问题描述】:

我有一个 Directadmin/CentOS 7.0 Dedicate,有 128 GB RAM 和 2x CPU 10 core 2.0 Ghz

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

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

>>  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 10.4.11-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[OK] Log file /var/log/mysql.log exists
[--] Log file: /var/log/mysql.log(0B)
[OK] Log file /var/log/mysql.log is readable.
[!!] Log file /var/log/mysql.log is empty
[OK] Log file /var/log/mysql.log is smaller than 32 Mb
[OK] /var/log/mysql.log doesn't contain any warning.
[OK] /var/log/mysql.log doesn't contain any error.
[--] 0 start(s) detected in /var/log/mysql.log
[--] 0 shutdown(s) detected in /var/log/mysql.log

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +Aria +CSV +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in MyISAM tables: 426.0M (Tables: 34)
[--] Data in InnoDB tables: 175.6M (Tables: 29)
[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 is no basic password file list!

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

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 22m 0s (2M q [2K qps], 3K conn, TX: 2G, RX: 317M)
[--] Reads / Writes: 99% / 1%
[--] Binary logging is disabled
[--] Physical Memory     : 125.7G
[--] Max MySQL memory    : 131098.3G
[--] Other process memory: 0B
[--] Total buffers: 15.6G global + 32.0G per thread (4096 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 271.7G (216.19% of installed RAM)
[!!] Maximum possible memory usage: 131098.3G (104328.03% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/2M)
[OK] Highest usage of available connections: 0% (8/4096)
[OK] Aborted connections: 0.00%  (0/3914)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[!!] Query cache may be disabled by default due to mutex contention.
[OK] Query cache efficiency: 48.9% (2M cached / 5M selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 43K sorts)
[!!] Joins performed without indexes: 415
[!!] Temporary tables created on disk: 64% (8K on disk / 12K total)
[OK] Thread cache hit rate: 99% (8 created / 3K connections)
[OK] Table cache hit rate: 96% (187 open / 193 opened)
[OK] table_definition_cache(400) is upper than number of tables(329)
[OK] Open file limit used: 0% (148/20K)
[OK] Table locks acquired immediately: 99% (70K immediate / 70K locks)

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

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 40 thread(s).
[--] Using default value is good enough for your version (10.4.11-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 23.4% (125M used / 536M cache)
[OK] Key buffer size / total MyISAM indexes: 512.0M/53.6M
[OK] Read Key buffer hit rate: 100.0% (121M cached / 27K reads)
[OK] Write Key buffer hit rate: 99.8% (636 cached / 635 writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 8.0G/175.6M
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 1.0G * 2/8.0G should be equal to 25%
[OK] InnoDB buffer pool instances: 8
[--] Number of InnoDB Buffer Pool Chunk : 64 for 8 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.99% (58380753 hits/ 58389392 total)
[!!] InnoDB Write Log efficiency: 125.23% (139 hits/ 111 total)
[OK] InnoDB log waits: 0.00% (0 waits / 250 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/368.0K
[OK] Aria pagecache hit rate: 97.7% (357K cached / 8K reads)

-------- 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: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: OFF
[--] Semi synchronous replication Slave: OFF
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    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).
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Performance schema should be activated for better diagnostics
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (=0)
    query_cache_type (=0)
    join_buffer_size (> 32.0G, or always use indexes with JOINs)
    performance_schema = ON enable PFS

这是 my.cnf 配置

# include all files from the config directory
#
!includedir /etc/my.cnf.d

[mysqld]
max_connections = 4096
key_buffer_size = 512M
max_allowed_packet = 1024M
query_cache_limit = 256M
query_cache_size = 5G
query_cache_type = 1
innodb_log_file_size=1G
innodb_buffer_pool_size=8G
max_heap_table_size=2G
join_buffer_size=32G
tmp_table_size=2G
thread_cache_size=512

有人对现在该怎么做有任何建议吗?

【问题讨论】:

标签: mysql server config


【解决方案1】:

join_buffer_size=32G 对我来说似乎很高,可能是内存警告的原因,特别是考虑到max_connections = 4096 和 MySQLTuner 基本上将这两个数字相乘来估计内存消耗。我认为max_connections 也很高,根据目前提供的信息,远不及要求,请参阅Highest usage of available connections: 0% (8/4096)

MySQLTuner 给出了一些关于查询改进的提示,我将首先关注这些提示。 (确保 JOIN 使用索引,减少结果集大小……)

之后,重新启动服务器并让它运行 24 小时。然后再次运行 MySQLTuner 以获取新建议。

【讨论】:

    【解决方案2】:

    不要盲目增加设置,你很可能会用完内存,要么因为交换而变慢,甚至崩溃。

    max_connections = 4096  --> 500
    key_buffer_size = 512M  --> If not using MyISAM, lower to 20M
    query_cache_size = 5G     --> This is terrible (unless on Aurora).  Lower to 50M
    query_cache_limit = 256M  --> ditto
    innodb_buffer_pool_size=8G  --> 100G, since the 128G is dedicated for MySQL
    max_heap_table_size=2G   --> 1G
    tmp_table_size=2G        --> 1G
    join_buffer_size=32G   --> 1G
    thread_cache_size=512  --> 50
    

    “最大内存”警告是虚假的。没有准确的公式。不过,它为您指明了正确的方向。

    mysqltuner 有一些缺陷。有些是很难避免的。 (例如,一件事说提高 xyz,另一件事说降低 xyz。哪个应该赢?)

    如果您想要不同的分析,请参阅http://mysql.rjweb.org/doc.php/mysql_analysis

    【讨论】:

      【解决方案3】:

      为您的 my.cnf [mysqld] 部分考虑的建议

      max_connections = 256
      key_buffer_size = 192M
      max_allowed_packet = 1024M
      query_cache_limit = 1M
      query_cache_size = 96M
      query_cache_type = 1
      query_cache_min_res_unit=512
      innodb_log_file_size=1G
      innodb_log_buffer_size=512M
      innodb_buffer_pool_size=1G
      max_heap_table_size=64M
      join_buffer_size=256K
      tmp_table_size=64M
      thread_cache_size=256
      performance_schema=ON
      skip_name_resolve=1
      

      这些更改将显着降低 MySQLTuner 计算的最大可能内存使用量 131 TB,并在 TOP 报告时减少 CPU Busy。

      【讨论】:

      • mysql 没有在这个配置下运行!
      • @BehroozSedghi 删除最后两行。 PS和SNR,请尝试一下。如果它无法启动,您的错误日志中可能会有线索。如果它无法启动,请发布错误日志的最后 400 行。谢谢