MySQL的CPU占用率过高(只发生一个更大的分贝> 100MB)

MySQL的CPU占用率过高(只发生一个更大的分贝> 100MB)

问题描述:

我在寻找3天已经并不能找到答案,为什么我的MySQL服务器使用如此多的CPU。 例如,当我从表中的一个运行查询来SELECT像100行中,CPU从0到50%,或当我取的值从所述表中的一个,并INSERT一个行中的另一个表,则CPU从0到10-30%不等。目前,它使用170MB,我尝试了一个干净的,并且CPU一直低于1%。查询速度并不慢,他们在0.05秒内完成工作,但仍使用约30%的CPU。如果我在网站上有超过30人,并且他们都这样做,那么网站的速度会变得非常糟糕。直到前一段时间才发生这种情况,所以也许我在项目工作的某个地方犯了错误。MySQL的CPU占用率过高(只发生一个更大的分贝> 100MB)

这里是MySQLTuner的结果:

>> MySQLTuner 1.7.0 - Major Hayden <[email protected]> 
>> 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] Logged in using credentials from debian maintenance account. 
[OK] Currently running supported MySQL version 5.5.52-0+deb7u1 
[OK] Operating on 64-bit architecture 

-------- Storage Engine Statistics ---------------------------------------------       -------------------- 
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +My       ISAM +PERFORMANCE_SCHEMA 
[--] Data in MyISAM tables: 23K (Tables: 10) 
[--] Data in InnoDB tables: 218M (Tables: 18) 
[OK] Total fragmented tables: 0 

-------- Security Recommendations ----------------------------------------------       -------------------- 
[OK] There are no anonymous accounts for any database users 
[OK] All database users have passwords assigned 
[--] There are 612 basic passwords in the list. 

-------- CVE Security Recommendations ------------------------------------------       -------------------- 
[!!] CVE-2016-6662(<= 5.5.52) : "Oracle MySQL through 5.5.52 
[--] False positive CVE(s) for MySQL and MariaDB 5.5.x can be found. 
[--] Check careful each CVE for those particular versions 
[!!] 1 CVE(s) found for your MySQL release. 

-------- Performance Metrics ----------------------------------------------------------------------- 
[--] Up for: 13m 39s (2K q [2.452 qps], 434 conn, TX: 925K, RX: 146K) 
[--] Reads/Writes: 70%/30% 
[--] Binary logging is disabled 
[--] Physical Memory  : 3.8G 
[--] Max MySQL memory : 597.8M 
[--] Other process memory: 63.7M 
[--] Total buffers: 192.0M global + 2.7M per thread (151 max threads) 
[--] P_S Max memory usage: 0B 
[--] Galera GCache Max memory usage: 0B 
[OK] Maximum reached memory usage: 232.3M (6.00% of installed RAM) 
[OK] Maximum possible memory usage: 597.8M (15.44% of installed RAM) 
[OK] Overall possible memory usage with other process is compatible with memory available 
[OK] Slow queries: 0% (0/2K) 
[OK] Highest usage of available connections: 9% (15/151) 
[OK] Aborted connections: 0.00% (0/434) 
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance 
[!!] Query cache may be disabled by default due to mutex contention. 
[OK] Sorts requiring temporary tables: 0% (0 temp sorts/5 sorts) 
[OK] No joins without indexes 
[OK] Temporary tables created on disk: 25% (69 on disk/273 total) 
[OK] Thread cache hit rate: 96% (15 created/434 connections) 
[OK] Table cache hit rate: 91% (72 open/79 opened) 
[OK] Open file limit used: 6% (68/1K) 
[OK] Table locks acquired immediately: 100% (224 immediate/224 locks) 

-------- Performance schema ------------------------------------------------------------------------ 
[--] Performance schema is disabled. 

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

-------- MyISAM Metrics ---------------------------------------------------------------------------- 
[!!] Key buffer used: 18.2% (3M used/16M cache) 
[OK] Key buffer size/total MyISAM indexes: 16.0M/124.0K 

