教程:提高MySQL性能

我们最近有一个用户问我们一个相对简单的问题:如何加快我的MySQL查询性能,而不用依靠提升CPU来达到目的?

对于那些运行依赖MySQL数据库的网站或应用程序的人来说,这是一个非常重要的问题,因为它可能意味着平滑的体验与导致您和您的用户沮丧的体验之间的区别。

在本教程中,我们将介绍三种改进MySQL数据库性能的方法,或者首先建立减少查询数据库需求的方法。

要求

一个运行MySQL数据库的服务器(任何操作系统都可以工作)

选项1.Percona的向导

Percona为MySQL 提供了一个免费的配置向导,可以轻松建立基准配置,更好地适应您将要在数据库中抛出的问题。有了坚实的基础工作,您可以快速获得您的服务,然后进一步调整目标,以提高性能。

注意:Percona建议此配置向导仅用于新的服务器/数据库,而不是已经运行并包含数据的服务器/数据库。主要原因是如果将旧配置替换为由此向导创建的配置,则数据库可能无法访问。如果您已经有一个活动的数据库,最好跳到第二个选项。

这个服务器托管在哪里? 其他

你使用虚拟化吗? 其他

你使用什么类型的存储? SSD /闪存

您的系统有多少CPU? 4(除非你是我们最小的计划)

你的操作系统是什么? Linux的

其余的完全依赖于您的特定应用程序,尽管在大多数情况下选择默认值将会为您提供一个良好的启动配置,您将来可以构建它。在完成向导的七个步骤之后,您将得到一个配置,您可以将其放在您或相关服务器上的文件中。 my.cnf my.ini

选项2. mysqltuner.pl

这个流行的脚本扫描您的MySQL数据库,并提供有关漏洞或弱密码的警告,并提供有关哪些配置调整将提高性能的逻辑建议。

要下载脚本,您有两个选项:克隆整个存储库,或者只需要更多的“最小化”安装所需的内容。

克隆:

$ git clone https://github.com/major/MySQLTuner-perl.git
$ cd MySQLTuner-perl

之后,您可以开始脚本了。更多的一点。

最小:

$ mkdir MySQLTuner-perl && cd MySQLTuner-perl
$ wget http://mysqltuner.pl/ -O mysqltuner.pl
$ wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/basic_passwords.txt -O basic_passwords.txt
$ wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/vulnerabilities.csv -O vulnerabilities.csv

在这种情况下,您正在为这些文件创建一个新目录,然后使用下载相关文件,包括脚本本身,易受攻击密码的文本文件以及漏洞列表。 wget mysqltuner.pl

无论您选择哪种方法,现在都可以运行脚本了。

$ perl mysqltuner.pl

您还可以通过以下方式启用CVE漏洞:

$ perl mysqltuner.pl --cvefile=vulnerabilities.csv

什么时候完成运行,它将提供关于如何改进MySQL配置的任何建议,无论是基于安全性,基于卓越的性能。也就是说,重要的一点是,调谐器的开发人员已经包括在项目中。 mysqltuner.pl README

完全了解您对MySQL数据库服务器所做的每项更改都非常重要。如果您不了解脚本输出的部分内容,或者您​​不了解建议,请咨询您信任的知识渊博的DBA或系统管理员。始终测试您的分期环境的变化,并始终记住,一个领域的改进可能会对其他领域的MySQL产生负面影响。

也可以在Docker容器中运行的数据库上运行,如果您遵循我们以前关注WordPress的Docker教程,也许很有用。例如,以下是我在测试服务器上的一个Wordpress数据库中进行的快速扫描的结果。 mysqltuner.pl

$ perl mysqltuner.pl --host 127.0.0.1 --forcemem 8000 --user root --pass ███████████
 >> MySQLTuner 1.7.2 - 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
