mysql常见监控项
1、MySQL服務(wù)運(yùn)行狀態(tài)
約定所有MySQL服務(wù)都必須以ip1(內(nèi)網(wǎng)ip)來綁定,每個機(jī)器只有一個ip1,可以有多個端口,即多個MySQL Server。采集程序讀取ip端口信息文件來判斷server是否存在。
sockParam=`ps aux | grep -P "mysqld.*--port=${port}" | grep -oP " --socket.*\.sock"`? # 空則獲取不到該服務(wù)器端口mysql socket配置,請檢查mysql配置是否正確
MYSQL="/usr/local/mysql/bin/mysql -hlocalhost --port=${port} ${sockParam} -u${user} -p${password} "
MYSQL_ADMIN="/usr/local/mysql/bin/mysqladmin -hlocalhost --port=${port} ${sockParam} -u${user} -p${password} "
curStatus=`${MYSQL} -e"show global status"`? # 空則是獲取不到該服務(wù)器mysql狀態(tài),請檢查mysql是否正常運(yùn)行
if [ -z "${curStatus}" ]
then
? ? portExists=0
else
? ? echo "${curStatus}" >> ${curFile}
? ? portExists=1
2、連接數(shù)
${MYSQL_ADMIN} processlist -v | wc -l
3、線程數(shù)
grep 'Threads_connected' ${curFile} | awk '{print $2}'
4、慢查詢數(shù)
grep 'Slow_queries' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的慢查詢次數(shù)。上次數(shù)據(jù)保存在last.cache。
5、打開表數(shù)
grep 'Open_tables' ${curFile} | awk -F ' ' '{print $2}'
6、每秒執(zhí)行select數(shù)
grep 'Com_select' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
7、每秒執(zhí)行delete數(shù)
grep 'Com_delete' ${curFile} | grep -v 'multi' | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
8、每秒執(zhí)行insert數(shù)
grep 'Com_insert' ${curFile} | grep -v 'select' | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
9、每秒執(zhí)行update數(shù)
grep 'Com_update' ${curFile} | grep -v 'multi' | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
10、每秒鐘執(zhí)行replace數(shù)
grep 'Com_replace' ${curFile} | grep -v 'select' | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
11、每秒鐘執(zhí)行的 Innodb_rows_deleted
grep 'Innodb_rows_deleted' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
12、每秒鐘執(zhí)行的 Innodb_rows_inserted
grep 'Innodb_rows_inserted' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
13、每秒鐘執(zhí)行的 Innodb_rows_read
grep 'Innodb_rows_read' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
14、每秒鐘執(zhí)行的 Innodb_rows_updated
grep 'Innodb_rows_updated' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量。上次數(shù)據(jù)保存在last.cache。
15、每秒鐘執(zhí)行的 innodb rows total
expr ${innodbRowsDeletedPS} + ${innodbRowsInsertedPS} + ${innodbRowsReadPS} + ${innodbRowsUpdatedPS}
等于前面四個Innodb_rows_*執(zhí)行次數(shù)的總和
16、每秒處理命令數(shù) qps
expr ${mysqlSelectNumPS} + ${mysqlInsertNumPS} + ${mysqlUpdateNumPS} + ${mysqlDeleteNumPS} + ${mysqlReplaceNumPS}
等于前面五個mysql命令Com_*的數(shù)量總和
17、每秒接收字節(jié)數(shù) KByte/s
grep 'Bytes_received' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量,除以1024得到單位KByte/s。上次數(shù)據(jù)保存在last.cache。
18、每秒發(fā)送字節(jié)數(shù)
grep 'Bytes_sent' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值除以時間差,等于最近1分鐘的執(zhí)行數(shù)量,除以1024得到單位KByte/s。上次數(shù)據(jù)保存在last.cache。
19、可立即獲得鎖的次數(shù)
grep 'Table_locks_immediate' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的可立即獲得鎖數(shù)量。上次數(shù)據(jù)保存在last.cache。
20、不可立即獲得鎖的次數(shù)
grep 'Table_locks_waited' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的不可立即獲得鎖數(shù)量。上次數(shù)據(jù)保存在last.cache。
21、一行鎖定需等待時間
grep 'Innodb_row_lock_waits' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的一行鎖定需等待時間。上次數(shù)據(jù)保存在last.cache。
22、 當(dāng)前臟頁數(shù)
grep 'Innodb_buffer_pool_pages_dirty' ${curFile} | awk -F ' ' '{print $2}'
23、要求清空的緩沖池頁數(shù)
grep 'Innodb_buffer_pool_pages_flushed' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的要求清空的緩沖池頁數(shù)。上次數(shù)據(jù)保存在last.cache。
24、Innodb 寫入日志字節(jié)數(shù) KByte
grep 'Innodb_os_log_written' ${curFile} | awk -F ' ' '{print $2}'
需要計算兩次的慢查詢次數(shù)得到差值,等于最近1分鐘的寫入日志字節(jié)數(shù),除以1024得到KByte。上次數(shù)據(jù)保存在last.cache。
25、占用內(nèi)存大小 MByte
pid=`ps aux | grep 'mysqld' | grep -Ev 'safe|grep' | awk '{print $2}' `
mem=`cat /proc/${pid}/status | grep 'VmRSS' | awk '{print $2}'`
mysqlMem=`echo "scale=2;${mem} / 1024" | bc`
除以1024得到MByte
26、handler socket每秒處理數(shù)
curHsTableLock=`grep 'Hs_table_lock' ${curFile} | awk '{print $2}'`
preHsTableLock=`grep 'Hs_table_lock' ${preFile} | awk '{print $2}'`
if [ -n "${curHsTableLock}" ]
then
? ? hsQPS=`echo "scale=0;(${curHsTableLock} - ${preHsTableLock}) / ${intervalTime}" | bc`
else
? ? hsQPS=0
fi
27、主從同步和狀態(tài)
#主從信息
#是否為從服務(wù)器
slave_running=`grep 'Slave_running' ${curFile} | awk '{print $2}'`
if [ "${slave_running}A" = "ONA" ]
then
? ? slaveRunning=1
? ? slaveStatus=`${MYSQL} -e'show slave status\G'`
? ? echo "${slaveStatus}" > ${slaveFile}
? ??
? ? slaveIoRunning=`grep 'Slave_IO_Running' ${slaveFile} | awk -F ':' '{print $2}'`
? ? slaveSqlRunning=`grep 'Slave_SQL_Running' ${slaveFile} | awk -F ':' '{print $2}'`
? ? if [ "${slaveIoRunning}A" == "NoA" -o "${slaveSqlRunning}A" == "NoA" ]
? ? then
? ? ? ? slaveRunning=3
? ? fi
? ??
? ? secondsBehindMaster=`grep 'Seconds_Behind_Master' ${slaveFile} | awk -F ':' '{print $2}'`
? ? if [ "${secondsBehindMaster}A" = "NULLA" ]
? ? then
? ? ? ? secondsBehindMaster=8888? # 表示主從不同步
? ? fi
? ? #是從庫時 獲取主庫ip
? ? master=`grep 'Master_Host' ${slaveFile} | awk -F ':' '{print $2}'`
? ? masterPort=`grep 'Master_Port' ${slaveFile} | awk -F ':' '{print $2}'`
else
? ? master=""
? ? masterPort=""
? ? slaveRunning=0
? ? secondsBehindMaster=10000? # 不用檢測
fi
注:Seconds_Behind_Master,該值作為判斷主從延時的指標(biāo),那么它又是怎么得到這個值的呢,同時,它為什么又受到很多人 的質(zhì)疑?
Seconds_Behind_Master是通過比較sql_thread執(zhí)行的event的timestamp和io_thread復(fù)制好的 event的timestamp(簡寫為ts)進(jìn)行比較,而得到的這么一個差值。我們都知道的relay-log和主庫的bin-log里面的內(nèi)容完全一樣,在記錄sql語句的同時會被記錄上當(dāng)時的ts,所以比較參考的值來自于binlog,其實(shí)主從沒有必要與NTP進(jìn)行同步,也就是說無需保證主從時鐘的 一致。你也會發(fā)現(xiàn),其實(shí)比較真正是發(fā)生在io_thread與sql_thread之間,而io_thread才真正與主庫有關(guān)聯(lián),于是,問題就出來了, 當(dāng)主庫I/O負(fù)載很大或是網(wǎng)絡(luò)阻塞,io_thread不能及時復(fù)制binlog(沒有中斷,也在復(fù)制),而sql_thread一直都能跟上 io_thread的腳本,這時Seconds_Behind_Master的值是0,也就是我們認(rèn)為的無延時,但是,實(shí)際上不是,你懂得。這也就是為什 么大家要批判用這個參數(shù)來監(jiān)控數(shù)據(jù)庫是否發(fā)生延時不準(zhǔn)的原因,但是這個值并不是總是不準(zhǔn),如果當(dāng)io_thread與master網(wǎng)絡(luò)很好的情況下,那么 該值也是很有價值的。
之前,提到 Seconds_Behind_Master這個參數(shù)會有負(fù)值出現(xiàn),我們已經(jīng)知道該值是io_thread的最近跟新的ts與sql_thread執(zhí)行到 的ts差值,前者始終是大于后者的,唯一的肯能就是某個event的ts發(fā)生了錯誤,比之前的小了,那么當(dāng)這種情況發(fā)生時,負(fù)值出現(xiàn)就成為可能。
28、檢測采集Agent心跳情況
轉(zhuǎn)載于:https://www.cnblogs.com/cyt1153/p/6608069.html
總結(jié)
以上是生活随笔為你收集整理的mysql常见监控项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP header的一些用法
- 下一篇: 初识Spark2.0之Spark SQL