首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL优化建议

MySQL优化建议
EN

Stack Overflow用户
提问于 2019-12-22 23:55:57
回答 3查看 3.6K关注 0票数 1

我有一台Directadmin/CentOS 7.0专用服务器,配备128 GB RAM和2x CPU 10核心2.0 Ghz

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

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

代码语言:javascript
复制
>>  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配置

代码语言:javascript
复制
# 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

有谁对现在该怎么做有什么建议吗?

EN

回答 3

Stack Overflow用户

发布于 2019-12-23 00:22:58

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

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

之后,重新启动服务器并让其运行24小时。然后再次运行MySQLTuner以获得新的推荐。

票数 1
EN

Stack Overflow用户

发布于 2019-12-23 03:43:01

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

代码语言:javascript
复制
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计算的131TB的最大可能内存使用量,并减少TOP报告的CPU占用率。

票数 0
EN

Stack Overflow用户

发布于 2019-12-24 08:30:34

不要盲目地增加设置,你可能会耗尽内存,或者因为交换而变得很慢,甚至崩溃。

代码语言:javascript
复制
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

"max mem“警告是假的。没有准确的公式。尽管如此,它还是为您指明了正确的方向。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59445763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档