生活随笔
收集整理的這篇文章主要介紹了
Zabbix监控Mysql数据库性能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
來源:http://os.51cto.com/art/201404/435139.htm
本文通過Zabbix Graphs實時查看的SQL語句操作情況和mysql發送接收的字節數,Zabbix還可以監控mysql slow queries,mysql version,uptime,alive等。
作者:sfzhang來源:51TCO博客|
2014-04-09 11:43 收藏 分享
在之前的博文里面寫過如何通過Zabbix監控mysql主從同步是否OK,mysql從庫是否有延時(Seconds_Behind_Master)主庫,當mysql主從有異常時通過Email或者SMS通知DBA和系統人員。除此之外,Zabbix還可以監控mysql slow queries,mysql version,uptime,alive等。下面通過Zabbix Graphs實時查看的SQL語句操作情況和mysql發送接收的字節數。
1.Zabbix官方提供的監控mysql的模板Template App MySQL,可以看到相關的Items和key。
2.把該模板Template App MySQL Link到相關的主機上面,發現Item的Status是不可用的,因為key的值是通過Mysql用戶查看"show global status"信息或者用mysqladmin命令查看status或extended-status的信息而取的值。
mysql>?show?global?status;?mysql>?show?status;? 3.結合官方提供的key編寫Shell腳本,從數據庫中取出Items的key的值。
[root@monitor?scripts]#?cat?checkmysqlperformance.sh?#!/bin/sh?#Create?by?sfzhang?2014.02.20?MYSQL_SOCK="/data/mysql/3306/mysql.sock"?MYSQL_PWD=`cat?/data/mysql/3306/.mysqlpassword`?ARGS=1?if?[?$#?-ne?"$ARGS"?];then?????echo?"Please?input?one?arguement:"?fi?case?$1?in?????Uptime)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?status|cut?-f2?-d":"|cut?-f1?-d"T"`?????????????echo?$result?????????????;;?????????Com_update)?????????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_update"|cut?-d"|"?-f3`?????????????echo?$result?????????????;;?????????Slow_queries)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?status?|cut?-f5?-d":"|cut?-f1?-d"O"`?????????????????echo?$result?????????????????;;?????Com_select)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_select"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Com_rollback)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_rollback"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Questions)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?status|cut?-f4?-d":"|cut?-f1?-d"S"`?????????????????echo?$result?????????????????;;?????Com_insert)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_insert"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Com_delete)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_delete"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Com_commit)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_commit"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Bytes_sent)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Bytes_sent"?|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Bytes_received)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Bytes_received"?|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????Com_begin)?????????result=`mysqladmin?-uroot?-p${MYSQL_PWD}?-S?$MYSQL_SOCK?extended-status?|grep?-w?"Com_begin"|cut?-d"|"?-f3`?????????????????echo?$result?????????????????;;?????????????????????????????????*)?????????echo?"Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"?????????;;?esac? 4.在Zabbix_agentd.conf里面添加UserParameter,格式如下,對于Zabbix來說,腳本其實就是一個插件。
UserParameter=mysql.version,mysql?-V?UserParameter=mysql.ping,mysqladmin?-uroot?-p123456?-S?/data/mysql/3306/mysql.sock?ping?|?grep?-c?alive?UserParameter=mysql.status[*],/etc/zabbix/scripts/checkmysqlperformance.sh?$1?$2? 5.重啟agentd服務器,然后在zabbix server用zabbix_get就可以取到key的值。
6.在zabbix前端可以實時查看SQL語句每秒鐘的操作次數。
7.在zabbix前端可以實時查看mysql發送接收的字節數。其中bytes received表示從所有客戶端接收到的字節數,bytes sent表示發送給所有客戶端的字節數。
總結
把該腳本放到要監控的服務器上面(Modify mysql user and password),修改UserParameter的參數并重啟agentd,Link官方提供的Template App MySQL模板即可。
我這里是測試環境用root賬號,線上服務器安全期間可以給mysql用戶授權readonly權限。
根據實際的需求,除了監控上述監控項之外,還可以監控mysql processlist,Innodb等。
總結
以上是生活随笔為你收集整理的Zabbix监控Mysql数据库性能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。