-------- InnoDB Metrics ---------------------------------------------------------------------------- 
[--] InnoDB is enabled. 
[--] InnoDB Thread Cucurrency: 0 
[!!] InnoDB File per table is not activated 
[!!] InnoDB buffer pool/data size: 128.0M/218.7M 
[!!] Ratio InnoDB log file size/InnoDB Buffer pool size (3.90625 %): 5.0M/128.0M should be equal 25% 
[OK] InnoDB buffer pool instances: 1 
[--] InnoDB Buffer Pool Chunk Size not used or defined in your version 
[OK] InnoDB Read buffer efficiency: 99.98% (4252997 hits/ 4253965 total) 
[!!] InnoDB Write Log efficiency: 17.98% (16 hits/ 89 total) 
[OK] InnoDB log waits: 0.00% (0 waits/73 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: 
    1 CVE(s) found for your MySQL release. Consider upgrading your version ! 
    MySQL started within last 24 hours - recommendations may be inaccurate 
    Enable the slow query log to troubleshoot bad queries 
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1 
Variables to adjust: 
    query_cache_type (=0) 
    innodb_file_per_table=ON 
    innodb_buffer_pool_size (>= 218M) if possible. 
    innodb_log_file_size should be equals to 1/4 of buffer pool size (=32M) if possible. 

而这里的my.cnf:

# 
# The MySQL database server configuration file. 
# 
# You can copy this to one of: 
# - "/etc/mysql/my.cnf" to set global options, 
# - "~/.my.cnf" to set user-specific options. 
# 
# One can use all long options that the program supports. 
# Run program with --help to get a list of available options and with 
# --print-defaults to see which it would actually understand and use. 
# 
# For explanations see 
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html 

# This will be passed to all mysql clients 
# It has been reported that passwords should be enclosed with ticks/quotes 
# escpecially if they contain "#" chars... 
# Remember to edit /etc/mysql/debian.cnf when changing the socket location. 
[client] 
port  = 3306 
socket  = /var/run/mysqld/mysqld.sock 

# Here is entries for some specific programs 
# The following values assume you have at least 32M ram 

# This was formally known as [safe_mysqld]. Both versions are currently parsed. 
[mysqld_safe] 
socket  = /var/run/mysqld/mysqld.sock 
nice  = 0 

[mysqld] 
# 
# * Basic Settings 
# 
user  = mysql 
pid-file = /var/run/mysqld/mysqld.pid 
socket  = /var/run/mysqld/mysqld.sock 
port  = 3306 
basedir  = /usr 
datadir  = /var/lib/mysql 
tmpdir  = /tmp 
lc-messages-dir = /usr/share/mysql 
skip-external-locking 
# 
# Instead of skip-networking the default is now to listen only on 
# localhost which is more compatible and is not less secure. 
bind-address  = 127.0.0.1 
# 
# * Fine Tuning 
# 
key_buffer  = 16M 
max_allowed_packet = 16M 
thread_stack  = 192K 
thread_cache_size  = 8 
wait_timeout = 1800 
# This replaces the startup script and checks MyISAM tables if needed 
# the first time they are touched 
myisam-recover   = BACKUP 
#max_connections  = 100 
#table_cache   = 64 
#thread_concurrency  = 10 
# 
# * Query Cache Configuration 
# 
query_cache_limit = 1M 
query_cache_size  = 16M 
# 
# * 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 logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf. 
# 
# 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  = 16M 

# 
# * IMPORTANT: Additional settings that can override those from this file! 
# The files must end with '.cnf', otherwise they'll be ignored. 
# 
!includedir /etc/mysql/conf.d/ 

我也查了慢查询日志,但是什么也没有足够慢去那里。我只在users -table的一列中使用一个索引。这加快了一些查询的速度,但是,MySQL的CPU使用率与没有它一样。

+0

你可以给每个表的行数和如果你使用索引的指标? – Simon

+0

您忘记了添加您的查询并解释了 – e4c5

+0

只是一个WAG。检查导致抖动的数据模式。这种颠簸可能发生在多个位置:数据库缓存,TLB(翻译后备缓冲区),内存和磁盘之间等。使用分析器检查任何抖动的原因。看看数据访问模式,看看你是否可以修改它们以避免这种颠簸。 –

答案是我没有任何表中的索引。索引表后,cpu不会超过5%。