100%MySQL CPU使用

时间:2014-05-06 22:19:47

标签: mysql apache video-streaming cpu-usage

我正在管理一个播放视频的网站,该网站正在运行:

Intel(R)Xeon(R)CPU E31220 @ 3.10GHz

16 GB RAM

2 x 2TB RAID1

1gbit未计量带宽不是问题,因为使用率仅为150-250mbit等。

在这台机器上,我执行以下操作: - apache主持[url] www.domain.com [/ url] - mysql服务器(apt-get install mysql-server)

一切都很顺利,除非现在有200人在任何时候播放视频..所以现在我注意到网站加载速度较慢..我相信这是因为mysql正在使用100%的cpu。下面我贴了我的顶部,还有我的mysql设置..有人可以帮我解决这个问题吗?

我确实意识到在某些时候我需要将mysql服务器自己移动到另一台机器上,但我仍然认为即将发布的当前设置,它甚至会在独立机器上使用100%cpu。所以我认为需要改变设置?或者有人可以指导我此外,我无法降低wait_timeout,因为当我这样做时,它会导致视频转换脚本出错,它会取出视频并转换它们,有时它可能需要一段时间,所以我不知道这是不是问题或什么

my.cnf:
# * Fine Tuning
#
max_allowed_packet      = 16M
thread_stack            = 1M
thread_cache_size       = 50
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
max_connections        = 1000
wait_timeout            = 20000
tmp_table_size          = 500M
max_heap_table_size     = 1000M
table_cache            = 1000
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 4M
query_cache_size        = 64M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries       = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer_size                = 64M

----------------------------------------

     >>  MySQLTuner 1.1.1 - Major Hayden <major@mhtx.net>
     >>  Bug reports, feature requests, and downloads at [url]http://mysqltuner.com/[/url]
     >>  Run with '--help' for additional options and output filtering
    [!!] Successfully authenticated with no password - SECURITY RISK!