[--] Performing tests on 127.0.0.1:3306
[OK] Logged in using credentials passed on the command line
[--] Assuming 8000 MB of physical memory
[!!] Assuming 0 MB of swap space (use --forceswap to specify)
[OK] Currently running supported MySQL version 5.7.18
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: stderr(0B)
[!!] Log file stderr doesn't exist
[!!] Log file stderr isn't readable.

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in InnoDB tables: 16K (Tables: 1)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] User 'wordpress@%' has user name as password.
[!!] User 'root@%' hasn't specific host restriction.
[!!] User 'wordpress@%' hasn't specific host restriction.
[--] There are 612 basic passwords in the list.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 32s (19 q [0.594 qps], 12 conn, TX: 43K, RX: 1K)
[--] Reads / Writes: 100% / 0%
[--] Binary logging is disabled
[--] Physical Memory : 7.8G
[--] Max MySQL memory : 338.9M
[--] Other process memory: 202.5M
[--] Total buffers: 169.0M global + 1.1M per thread (151 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 170.1M (2.13% of installed RAM)
[OK] Maximum possible memory usage: 338.9M (4.24% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (0/19)
[OK] Highest usage of available connections: 0% (1/151)
[!!] Aborted connections: 8.33% (1/12)
[!!] Query cache may be disabled by default due to mutex contention.
[!!] Query cache efficiency: 0.0% (0 cached / 8 selects)
[OK] Query cache prunes per day: 0
[OK] No Sort requiring temporary tables
[OK] No joins without indexes
[OK] Temporary tables created on disk: 15% (10 on disk / 66 total)
[OK] Thread cache hit rate: 91% (1 created / 12 connections)
[OK] Table cache hit rate: 93% (102 open / 109 opened)
[OK] Open file limit used: 0% (14/65K)
[OK] Table locks acquired immediately: 100% (102 immediate / 102 locks)

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

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

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.3% (1M used / 8M cache)
[OK] Key buffer size / total MyISAM indexes: 8.0M/43.0K
[!!] Read Key buffer hit rate: 50.0% (6 cached / 3 reads)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/16.0K
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[!!] InnoDB Read buffer efficiency: 82.53% (1176 hits/ 1425 total)
[!!] InnoDB Write Log efficiency: 0% (2 hits/ 0 total)
[OK] InnoDB log waits: 0.00% (0 waits / 2 writes)

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

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

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

-------- RocksDB Metrics ---------------------------------------------------------------------------
[--] RocksDB is disabled.

-------- Spider Metrics ----------------------------------------------------------------------------
[--] Spider is disabled.

-------- Connect Metrics ---------------------------------------------------------------------------
[--] Connect 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:
 Set up a Secure Password for user@host ( SET PASSWORD FOR 'user'@'SpecificDNSorIp' = PASSWORD('secure_password'); )
 Restrict Host for user@% to user@SpecificDNSorIp
 MySQL started within last 24 hours - recommendations may be inaccurate
 Reduce or eliminate unclosed connections and network issues
Variables to adjust:
 query_cache_size (=0)
 query_cache_type (=0)
 query_cache_limit (> 1M, or use smaller result sets)
 innodb_log_file_size * innodb_log_files_in_group should be equal to 1/4 of buffer pool size (=64M) if possible.

正如你所看到的,我有一些安全性改进可以跟进,我可以改变一些变量来提高性能。

选项3. 使用CDN

以前的两个建议一直是提高MySQL查询的性能,但是首先减少查询呢?许多网站,如个人博客或投资组合,实际上并没有经常改变。这意味着应该可以提供站点的静态HTML版本,而不是让每个访问者在每个加载的页面上查询数据库。

CDN是在您的用户和数据库之间放置缓存层的最流行的解决方案。它的全球内容传送网络(CDN)是速度最快的网站之一,这意味着您的网站不仅会加载速度更快,而且还将依赖于您的数据库。另外,如果您的网站发生故障,CDN将尝试提供其缓存的版本,而不是显示错误。

对于WordPress用户,甚至还有一个插件,使这个过程比以往任何时候都更容易。简单地复制CDN在自己的支持页面上组合的过程是没有意义的,所以我们建议您使用WordPress的“ 使用CDN”页面了解更多信息。

对于其他应用程序,还可以使用CDN的免费层作为缓存层。在这种情况下,您需要将您的域名指向CDN的名称服务器,然后将CDN的DNS指向服务器的IP。

附加选项和资源

服务器本身存在其他缓存层(如memcached),而不是像Cloudflare这样的CDN,但是将这些缓存层超出了本教程的范围。

memcached

Redis

mysql5.7调试安装