【问题标题】:Apache CPU 100% when query DB查询数据库时 Apache CPU 100%
【发布时间】:2013-12-19 11:07:30
【问题描述】:

我有一个 Ubuntu 服务器和 10 个具有不同框架/CMS 的站点。

我的问题是,当我打开一个带有与 MySQL DB 集成的大型 CMS 的站点时,Apache2 使用 100% CPU,在这些站点中,我的页面速度在 10 到 20 秒之间(呈现 HTML 页面),我真的不不知道为什么。 (但所有没有 MySQL 连接的 PHP 框架都可以正常工作)。

通过我的服务器支持经理,我们看到我的服务器上一切正常(没有 I/O 问题或其他问题),我们认为 Apache / MySQL 配置存在问题。

我还没有崩溃 DB 表并优化了所有的 innodb 表。

这是我从浏览器加载这个慢速 CMS 时的top 快照:

PID USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
13307 www-data  20   0 90116  42m  22m R  100  4.2   0:15.23 apache2
2224 root      20   0 37932 8148 1724 S    8  0.8 212:21.99 newrelic-daemon
13422 mysql     20   0  186m  47m 6280 S    0  4.7   0:11.20 mysqld
13889 root      20   0  2640 1132  860 R    0  0.1   0:00.03 top
1 root      20   0  2796 1124  792 S    0  0.1   0:06.28 init

Newrelic 截图问题(不仅是 wordpress,而且是所有的 CMS):Newrelic screenshot

我的 CPU 信息:

$ head /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
stepping        : 7
cpu MHz         : 2600.086
cache size      : 20480 KB
fdiv_bug        : no
hlt_bug         : no

我的记忆信息:

$ head /proc/meminfo
MemTotal:        1028476 kB
MemFree:          571424 kB
Buffers:           17920 kB
Cached:           273312 kB
SwapCached:          824 kB
Active:           232768 kB
Inactive:         180264 kB
Active(anon):     147336 kB
Inactive(anon):     5292 kB
Active(file):      85432 kB

我的 Apache 配置:

LockFile ${APACHE_LOCK_DIR}/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
    StartServers          2
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients           50
    MaxRequestsPerChild   0
    </IfModule>
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
    </IfModule>
<IfModule mpm_event_module>
    StartServers          2
    MinSpareThreads       5
    MaxSpareThreads      10 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients           50
    MaxRequestsPerChild   0
    </IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
    </Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
Include mods-enabled/*.load
Include mods-enabled/*.conf
Include httpd.conf
Include ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
Include conf.d/
Include sites-enabled/

MySQL 配置:

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
skip-external-locking
key_buffer      = 32M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 286
interactive_timeout = 25
wait_timeout = 1000
myisam-recover         = BACKUP
myisam_sort_buffer_size = 32MB
max_connections        = 400
read_buffer_size = 1M
sort_buffer_size = 2M
table_cache            = 400
thread_concurrency     = 10
query_cache_limit   = 64M
query_cache_size        = 32M
general_log_file        = /var/log/mysql/mysql.log

log_error                = /var/log/mysql/error.log

log_slow_queries    = /var/log/mysql/mysql-slow.log
long_query_time = 4 
log-queries-not-using-indexes
log_bin         = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
bind-address        = 127.0.0.1

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]

[isamchk]
key_buffer      = 32M

!includedir /etc/mysql/conf.d/

如何解决这个问题?

【问题讨论】:

  • 首先尝试在你的sql引擎上找到问题。调试或记录运行缓慢的查询。

标签: mysql performance apache cpu


【解决方案1】:

您的 Apache 配置有很多问题 - 但这可能与您当前遇到的问题无关,就像您的 DBMS 的状态一样 - 根据您提供的信息,它是 apache 出了问题。

您没有告诉我们 Apache 正在使用哪个 MPM(这对于理解问题至关重要),也没有告诉我们哪个 CMS。

我说过你忽略的信息只是冰山一角 - 调查和解决性能问题确实非常复杂 - 可能需要几个月(其中大部分时间用于收集信息) - 你非常不太可能在这里得到明确的答案。

【讨论】:

    【解决方案2】:

    您需要放置更多信息。例如,如果您尝试在 wordpress 中显示 1000000 个帖子,特别是如果您在单独的查询中获取此帖子的属性,您可以获得 100% 的 CPU 使用率。

    尝试找到一个导致此问题的脚本,监控您的 mysql(显示进程列表)在高使用时刻等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      相关资源
      最近更新 更多