Mysql

MySQL 突然每個請求需要 2000 毫秒,而不是 200

  • July 23, 2014

更新:終於記得看 munin,而且似乎其中一個硬碟驅動器畢竟是問題的根源,除非我完全讀錯了。這是 munin 輸出:http: //imagizer.imageshack.com/img536/1070/9d5c62.png。我是否認為我的一個 HDD 可能需要更換,或者其他原因是否可以解釋這些 I/O 峰值?



我有一個非常強大的伺服器(四核 i7,32GB RAM)在 LAMP 堆棧上執行 Drupal 7 網站(pixelscrapper.com),使用 Varnish、Memcached、APC、CloudFlare。我們每天獲得大約 20,000 次網頁瀏覽量,對於經過身份驗證的使用者,通常該網站在 3-4 秒內載入,總伺服器時間佔其中大約 0.5 秒,在 php 和 mysql 之間相當平均地分配(大約 0.2 秒在每個請求的 mysql 中)…所有都通過 New Relic 報告(精簡版,所以我看不到數據庫跟踪)。

3-4 秒的頁面載入時間,在 mysql 中只有 0.2 秒,這種情況已經持續了幾個月,一直如此。

幾天前,網站開始載入時間變長,查看 New Relic,我注意到 mysql 現在每個請求大約需要 2 秒,而不是 0.2 秒。這根本沒有對站點進行任何重大更改……只是正常站點使用。

所以我的問題是,什麼可能會突然導致 mysql 每個請求花費 2 秒而不是 0.2 秒,而站點沒有部署任何更改,流量也沒有重大變化?

我最好的直覺是,這可能是由以下原因引起的:

  1. 根據 my.cnf 中的目前記憶體分配,某些表變得太大,或者總 db 變得太大,所以以前在記憶體中的東西現在正在破壞 HDD。
  2. 一些硬體問題,例如其中一個硬碟驅動器的問題導致磁碟訪問緩慢(我在 RAID 中有兩個硬碟驅動器)。

問題是,基於查看 mysqltuner 輸出,記憶體分配似乎沒問題,並且基於初始驅動測試,硬碟似乎沒有損壞……所以……

關於什麼可能導致這種情況發生的任何想法,我應該如何解決這個問題?

這是 mysqltuner 輸出(mysql 執行 98 天):

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.5.28-0ubuntu0.12.04.2-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 88B (Tables: 10)
[--] Data in InnoDB tables: 1G (Tables: 582)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[!!] Total fragmented tables: 148

-------- Performance Metrics -------------------------------------------------
[--] Up for: 98d 2h 0m 34s (1B q [156.130 qps], 14M conn, TX: 3596B, RX: 225B)
[--] Reads / Writes: 72% / 28%
[--] Total buffers: 5.2G global + 3.1M per thread (300 max threads)
[OK] Maximum possible memory usage: 6.1G (19% of installed RAM)
[OK] Slow queries: 0% (32K/1B)
[OK] Highest usage of available connections: 37% (113/300)
[OK] Key buffer size / total MyISAM indexes: 128.0M/2.4M
[OK] Key buffer hit rate: 100.0% (35M cached / 545 reads)
[OK] Query cache efficiency: 71.4% (820M cached / 1B selects)
[!!] Query cache prunes per day: 43794
[OK] Sorts requiring temporary tables: 0% (15K temp sorts / 63M sorts)
[!!] Joins performed without indexes: 85938
[OK] Temporary tables created on disk: 1% (302K on disk / 17M total)
[OK] Thread cache hit rate: 99% (113K created / 14M connections)
[!!] Table cache hit rate: 13% (2K open / 19K opened)
[OK] Open file limit used: 0% (69/33K)
[OK] Table locks acquired immediately: 99% (529M immediate / 529M locks)
[OK] InnoDB data size / buffer pool: 1.5G/4.0G

-------- Recommendations -----------------------------------------------------
General recommendations:
   Run OPTIMIZE TABLE to defragment tables for better performance
   Increasing the query_cache size over 128M may reduce performance
   Adjust your join queries to always utilize indexes
   Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
   query_cache_size (> 1G) [see warning above]
   join_buffer_size (> 512.0K, or always use indexes with joins)
   table_cache (> 16384)

這是我的.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      = 128M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
max_connections        = 300
table_cache     = 16384
max_heap_table_size = 256M
join_buffer_size    = 512K

#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 4M
query_cache_size        = 1024M
#
# * 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 = 3
#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!
#
innodb_file_per_table
innodb_buffer_pool_size = 4G 
innodb_flush_method = O_DIRECT

# This is crucial to avoid checkpointing all the time:
innodb_log_file_size = 512M

# Lock wait timeout. See http://stackoverflow.com/questions/6000336/how-to-debug-lock-wait-timeout-exceeded
innodb_lock_wait_timeout = 120

# * 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/

這是我的慢查詢日誌:


https://gist.github.com/JordanMagnuson/68e3ab30ff9b6ccc5a96

試圖在 sdb 上執行 HDD 檢查,但無法完成……(最初我以為我已經執行了完成的 HDD 檢查,但當我實際測試 sda 時,我錯誤地認為我正在測試 sdb)。

所以在這種情況下,我的問題的答案是檢查 Munin,注意抖動(故障)磁碟,然後更換它。

一切都和現在一樣。

引用自:https://serverfault.com/questions/613690