Mysql佔用CPU太高如何優化php
一次生產DB服務器的 超負荷運行問題解決:html
1.查看生產DB服務器top列表,mysql
執行 top 命令 sql
查看Cpu(s) 參數一直處於 98% 狀態 ,load average達到了 5 (4核服務器)數據庫
可見DB已經超負荷運行了緩存
2.使用root用戶登陸mysql 服務器
執行 show full processlist 查看慢查詢,反覆執行,發現一直有3個select 查詢語句存在,爲了緩解DB服務器壓力,直接使用kill命令殺掉less
kill 慢查詢的id工具
服務器壓力緩解,恢復正常。oop
使用這條SQL語句去項目中找到對應功能,經查是沒用的功能,直接隱藏這個功能,問題解決。
Mysql佔用CPU太高如何優化?
mysql show processlist + show status + kill Id命令詳解
1. SHOW PROCESSLIST
顯示哪些線程正在運行。您也可使用mysqladmin processlist語句獲得此信息。若是您有SUPER權限,您能夠看到全部線程。不然,您只能看到您本身的線程(也就是,與您正在使用的MySQL 帳戶相關的線程)。請參見13.5.5.3節,「KILL語法」。若是您不使用FULL關鍵詞,則只顯示每一個查詢的前100個字符。
本語句報告TCP/IP鏈接的主機名稱(採用host_name:client_port格式),以方便地斷定哪一個客戶端正在作什麼。
若是您獲得「too many connections」錯誤信息,而且想要了解正在發生的狀況,本語句是很是有用的。MySQL保留一個額外的鏈接,讓擁有SUPER權限的 帳戶使用,以確保管理員可以隨時鏈接和檢查系統(假設您沒有把此權限給予全部的用戶)。
這個命令中最關鍵的就是state列,mysql列出的狀態主要有如下幾種:
Checking table
正在檢查數據表(這是自動的)。
Closing tables
正在將表中修改的數據刷新到磁盤中,同時正在關閉已經用完的表。這是一個很快的操做,若是不是這樣的話,就應該確認磁盤空間是否已經滿了或者磁盤是否正處於重負中。
Connect Out
複製從服務器正在鏈接主服務器。
Copying to tmp table on disk
因爲臨時結果集大於tmp_table_size,正在將臨時表從內存存儲轉爲磁盤存儲以此節省內存。
Creating tmp table
正在建立臨時表以存放部分查詢結果。
deleting from main table
服務器正在執行多表刪除中的第一部分,剛刪除第一個表。
deleting from reference tables
服務器正在執行多表刪除中的第二部分,正在刪除其餘表的記錄。
Flushing tables
正在執行FLUSH TABLES,等待其餘線程關閉數據表。
Killed
發送了一個kill請求給某線程,那麼這個線程將會檢查kill標誌位,同時會放棄下一個kill請求。MySQL會在每次的主循環中檢查 kill標誌位,不過有些狀況下該線程可能會過一小段才能死掉。若是該線程程被其餘線程鎖住了,那麼kill請求會在鎖釋放時立刻生效。
Locked
被其餘查詢鎖住了。
Sending data
正在處理SELECT查詢的記錄,同時正在把結果發送給客戶端。
Sorting for group
正在爲GROUP BY作排序。
Sorting for order
正在爲ORDER BY作排序。
Opening tables
這個過程應該會很快,除非受到其餘因素的干擾。例如,在執ALTER TABLE或LOCK TABLE語句行完之前,數據表沒法被其餘線程打開。正嘗試打開一個表。
Removing duplicates
正在執行一個SELECT DISTINCT方式的查詢,可是MySQL沒法在前一個階段優化掉那些重複的記錄。所以,MySQL須要再次去掉重複的記錄,而後再把結果發送給客戶端。
Reopen table
得到了對一個表的鎖,可是必須在表結構修改以後才能得到這個鎖。已經釋放鎖,關閉數據表,正嘗試從新打開數據表。
Repair by sorting
修復指令正在排序以建立索引。
Repair with keycache
修復指令正在利用索引緩存一個一個地建立新索引。它會比Repair by sorting慢些。
Searching rows for update
正在講符合條件的記錄找出來以備更新。它必須在UPDATE要修改相關的記錄以前就完成了。
Sleeping
正在等待客戶端發送新請求.
System lock
正在等待取得一個外部的系統鎖。若是當前沒有運行多個mysqld服務器同時請求同一個表,那麼能夠經過增長--skip-external-locking參數來禁止外部系統鎖。
Upgrading lock
INSERT DELAYED正在嘗試取得一個鎖表以插入新記錄。
Updating
正在搜索匹配的記錄,而且修改它們。
User Lock
正在等待GET_LOCK()。
Waiting for tables
該線程獲得通知,數據表結構已經被修改了,須要從新打開數據表以取得新的結構。而後,爲了能的從新打開數據表,必須等到全部其餘線程關閉這個 表。如下幾種狀況下會產生這個通知:FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE,或OPTIMIZE TABLE。
waiting for handler insert
INSERT DELAYED已經處理完了全部待處理的插入操做,正在等待新的請求。
大部分狀態對應很快的操做,只要有一個線程保持同一個狀態好幾秒鐘,那麼多是有問題發生了,須要檢查一下。
還有其餘的狀態沒在上面中列出來,不過它們大部分只是在查看服務器是否有存在錯誤是才用得着。
mysql 查看當前鏈接數
命令: show processlist;
若是是root賬號,你能看到全部用戶的當前鏈接。若是是其它普通賬號,只能看到本身佔用的鏈接。
show processlist;只列出前100條,若是想全列出請使用show full processlist;
mysql> show processlist;
2. show status
Aborted_clients 因爲客戶沒有正確關閉鏈接已經死掉,已經放棄的鏈接數量。
Aborted_connects 嘗試已經失敗的MySQL服務器的鏈接的次數。
Connections 試圖鏈接MySQL服務器的次數。
Created_tmp_tables 當執行語句時,已經被創造了的隱含臨時表的數量。
Delayed_insert_threads 正在使用的延遲插入處理器線程的數量。
Delayed_writes 用INSERT DELAYED寫入的行數。
Delayed_errors 用INSERT DELAYED寫入的發生某些錯誤(可能重複鍵值)的行數。
Flush_commands 執行FLUSH命令的次數。
Handler_delete 請求從一張表中刪除行的次數。
Handler_read_first 請求讀入表中第一行的次數。
Handler_read_key 請求數字基於鍵讀行。
Handler_read_next 請求讀入基於一個鍵的一行的次數。
Handler_read_rnd 請求讀入基於一個固定位置的一行的次數。
Handler_update 請求更新表中一行的次數。
Handler_write 請求向表中插入一行的次數。
Key_blocks_used 用於關鍵字緩存的塊的數量。
Key_read_requests 請求從緩存讀入一個鍵值的次數。
Key_reads 從磁盤物理讀入一個鍵值的次數。
Key_write_requests 請求將一個關鍵字塊寫入緩存次數。
Key_writes 將一個鍵值塊物理寫入磁盤的次數。
Max_used_connections 同時使用的鏈接的最大數目。
Not_flushed_key_blocks 在鍵緩存中已經改變可是還沒被清空到磁盤上的鍵塊。
Not_flushed_delayed_rows 在INSERT DELAY隊列中等待寫入的行的數量。
Open_tables 打開表的數量。
Open_files 打開文件的數量。
Open_streams 打開流的數量(主要用於日誌記載)
Opened_tables 已經打開的表的數量。
Questions 發往服務器的查詢的數量。
Slow_queries 要花超過long_query_time時間的查詢數量。
Threads_connected 當前打開的鏈接的數量。
Threads_running 不在睡眠的線程數量。
Uptime 服務器工做了多少秒。
After create
This occurs when the thread creates a table (including internal temporary tables), at the end of the function that creates the table. This state is used even if the table could not be created due to some error.
Analyzing
The thread is calculating a MyISAM table key distributions (for example, for ANALYZE TABLE).
checking permissions
The thread is checking whether the server has the required privileges to execute the statement.
Checking table
The thread is performing a table check operation.
cleaning up
The thread has processed one command and is preparing to free memory and reset certain state variables.
closing tables
The thread is flushing the changed table data to disk and closing the used tables. This should be a fast operation. If not, you should verify that you do not have a full disk and that the disk is not in very heavy use.
converting HEAP to MyISAM
The thread is converting an internal temporary table from a MEMORY table to an on-disk MyISAM table.
copy to tmp table
The thread is processing an ALTER TABLE statement. This state occurs after the table with the new structure has been created but before rows are copied into it.
Copying to group table
If a statement has different ORDER BY and GROUP BY criteria, the rows are sorted by group and copied to a temporary table.
Copying to tmp table
The server is copying to a temporary table in memory.
Copying to tmp table on disk
The server is copying to a temporary table on disk. The temporary result set was larger than tmp_table_size and the thread is changing the temporary table from in-memory to disk-based format to save memory.
Creating index
The thread is processing ALTER TABLE ... ENABLE KEYS for a MyISAM table.
Creating sort index
The thread is processing a SELECT that is resolved using an internal temporary table.
creating table
The thread is creating a table. This includes creation of temporary tables.
Creating tmp table
The thread is creating a temporary table in memory or on disk. If the table is created in memory but later is converted to an on-disk table, the state during that operation will be Copying to tmp table on disk.
deleting from main table
The server is executing the first part of a multiple-table delete. It is deleting only from the first table, and saving columns and offsets to be used for deleting from the other (reference) tables.
deleting from reference tables
The server is executing the second part of a multiple-table delete and deleting the matched rows from the other tables.
discard_or_import_tablespace
The thread is processing an ALTER TABLE ... DISCARD TABLESPACE or ALTER TABLE ... IMPORT TABLESPACE statement.
end
This occurs at the end but before the cleanup of ALTER TABLE, CREATE VIEW, DELETE, INSERT, SELECT, or UPDATE statements.
executing
The thread has begun executing a statement.
Execution of init_command
The thread is executing statements in the value of the init_command system variable.
freeing items
The thread has executed a command. This state is usually followed by cleaning up.
Flushing tables
The thread is executing FLUSH TABLES and is waiting for all threads to close their tables.
FULLTEXT initialization
The server is preparing to perform a natural-language full-text search.
init
This occurs before the initialization of ALTER TABLE, DELETE, INSERT, SELECT, or UPDATE statements.
Killed
Someone has sent a KILL statement to the thread and it should abort next time it checks the kill flag. The flag is checked in each major loop in MySQL, but in some cases it might still take a short time for the thread to die. If the thread is locked by some other thread, the kill takes effect as soon as the other thread releases its lock.
Locked
The query is locked by another query.
logging slow query
The thread is writing a statement to the slow-query log.
NULL
This state is used for the SHOW PROCESSLIST state.
login
The initial state for a connection thread until the client has been authenticated successfully.
Opening tables, Opening table
The thread is trying to open a table. This is should be very fast procedure, unless something prevents opening. For example, an ALTER TABLE or a LOCK TABLE statement can prevent opening a table until the statement is finished.
preparing
This state occurs during query optimization.
Purging old relay logs
The thread is removing unneeded relay log files.
query end
This state occurs after processing a query but before the freeing items state.
Reading from net
The server is reading a packet from the network.
Removing duplicates
The query was using SELECT DISTINCT in such a way that MySQL could not optimize away the distinct operation at an early stage. Because of this, MySQL requires an extra stage to remove all duplicated rows before sending the result to the client.
removing tmp table
The thread is removing an internal temporary table after processing a SELECT statement. This state is not used if no temporary table was created.
rename
The thread is renaming a table.
rename result table
The thread is processing an ALTER TABLE statement, has created the new table, and is renaming it to replace the original table.
Reopen tables
The thread got a lock for the table, but noticed after getting the lock that the underlying table structure changed. It has freed the lock, closed the table, and is trying to reopen it.
Repair by sorting
The repair code is using a sort to create indexes.
Repair done
The thread has completed a multi-threaded repair for a MyISAM table.
Repair with keycache
The repair code is using creating keys one by one through the key cache. This is much slower than Repair by sorting.
Rolling back
The thread is rolling back a transaction.
Saving state
For MyISAM table operations such as repair or analysis, the thread is saving the new table state to the .MYI file header. State includes information such as number of rows, the AUTO_INCREMENT counter, and key distributions.
Searching rows for update
The thread is doing a first phase to find all matching rows before updating them. This has to be done if the UPDATE is changing the index that is used to find the involved rows.
Sending data
The thread is processing rows for a SELECT statement and also is sending data to the client.
setup
The thread is beginning an ALTER TABLE operation.
Sorting for group
The thread is doing a sort to satisfy a GROUP BY.
Sorting for order
The thread is doing a sort to satisfy a ORDER BY.
Sorting index
The thread is sorting index pages for more efficient access during a MyISAM table optimization operation.
Sorting result
For a SELECT statement, this is similar to Creating sort index, but for nontemporary tables.
statistics
The server is calculating statistics to develop a query execution plan.
System lock
The thread is going to request or is waiting for an internal or external system lock for the table. If this state is being caused by requests for external locks and you are not using multiple mysqld servers that are accessing the same tables, you can disable external system locks with the --skip-external-locking option. However, external locking is disabled by default, so it is likely that this option will have no effect. For SHOW PROFILE, this state means the thread is requesting the lock (not waiting for it).
Table lock
The next thread state after System lock. The thread has acquired an external lock and is going to request an internal table lock.
Updating
The thread is searching for rows to update and is updating them.
updating main table
The server is executing the first part of a multiple-table update. It is updating only the first table, and saving columns and offsets to be used for updating the other (reference) tables.
updating reference tables
The server is executing the second part of a multiple-table update and updating the matched rows from the other tables.
User lock
The thread is going to request or is waiting for an advisory lock requested with a GET_LOCK() call. For SHOW PROFILE, this state means the thread is requesting the lock (not waiting for it).
Waiting for tables, Waiting for table
The thread got a notification that the underlying structure for a table has changed and it needs to reopen the table to get the new structure. However, to reopen the table, it must wait until all other threads have closed the table in question.
This notification takes place if another thread has used FLUSH TABLES or one of the following statements on the table in question: FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, or OPTIMIZE TABLE.
Waiting on cond
A generic state in which the thread is waiting for a condition to become true. No specific state information is available.
Writing to net
The server is writing a packet to the network.
3. kill mysql 執行線程
(1) show processlist;
(2) kill 152;
(3) show preocesslist;
+-----+------+--------------------+-------------+---------+------+-------------------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+--------------------+-------------+---------+------+-------------------+------------------------------------------------------------------------------------------------------+
| 155 | root | 192.168.0.100:1944 | atomic_data | Query | 4840 | copy to tmp table | alter table `atomic_data`.`basic_point_beijing_p1`
change `valid` `type` int(2) NULL ,
chang |
| 158 | root | localhost | atomic_data | Query | 0 | NULL | show processlist
如下內容來自mysql手冊:
13.5.5.3. KILL語法
KILL [CONNECTION | QUERY] thread_id
每一個與mysqld的鏈接都在一個獨立的線程裏運行,您可使用SHOW PROCESSLIST語句查看哪些線程正在運行,並使用KILL thread_id語句終止一個線程。
KILL容許自選的CONNECTION或QUERY修改符:
· KILL CONNECTION與不含修改符的KILL同樣:它會終止與給定的thread_id有關的鏈接。
· KILL QUERY會終止鏈接當前正在執行的語句,可是會保持鏈接的原狀。
若是您擁有PROCESS權限,則您能夠查看全部線程。若是您擁有SUPER權限,您能夠終止全部線程和語句。不然,您只能查看和終止您本身的線程和語句。
您也可使用mysqladmin processlist和mysqladmin kill命令來檢查和終止線程。
註釋:您不能同時使用KILL和Embedded MySQL Server庫,由於內植的服務器只運行主機應用程序的線程。它不能建立任何自身的鏈接線程。
當您進行一個KILL時,對線程設置一個特有的終止標記。在多數狀況下,線程終止可能要花一些時間,這是由於終止標記只會在在特定的間隔被檢查:
· 在SELECT, ORDER BY和GROUP BY循環中,在讀取一組行後檢查標記。若是設置了終止標記,則該語句被放棄。
· 在ALTER TABLE過程當中,在每組行從原來的表中被讀取前,檢查終止標記。若是設置了終止標記,則語句被放棄,臨時表被刪除。
· 在UPDATE或DELETE運行期間,在每一個組讀取以後以及每一個已更行或已刪除的行以後,檢查終止標記。若是終止標記被設置,則該語句被放棄。注意,若是您正在使用事務,則變動不會被 回滾。
· GET_LOCK()會放棄和返回NULL。
· INSERT DELAYED線程會快速地刷新(插入)它在存儲器中的全部的行,而後終止。
· 若是線程在表鎖定管理程序中(狀態:鎖定),則表鎖定被快速地放棄。
· 若是在寫入調用中,線程正在等待空閒的磁盤空間,則寫入被放棄,並伴隨"disk full"錯誤消息。
· 警告:對MyISAM表終止一個REPAIR TABLE或OPTIMIZE TABLE操做會致使出現一個被損壞的沒有用的表。對這樣的表的任何讀取或寫入都會失敗,直到您再次優化或修復它(不中斷)。
一、經過information_schema.processlist表中的鏈接信息生成須要處理掉的MySQL鏈接的語句臨時文件,而後執行臨時文件中生成的指令
mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root'; +------------------------+ | concat('KILL ',id,';') +------------------------+ | KILL 3101; | KILL 2946; +------------------------+ 2 rows in set (0.00 sec) mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt'; Query OK, 2 rows affected (0.00 sec) mysql>source /tmp/a.txt; Query OK, 0 rows affected (0.00 sec)
二、
殺掉當前全部的MySQL鏈接
mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill
殺掉指定用戶運行的鏈接,這裏爲Mike
mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill
三、經過SHEL腳本實現
#殺掉鎖定的MySQL鏈接 for id in `mysqladmin processlist|grep -i locked|awk '{print $1}'` do mysqladmin kill ${id} done
四、經過Maatkit工具集中提供的mk-kill命令進行
#殺掉超過60秒的sql mk-kill -busy-time 60 -kill #若是你想先不殺,先看看有哪些sql運行超過60秒 mk-kill -busy-time 60 -print #若是你想殺掉,同時輸出殺掉了哪些進程 mk-kill -busy-time 60 -print –kill
mk-kill更多用法可參考: http://www.maatkit.org/doc/mk-kill.html http://www.sbear.cn/archives/426 Maatkit工具集的其它用法可參考: http://code.google.com/p/maatkit/wiki/TableOfContents?tm=6 參考文檔: http://www.google.com http://www.orczhou.com/index.php/2010/10/kill-mysql-connectio-in-batch/ http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/