mysql監控經常使用shell腳本

#/bin/sh #檢測mysql server是否正常提供服務 mysqladmin -u sky -ppwd -h localhost ping #獲取mysql當前的幾個狀態值 mysqladmin -u sky -ppwd -h localhost status #獲取數據庫當前的鏈接信息 mysqladmin -u sky -ppwd -h localhost processlist #獲取當前數據庫的鏈接數 mysql -u root -p123456 -BNe "select host,count(host) from processlist group by host;" information_schema #顯示mysql的uptime mysql -e"SHOW STATUS LIKE '%uptime%'"|awk '/ptime/{ calc = $NF / 3600;print $(NF-1), calc"Hour" }' #查看數據庫的大小 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;' #查看某個表的列信息 mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>" <database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g' #執行mysql腳本 mysql -u user-name -p password < script.sql #mysql dump數據導出 mysqldump -uroot -T/tmp/mysqldump test test_outfile --fields-enclosed-by=\" --fields-terminated-by=, #mysql數據導入 mysqlimport --user=name --password=pwd test --fields-enclosed-by=\" --fields-terminated-by=, /tmp/test_outfile.txt LOAD DATA INFILE '/tmp/test_outfile.txt' INTO TABLE test_outfile FIELDS TERMINATED BY '"' ENCLOSED BY ','; #mysql進程監控 ps -ef | grep "mysqld_safe" | grep -v "grep" ps -ef | grep "mysqld" | grep -v "mysqld_safe"| grep -v "grep" #查看當前數據庫的狀態 mysql -u root -p123456 -e 'show status' #mysqlcheck 工具程序能夠檢查(check),修 復( repair),分 析( analyze)和優化(optimize)MySQL Server 中的表 mysqlcheck -u root -p123456 --all-databases #mysql qps查詢  QPS = Questions(or Queries) / Seconds mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Questions"' mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Queries"' #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% mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Key%"' #mysql Innodb Buffer 命中率  innodb_buffer_read_hits=(1-Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests) * 100% mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Innodb_buffer_pool_read%"' #mysql Query Cache 命中率 Query_cache_hits= (Qcache_hits / (Qcache_hits + Qcache_inserts)) * 100% mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Qcache%"' #mysql Table Cache 狀態量 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Open%"' #mysql Thread Cache 命中率  Thread_cache_hits = (1 - Threads_created / Connections) * 100%  正常來講,Thread Cache 命中率要在 90% 以上纔算比較合理。 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Thread%"' #mysql 鎖定狀態:鎖定狀態包括表鎖和行鎖兩種,咱們能夠經過系統狀態變量得到鎖定總次數,鎖定形成其餘線程等待的次數,以及鎖定等待時間信息 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "%lock%"' #mysql 複製延時量 在slave節點執行 mysql -u root -p123456 -e 'SHOW SLAVE STATUS' #mysql Tmp table 情況 Tmp Table 的情況主要是用於監控 MySQL 使用臨時表的量是否過多,是否有臨時表過大而不得不從內存中換出到磁盤文件上 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Created_tmp%"' #mysql Binlog Cache 使用情況:Binlog Cache 用於存放還未寫入磁盤的 Binlog 信 息 。 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Binlog_cache%"' #mysql nnodb_log_waits 量:Innodb_log_waits 狀態變量直接反應出 Innodb Log Buffer 空間不足形成等待的次數 mysql -u root -p123456 -e 'SHOW /*!50000 GLOBAL */ STATUS LIKE "Innodb_log_waits'
相關文章
相關標籤/搜索