mysql常用監控腳本整理
01 #!/bin/sh 02 #filename:mysql_monitor.sh 03 #site:www.jbxue.com 04 #檢測mysql server是否正常提供服務 05 mysqladmin -u sky -ppwd -h localhost ping 06 07 #獲取mysql當前的幾個狀態值 08 mysqladmin -u sky -ppwd -h localhost status 09 10 #獲取數據庫當前的連接信息 11 mysqladmin -u sky -ppwd -h localhost processlist 12 13 #獲取當前數據庫的連接數 14 mysql -u root -p123456 -BNe "select host,count(host) from processlist group by host;" information_schema 15 16 #顯示mysql的uptime 17 mysql -e"SHOW STATUS LIKE '%uptime%'"|awk '/ptime/{ calc = $NF / 3600;print $(NF-1), calc"Hour" }' 18 19 #查看數據庫的大小 20 mysql -u root -p123456-e 'select table_schema,round(sum(data_length+index_length)/1024/1024,4) from information_schema.tables group by table_schema;' 21 22 #查看某個表的列信息 23 mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>" <database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g' 24 25 #執行mysql腳本 26 mysql -u user-name -p password < script.sql 27 28 #mysql dump數據導出 29 mysqldump -uroot -T/tmp/mysqldump test test_outfile --fields-enclosed-by=\" --fields-terminated-by=, 30 31 #mysql數據導入 32 mysqlimport --user=name --password=pwd test --fields-enclosed-by=\" --fields-terminated-by=, /tmp/test_outfile.txt 33 LOAD DATA INFILE '/tmp/test_outfile.txt' INTO TABLE test_outfile FIELDS TERMINATED BY '"' ENCLOSED BY ','; 34 35 36 #mysql進程監控 37 ps -ef | grep "mysqld_safe" | grep -v "grep" 38 ps -ef | grep "mysqld" | grep -v "mysqld_safe"| grep -v "grep" 39 40 #查看當前數據庫的狀態 41 mysql -u root -p123456 -e 'show status' 42 43 #mysqlcheck 工具程序可以檢查(check),修 復( repair),分 析( analyze)和優化(optimize)MySQL Server 中的表 44 mysqlcheck -u root -p123456 --all-databases 45 46 #mysql qps查詢 QPS = Questions(or Queries) / Seconds 47 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Questions"' 48 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Queries"' 49 #mysql Key Buffer 命中率 key_buffer_read_hits = (1 - Key_reads / Key_read_requests) * 100% key_buffer_write_hits= (1 - Key_writes / Key_write_requests) * 100% 50 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Key%"' 51 52 #mysql Innodb Buffer 命中率 innodb_buffer_read_hits=(1-Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests) * 100% 53 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Innodb_buffer_pool_read%"' 54 55 #mysql Query Cache 命中率 Query_cache_hits= (Qcache_hits / (Qcache_hits + Qcache_inserts)) * 100% 56 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Qcache%"' 57 58 #mysql Table Cache 狀態量 59 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Open%"' 60 61 #mysql Thread Cache 命中率 Thread_cache_hits = (1 - Threads_created / Connections) * 100% 正常來說,Thread Cache 命中率要在 90% 以上才算比較合理。 62 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Thread%"' 63 # www.jbxue.com 64 65 #mysql 鎖定狀態:鎖定狀態包括表鎖和行鎖兩種,我們可以通過系統狀態變量獲得鎖定總次數,鎖定造成其他線程等待的次數,以及鎖定等待時間信息 66 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "%lock%"' 67 68 #mysql 復制延時量 在slave節點執行 69 mysql -u root -p123456 -e 'SHOW SLAVE STATUS' 70 71 #mysql Tmp table 狀況 Tmp Table 的狀況主要是用於監控 MySQL 使用臨時表的量是否過多,是否有臨時表過大而不得不從內存中換出到磁盤文件上 72 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Created_tmp%"' 73 74 #mysql Binlog Cache 使用狀況:Binlog Cache 用於存放還未寫入磁盤的 Binlog 信 息 。 75 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Binlog_cache%"' 76 77 #mysql nnodb_log_waits 量:Innodb_log_waits 狀態變量直接反應出 Innodb Log Buffer 空間不足造成等待的次數 78 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Innodb_log_waits'