數據庫巡檢的內容通常涵蓋主機硬件、操作系統和MySQL巡檢項。其中,主機/os巡檢主要包括:主機的硬件配置、CPU/內存/磁盤使用率以及磁盤的I/O使用情況;MySQL巡檢項包括:數據庫配置、用戶權限、大表數據量、業務表主鍵和自增長情況、數據庫的並發性、當前和歷史連接情況統計、備份執行情況以及日志記錄和慢SQL的分析優化等。
1、查看MySQL服務器配置信息及運行狀況
通過show variables來查看mysql服務器配置信息,例如show variables like '%slow%';用於查看慢查詢,show variables like 'max_connections';;用於查看最大連接數。
通過ps -ef | grep mysql查看mysql進程運行狀況。
2、通過show status統計各種SQL的執行頻率
通過show status可以查看服務器狀態信息。show status可以根據需要顯示session 級別的統計結果和global級別的統計結果。
1)以下幾個參數對Myisam和Innodb存儲引擎都計數:
Com_select 執行select 操作的次數,一次查詢只累加1;
Com_insert 執行insert 操作的次數,對於批量插入的insert 操作,只累加一次;
Com_update 執行update 操作的次數;
Com_delete 執行delete 操作的次數。
2)以下幾個參數是針對Innodb存儲引擎計數的,累加的算法也略有不同:
Innodb_rows_read 執行select 查詢返回的行數;
Innodb_rows_inserted 執行insert 操作插入的行數;
Innodb_rows_updated 執行update 操作更新的行數;
Innodb_rows_deleted 執行delete 操作刪除的行數。
通過以上幾個參數,可以很容易的了解當前數據庫的應用是以插入更新為主還是以查詢操作為主,以及各種類型的SQL大致的執行比例是多少。對於更新操作的計數,是對執行次數的計數,不論提交還是回滾都會累加。
對於事務型的應用,通過Com_commit和Com_rollback可以了解事務提交和回滾的情況,對於回滾操作非常頻繁的數據庫,可能意味着應用編寫存在問題。
3)以下幾個參數便於我們了解數據庫的基本情況:
Connections 試圖連接Mysql 服務器的次數;
Uptime 服務器工作時間;
Slow_queries 慢查詢的次數。
3、通過show status判斷系統瓶頸
1)QPS(每秒Query量)
QPS = Questions(or Queries) / seconds
mysql > show global status like 'Question%';
2)TPS(每秒事務量)
TPS = (Com_commit + Com_rollback) / seconds
mysql > show global status like 'Com_commit';
mysql > show global status like 'Com_rollback';
3)key Buffer 命中率
mysql>show global status like 'key%';
key_buffer_read_hits = (1-key_reads / key_read_requests) * 100%
key_buffer_write_hits = (1-key_writes / key_write_requests) * 100%
4)InnoDB Buffer命中率
mysql> show status like 'innodb_buffer_pool_read%';
innodb_buffer_read_hits = (1 - innodb_buffer_pool_reads /
innodb_buffer_pool_read_requests) * 100%
5)Query Cache命中率
mysql> show status like 'Qcache%';
Query_cache_hits = (Qcahce_hits / (Qcache_hits + Qcache_inserts )) * 100%;
6)Table Cache狀態量
mysql> show global status like 'open%';
比較open_tables與opend_tables值。
7)Thread Cache 命中率
mysql> show global status like 'Thread%';
mysql> show global status like 'Connections';
Thread_cache_hits = (1 - Threads_created / connections ) * 100%
創建用來處理連接的線程數。如果 Threads_created 較大,你可能要增加 thread_cache_size 值。緩存訪問率的計算方法 Threads_created/Connections。
8)鎖定狀態
mysql> show global status like '%lock%';
Table_locks_waited/Table_locks_immediate 如果這個比值比較大的話,說明表鎖造成的阻塞比較嚴重。
Innodb_row_lock_waits:innodb行鎖,太大可能是間隙鎖造成的。
Table_locks_waited:不能立即獲得的表的鎖的次數。如果該值較高並且有性能問題,應首先優化查詢,然后拆分表或使用復制。
9)Tmp Table 狀況(臨時表狀況)
mysql >show status like 'Created_tmp%';
Created_tmp_disk_tables/Created_tmp_tables比值最好不要超過10%,如果Created_tmp_tables值比較大,可能是排序子句過多或者是連接子句不夠優化。
10)Binlog Cache 使用狀況
mysql > show status like 'Binlog_cache%';
如果Binlog_cache_disk_use值不為0 ,可能需要調大 binlog_cache_size大小。
11)Innodb_log_waits
mysql > show status like 'innodb_log_waits';
Innodb_log_waits值不等於0的話,表明 innodb log buffer 因為空間不足而等待。
12)連接數大小——max_connections
mysql> show variables like 'max_connections';
+-----------------------+-------+
| Variable_name | Value |
+----------------------+--------+
| max_connections | 500 |
+---------------------+--------+
mysql> show global status like 'max_used_connections';
+------------------------------+--------+
| Variable_name | Value |
+------------------------------+--------+
| Max_used_connections | 498 |
+-----------------------------+--------+
設置的最大連接數是500,而響應的連接數是498 。
max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)
13)Handler_read_rnd
mysql> show status like 'Handler_read_rnd';
如果 Handler_read_rnd 太大 ,則你寫的 SQL 語句里很多查詢都是要掃描整個表,而沒有發揮鍵的作用。
14)Key_reads
mysql> show status like 'key_read%';
+--------------------------+---------+
| Variable_name | Value |
+--------------------------+---------+
| Key_read_requests | 1190 |
| Key_reads | 2 |
+--------------------------+---------+
如果 Key_reads 太大,則應該把 my.cnf 中 key_buffer_size 變大,可以用 Key_reads/Key_read_requests計算出 cache 失敗率。
15)Handler_read_rnd
mysql> show status like 'Handler_read_rnd';
根據固定位置讀一行的請求數。如果正執行大量查詢並需要對結果進行排序該值較高。可能使用了大量需要MySQL 掃描整個表的查詢或連接沒有正確使用鍵。
16)Handler_read_rnd_next
mysql>show status like 'Handler_read_rnd_next';
在數據文件中讀下一行的請求數。如果你正進行大量的表掃描,該值較高。通常說明你的表索引不正確或寫入的查詢沒有利用索引。
17)Select_full_join
mysql>show status like 'Select_full_join';
沒有使用索引的聯接的數量。如果該值不為 0, 你應仔細檢查表的索引。
18)Select_range_check
mysql>show status like 'Select_range_check';
在每一行數據后對鍵值進行檢查的不帶鍵值的聯接的數量。如果不為 0 ,你應仔細檢查表的索引。
19)Sort_merge_passes
mysql>show status like 'Sort_merge_passes';
排序算法已經執行的合並的數量。如果這個變量值較大,應考慮增加 sort_buffer_size 系統變量的值。
20)Handler_read_first
mysql>show status like 'Handler_read_first';
索引中第一條被讀的次數。如果較高,它表明服務器正執行大量全索引掃描。例如, SELECT col1 FROM foo ,假定col1 有索引。
21)Handler_read_key
mysql>show status like 'Handler_read_key';
根據鍵讀一行的請求數。如果較高,說明查詢和表的索引正確。
22)Handler_read_next
mysql>show status like 'Handler_read_next';
按照鍵順序讀下一行的請求數。如果你用范圍約束或如果執行索引掃描來查詢索引列,該值增加。
23) Handler_read_prev
mysql>show status like 'Handler_read_prev';
按照鍵順序讀前一行的請求數。該讀方法主要用於優化 ORDER BY ... DESC 。
24)Handler_read_rnd
mysql>show status like 'Handler_read_rnd';
根據固定位置讀一行的請求數。如果你正執行大量查詢並需要對結果進行排序該值較高。你可能使用了大量需要MySQL 掃描整個表的查詢或你的連接沒有正確使用鍵。
4、打開相應的監控信息
1)error log
在配置文件my.cnf中進行配置,在運行過程中不能改變。
2)打開慢查詢
set global slow_query_log='ON';
3)設置慢查詢的時間閾值
set global long_query_time = 0.1;
4)未使用索引的sql語句打開
set global log_queries_not_using_indexes='ON
參考:https://www.cnblogs.com/feiyun8616/p/11680010.html