-------- General Statistics --------------------------------------------------

    [--] Skipped version check for MySQLTuner script
    [OK] Currently running supported MySQL version 5.5.37-0ubuntu0.13.10.1
    [OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------

    [--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster 
    [--] Data in MyISAM tables: 1G (Tables: 41)
    [--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
    [!!] InnoDB is enabled but isn't being used
    [!!] Total fragmented tables: 4

-------- Security Recommendations  -------------------------------------------

    [OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 22h 23m 46s (5M q [69.809 qps], 1M conn, TX: 8B, RX: 4B)
[--] Reads / Writes: 29% / 71%
[--] Total buffers: 716.0M global + 3.5M per thread (1000 max threads)
[OK] Maximum possible memory usage: 4.1G (26% of installed RAM)
[OK] Slow queries: 0% (2/5M)
[OK] Highest usage of available connections: 77% (775/1000)
[OK] Key buffer size / total MyISAM indexes: 8.0M/119.0M
[OK] Key buffer hit rate: 99.4% (33M cached / 208K reads)
[OK] Query cache efficiency: 67.6% (1M cached / 1M selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 921 sorts)
[!!] Temporary tables created on disk: 28% (244 on disk / 849 total)
[OK] Thread cache hit rate: 98% (17K created / 1M connections)
[OK] Table cache hit rate: 62% (428 open / 681 opened)
[OK] Open file limit used: 9% (476/5K)
[!!] Table locks acquired immediately: 55%

-------- Recommendations -----------------------------------------------------
General recommendations:
    Add skip-innodb to MySQL configuration to disable InnoDB
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Optimize queries and/or use InnoDB to reduce lock wait
-------------------------------------------------------

它说运行优化表,但我该怎么做?在网络上,我试图搜索并发现运行mysql check命令,但我得到

# mysqlcheck -u root -p --auto-repair --check --optimize --all-databases
Error:  mysqlcheck doesn't support multiple contradicting commands.

此外,仅优化数据库是否安全?它不会伤害它或任何东西?当然先生病了吗

我现在启用了慢查询日志,但到目前为止还没有...

有一次,Mysqltuner说我已经达到了996/1000个连接..但是当我去提高max_connections到2000然后我重新启动mysql服务器时,网站变得比之前更慢..也许我应该不是重新启动mysql服务器而只是全局调整?

top - 23:27:42 up 11 days,  5:28,  3 users,  load average: 2.41, 4.40, 5.97
Tasks: 269 total,   3 running, 265 sleeping,   0 stopped,   1 zombie
%Cpu(s): 24.3 us,  5.6 sy,  0.0 ni, 69.3 id,  0.3 wa,  0.0 hi,  0.5 si,  0.0 st
KiB Mem:  16408692 total, 16237624 used,   171068 free,     9552 buffers
KiB Swap: 15624184 total,     8220 used, 15615964 free, 15072644 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                               
29178 mysql     20   0 2662m  73m 7280 S 100.4  0.5   4:49.03 /usr/sbin/mysqld                                                      
29428 daemon    20   0  544m  19m 7048 S   2.7  0.1   0:07.81 /usr/sbin/apache2 -k start                                            
29943 daemon    20   0  543m  17m 5428 S   2.0  0.1   0:02.57 /usr/sbin/apache2 -k start                                            
29945 daemon    20   0  543m  17m 5352 S   2.0  0.1   0:00.76 /usr/sbin/apache2 -k start                                            
29672 daemon    20   0  543m  16m 4696 S   1.7  0.1   0:00.63 /usr/sbin/apache2 -k start                                            
29792 daemon    20   0  544m  17m 5396 S   1.3  0.1   0:00.15 /usr/sbin/apache2 -k start                                            
  183 root      20   0     0    0    0 S   1.0  0.0   8:51.20 [md0_raid1]                                                           
29445 daemon    20   0  543m  16m 4700 S   0.7  0.1   0:01.04 /usr/sbin/apache2 -k start                                            
29744 daemon    20   0  543m  16m 5384 S   0.7  0.1   0:00.23 /usr/sbin/apache2 -k start                                            
 7705 root      35  15     0    0    0 D   0.3  0.0   2:27.65 [md0_resync]                                                          
29435 daemon    20   0  543m  17m 5596 S   0.3  0.1   0:00.37 /usr/sbin/apache2 -k start                                            
29451 daemon    20   0  543m  17m 5356 S   0.3  0.1   0:00.44 /usr/sbin/apache2 -k start                                            
29453 daemon    20   0     0    0    0 Z   0.3  0.0   0:00.07 [apache2] <defunct>                                                   
29501 daemon    20   0  543m  16m 5324 S   0.3  0.1   0:00.10 /usr/sbin/apache2 -k start                                            
29518 daemon    20   0  543m  17m 5948 S   0.3  0.1   0:00.31 /usr/sbin/apache2 -k start                                            
29534 daemon    20   0  543m  17m 5456 S   0.3  0.1   0:00.42 /usr/sbin/apache2 -k start                                            
29539 daemon    20   0  543m  16m 5348 S   0.3  0.1   0:00.24 /usr/sbin/apache2 -k start                                            
29542 daemon    20   0  543m  16m 4680 S   0.3  0.1   0:00.51 /usr/sbin/apache2 -k start                                            
29549 daemon    20   0  543m  17m 5352 S   0.3  0.1   0:00.90 /usr/sbin/apache2 -k start                                            
29656 daemon    20   0  543m  16m 4792 S   0.3  0.1   0:00.25 /usr/sbin/apache2 -k start                                            
29673 daemon    20   0  543m  16m 5392 R   0.3  0.1   0:00.20 /usr/sbin/apache2 -k start                                            
29682 daemon    20   0  543m  16m 4704 S   0.3  0.1   0:00.58 /usr/sbin/apache2 -k start                                            
29791 daemon    20   0  543m  17m 5392 S   0.3  0.1   0:00.81 /usr/sbin/apache2 -k start                                            
29793 daemon    20   0  543m  17m 5712 S   0.3  0.1   0:00.12 /usr/sbin/apache2 -k start                                            
29926 daemon    20   0  543m  16m 4704 S   0.3  0.1   0:00.44 /usr/sbin/apache2 -k start                                            
29956 daemon    20   0  543m  16m 4700 S   0.3  0.1   0:00.33 /usr/sbin/apache2 -k start    

=====================================


# free -tm
             total       used       free     shared    buffers     cached
Mem:         16024      15861        162          0          4      14351
-/+ buffers/cache:       1506      14517
Swap:        15257          8      15249
Total:       31282      15870      15411

其他问题: 如果我将mysql移动到自己的机器上,硬盘应该是ssds吗?对于mysql更好吗?而32克的公羊就足够了

数据库中最重要的是会话表到目前为止可以达到1.5 gb ..但我清除它并且数据库缩小到100mb以下

1 个答案:

答案 0 :(得分:0)

我相信你不能同时--check--optimize,因为--check表示&#34;只是检查,不要碰任何事情&#34;。

MySQL使用大量CPU是不正常的,因为它的主要目标是存储和获取信息,而不是处理它。它应该使用大量内存,实际上允许MySQL使用尽可能多的内存是件好事。毕竟,未使用的内存是浪费内存。我看到MySQL使用0.5%。这显然是一个不好的迹象。调整查询缓存。

您可能在您的网站中过度使用SQL查询,但如果我不得不猜测我相信您的问题可能是您的表格中缺少正确的索引。

当您执行SELECT * FROM parent, child WHERE child.parent_id = parent.id之类的操作时,parent.idPRIMARY KEY非常重要,以便让MySQL更有效地执行任务。