配置userparameter_mysql.conf
#移动到zabbix解压缩路径
cd /usr/local/src/zabbix-3.0.3/conf/zabbix_agentd
#拷贝文件到/opt/zabbix/etc/zabbix_agentd.conf.d/ #指定自己的zabbix安装目录
cp userparameter_mysql.conf /opt/zabbix/etc/zabbix_agentd.conf.d/
#添加用来关联mysql的配置文件my.cnf,并修改模板配置文件中的mysql路径
cd /opt/zabbix/etc/
vim .my.cnf
# Zabbix Agent
[mysql] host=localhost user=zabbix password=@Zabbix..0 socket=/data/mysql/mysql.sock [mysqladmin] host=localhost user=zabbix password=@Zabbix..0 socket=/data/mysql/mysql.sock
# mysql.sock文件通过find / -name mysql.sock找出路径
# 修改userparameter_mysql.conf:
sudo sed -i ‘s@/var/lib/zabbix@/opt/zabbix/etc@g‘ /opt/zabbix/etc/zabbix_agentd.d/userparameter_mysql.conf
# 修改完成userparameter_mysql.conf内容如下:
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/opt/zabbix/etc mysql -N | awk '{print $$2}' UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=/"$1/"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=/"$2/"");" | HOME=/var/lib/zabbix mysql -N' UserParameter=mysql.ping,HOME=/opt/zabbix/etc mysqladmin ping | grep -c alive
#HOME目录为新增.my.cnf文件所在目录
# 修改zabbix_agentd.conf文件,添加下面一行
Include=/opt/zabbix/etc/zabbix_agentd.conf.d/
#重启zabbix-agentd
/etc/init.d/zabbix_agentd restart
#测试
# /opt/zabbix/bin/zabbix_get -s 127.0.0.1 -p 10050 -k mysql.ping 1
# /opt/zabbix/bin/zabbix_get -s 127.0.0.1 -p 10050 -k mysql.version /data/mysql/bin/mysql Ver 14.14 Distrib 5.7.12, for Linux (x86_64) using EditLine wrapper
#注意:如果测试的时候报错:sh: mysql: command not found,sh: mysqladmin: command not found,修改userparameter_mysql.conf中mysql,mysqladmin命令的路径,使用绝对路径。
Zabbix-web页面设置
#Configuration—Hosts—Zabbix server—Tempaltes,搜索MySQL,添加弹出的模板,然后更新
#Monitoring—Graphs,选择组,然后选择host及展示的图形
#利用Zabbix自带模板监控MySQL完成。
本文原创地址: http://www.linuxprobe.com/monitor-mysql-with-zabbix.html 作者:王辉,审核员:冯振华