MySQL 数据库性能调优

标签 mysql database-administration mysqltuner

我开发了一个 PHP 和 MySQL 网络应用程序。最近,它一直显示出放缓的迹象。我在 090516 上运行了 mysql-tuner,正常运行时间为 12 天。

服务器规范如下:
- Linux CentOS 6
- 48 个处理器
- 64GB 内存

这是 090516 的 mysql-tuner 日志输出,正常运行时间为 12 天:

root@layer1 [~/mysqltuner]# perl mysqltuner.pl --outputfile /~/mysqltuner/result_mysqltuner.txt
String found where operator expected at mysqltuner.pl line 3096, near
        "get_wsrep_option 'gcache.mem_size'" (#1)
    (S syntax) The Perl lexer knows whether to expect a term or an operator.
    If it sees what it knows to be a term when it was expecting to see an
    operator, it gives you this warning.  Usually it indicates that an
    operator or delimiter was omitted, such as a semicolon.

        (Do you need to predeclare get_wsrep_option?)
Use of implicit split to @_ is deprecated at mysqltuner.pl line 3179 (#2)
    (D deprecated, W syntax) It makes a lot of work for the compiler when you
    clobber a subroutine's argument list, so it's better if you assign the results
    of a split() explicitly to an array (or list).

 >>  MySQLTuner 1.6.10 - 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 5.6.31-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 913M (Tables: 11)
[--] Data in InnoDB tables: 12M (Tables: 63)
[!!] Total fragmented tables: 11

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] User 'munin@localhost' has user name as password.
[--] There are 605 basic passwords in the list.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION
Argument "gcache.mem_size" isn't numeric in addition (+) at mysqltuner.pl line
        1965 (#3)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

Argument "gcache.mem_size" isn't numeric in addition (+) at mysqltuner.pl line
        1972 (#3)

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 12d 6h 21m 24s (2 q [0.000 qps], 1M conn, TX: 192B, RX: 240B)
[--] Reads / Writes: 100% / 0%
[--] Binary logging is disabled
[--] Physical Memory     : 62.8G
[--] Max MySQL memory    : 338.9M
[--] Other process memory: 505.0M
[--] Total buffers: 169.0M global + 1.1M per thread (151 max threads)
[--] P_S Max memory usage: 0B
Argument "*main::get_wsrep_option" isn't numeric in numeric ge (>=) at
        mysqltuner.pl line 285 (#3)
Argument "*main::get_wsrep_option" isn't numeric in numeric ge (>=) at
        mysqltuner.pl line 288 (#3)
Argument "*main::get_wsrep_option" isn't numeric in numeric ge (>=) at
        mysqltuner.pl line 291 (#3)
[--] Galera GCache Max memory usage: *main::get_wsrep_optionB
[OK] Maximum reached memory usage: 340.0M (0.53% of installed RAM)
[OK] Maximum possible memory usage: 338.9M (0.53% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (0/2)
[!!] Highest connection usage: 100%  (152/151)
[OK] Aborted connections: 0.00%  (34/1098331)
[OK] Query cache is disabled by default due to mutex contention.
[OK] No Sort requiring temporary tables
[OK] No joins without indexes
[OK] No tmp tables created on disk
[OK] Thread cache hit rate: 98% (17K created / 1M connections)
[OK] Table cache hit rate: 100% (381 open / 0 opened)
[OK] Open file limit used: 2% (228/10K)
[!!] Table locks acquired immediately: 94%

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is disabled.

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

-------- MyISAM Metrics ----------------------------------------------------------------------------
[OK] Key buffer used: 100.0% (8M used / 8M cache)
[!!] Key buffer size / total MyISAM indexes: 8.0M/147.7M
[!!] Read Key buffer hit rate: 87.6% (16M cached / 1M reads)
[!!] Write Key buffer hit rate: 56.8% (1M cached / 777K writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is disabled.

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[OK] InnoDB buffer pool / data size: 128.0M/12.1M
[!!] InnoDB buffer pool <= 1G and innodb_buffer_pool_instances(!=1).
[!!] InnoDB Used buffer: 17.82% (1460 used/ 8192 total)
[OK] InnoDB Read buffer efficiency: 100.00% (290756388 hits/ 290757478 total)
[!!] InnoDB Write Log efficiency: 12.12% (3691 hits/ 30446 total)
[OK] InnoDB log waits: 0.00% (0 waits / 34137 writes)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] This is a standalone server.

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    Set up a Secure Password for user@host ( SET PASSWORD FOR 'user'@'SpecificDNSorIp' = PASSWORD('secure_password'); )
    Reduce or eliminate persistent connections to reduce connection usage
    Optimize queries and/or use InnoDB to reduce lock wait
Variables to adjust:
    max_connections (> 151)
    wait_timeout (< 28800)
    interactive_timeout (< 28800)
    key_buffer_size (> 147.7M)
    innodb_buffer_pool_instances (=1)

我没有什么事情会突然想到,但是我不是经验丰富的 MySQL DBA,因此我正在寻求堆栈社区的建议。请告知我应该在 my.cnf 中修改哪些设置,如果我需要在 PHPMYADMIN 中对表进行碎片整理,是否需要在下类时间进行。

直到最近我才注意到我的网络应用程序变慢了,最近我在内核更新后重新启动了服务器。截至这篇文章,当前正常运行时间为 19 小时。

**额外信息:** Web 应用程序有一个前端,用户使用移动设备登录,办公室员工管理提交(表单)并将它们发送到不同部门的后端。

如果需要,我可以用 munin 图表更新它。我知道服务器严重过度配置(这意味着它的 RAM 和处理器比需要的多得多,但我希望在未来拥有足够多的资源)。我的问题是,我是否可以简单地调整一些 my.cnf 设置(如 mysqltuner 和/或 phpmyadmin 所建议的那样)以提高效率?

最佳答案

在接下来的十年中,MySQL 不太可能以使其高效使用大约四个以上处理器的方式进行重构。因此,您的配置浪费了 44 个处理器内核。这似乎太多了。此外,看起来 MySQL 使用的内存少于 1GiB。

通常,MySQL 是向外构建的,而不是向上构建的。意思是:大多数扩展基于 MySQL 的应用程序的人都是通过使用数据库的镜像来实现的。 MariaDB 的最新版本具有多主镜像方案。

您遇到麻烦的唯一明显领域是连接流失。您可能应该编辑 my.cnf 文件以增加您使用的同时连接数。

您需要调查您的网络应用程序是否使用连接池。 Web 应用程序需要一段时间才能建立与数据库的连接,如果您为每个 Web 请求重新建立它们,则连接打开操作可能成为瓶颈。您的统计数据显示,一天 24 小时,十二天,每秒有一个以上的新连接。除非你的工作量非常稳定地分布在一天中的所有 24 小时内,否则这意味着你的网络应用程序以非常高的新连接率达到峰值。这可能是您速度变慢的原因。

您没有说明您使用的是哪种网络应用程序平台。大多数平台都有连接池操作。大多数 Web 应用程序使用一组或两组 DBMS 凭据(用户名)来执行所有 Web 应用程序操作,因此它们可以重用池中的连接。您的统计数据表明您在数据库中有 600 多个不同的用户。这对于网络应用来说已经很多了。

另外(这是违反直觉的)有时过多的网络应用程序线程会损害性能。有时为了用户体验,限制线程数,让 Web 服务器对来自客户端的请求进行排队,而不是试图一次执行太多操作,会更好。现代服务器上的 TCP 堆栈允许 SYN 请求的合理深度队列,因此队列不会引人注意。

这是基于您所显示的有限数据的意见:您的瓶颈不是 MySQL。

关于MySQL 数据库性能调优,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604167/

相关文章:

MySqlTuner - 失败 : return code 256

php - Laravel 事件不起作用

php - Codeigniter 从表中删除记录,其中 page_id 和 user_id

oracle - 从 Oracle 数据库中的 sql Developer 生成 sql 跟踪文件

mysql - 如何优化 InnoDB 内存参数以提高性能?

大型 Magento 的 MySql 配置

mysql - 无法将mysql数据从一个数据库转移到另一个数据库

函数外的php foreach

database-administration - 获取 RocksDB 中键值对的总数

mysql - VPS 的最佳 my.cnf 配置是什么