MySQL 高 CPU 使用率

2021-11-20 00:00:00 cpu-usage mysql

最近我的服务器 CPU 一直很高.

Recently my server CPU has been going very high.

CPU 负载平均为 13.91(1 分钟)11.72(5 分钟)8.01(15 分钟),我的网站流量仅略有增加.

CPU load averages 13.91 (1 min) 11.72 (5 mins) 8.01 (15 mins) and my site has only had a slight increase in traffic.

运行 top 命令后,我看到 MySQL 使用了 160% 的 CPU!

After running a top command, I saw MySQL was using 160% CPU!

最近我一直在优化表,并切换到持久连接.这是否会导致 MySQL 使用大量 CPU?

Recently I've been optimizing tables and I've switched to persistent connections. Could this be causing MySQL to use high amounts of CPU?

推荐答案

首先,我想说您可能想要关闭持久连接,因为它们几乎总是弊大于利.

First I'd say you probably want to turn off persistent connections as they almost always do more harm than good.

其次,我想说您要仔细检查您的 MySQL 用户,以确保任何人都无法从远程服务器进行连接.这也是需要检查的主要安全事项.

Secondly I'd say you want to double check your MySQL users, just to make sure it's not possible for anyone to be connecting from a remote server. This is also a major security thing to check.

第三,我想说你想打开 MySQL 慢查询 记录以关注任何需要很长时间的查询,并使用它来确保您没有任何查询锁定关键表太长时间.

Thirdly I'd say you want to turn on the MySQL Slow Query Log to keep an eye on any queries that are taking a long time, and use that to make sure you don't have any queries locking up key tables for too long.

您可以检查的其他一些事情是在 CPU 负载很高时运行以下查询:

Some other things you can check would be to run the following query while the CPU load is high:

SHOW PROCESSLIST;

这将向您显示当前正在运行或在要运行的队列中的任何查询,查询是什么以及它在做什么(如果查询太长,此命令将截断查询,您可以使用 SHOW FULL PROCESSLIST 查看完整的查询文本).

This will show you any queries that are currently running or in the queue to run, what the query is and what it's doing (this command will truncate the query if it's too long, you can use SHOW FULL PROCESSLIST to see the full query text).

您还需要关注缓冲区大小等内容,表缓存, 查询缓存 和 innodb_buffer_pool_size(如果您使用的是 innodb 表),因为所有这些内存分配都会影响查询性能,从而导致 MySQL 占用 CPU.

You'll also want to keep an eye on things like your buffer sizes, table cache, query cache and innodb_buffer_pool_size (if you're using innodb tables) as all of these memory allocations can have an affect on query performance which can cause MySQL to eat up CPU.

您可能还想通读以下内容,因为它们包含一些很好的信息.

You'll also probably want to give the following a read over as they contain some good information.

  • MySQL 如何使用内存
  • MySQL 系统变量

使用分析器也是一个很好的主意.您可以在需要时打开的功能将显示您的应用程序正在运行哪些查询、是否存在重复查询、查询需要多长时间等等.类似这样的示例是我一直在研究的一个示例PHP Profiler 但有很多.如果您使用的是 Drupal、Joomla 或 Wordpress 等软件,您需要在社区内询问,因为他们可能有可用的模块,让您无需手动集成任何内容即可获取此信息.

It's also a very good idea to use a profiler. Something you can turn on when you want that will show you what queries your application is running, if there's duplicate queries, how long they're taking, etc, etc. An example of something like this is one I've been working on called PHP Profiler but there are many out there. If you're using a piece of software like Drupal, Joomla or Wordpress you'll want to ask around within the community as there's probably modules available for them that allow you to get this information without needing to manually integrate anything.

相关文章