zabbix自带的默认模板template db mysql对mysql的监控相当简单,而percona监控插件对mysql的监控比较全面,但由于是php开发,因此要在agent安装php环境,本文主要介绍使用percona-monitoring-plugins监控mysql。
安装zabbix-agent
#centos7 mysql zabbix3.4
rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
yum install zabbix-agent
下载地址:zabbix download
安装PHP
yum install php php-mysql -y
安装percona-zabbix-templates
yum install https://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.1.8/binary/redhat/7/x86_64/percona-zabbix-templates-1.1.8-1.noarch.rpm
下载地址:percona-monitoring-plugins
复制配置文件至zabbix_agent相关目录下
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
重启生效
service zabbix-agent restart
数据库添加只读账户
grant process,super,replication client on *.* to 'zabbixmoniter'@'%' identified by 'zabbix';
由于监控mysql需要使用show processlist 及主从相关命令,因此需要给zabbix权限以process,super,replication client等权限。修改脚本参数:
$ vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
<?php
...
$mysql_user = 'zabbixmoniter';
$mysql_pass = 'zabbix';
$mysql_port = 3306;
$mysql_flags = 0;
在mysql slave的agent里面测试脚本是否生效(输出1即表示成功):
sh /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave
问题:ERROR 1045 (28000): Access denied for user ‘root‘@’localhost’ (using password: NO)
方案1:在my.cnf配置用户名密码、sock路径
vim /usr/local/mysql/etc/my.cnf
[mysql]
user=zabbixmoniter
password=zabbix
socket=/var/lib/mysql/mysql.sock
方案2:修改/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh文件
RES=`HOME=~zabbix mysql -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
改为
RES=`HOME=~zabbix mysql -uzabbixmoniter -pzabbix -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
在zabbix server上测试
zabbix_get -s 192.168.1.49 -p 10050 -k MySQL.Threads-connected
zabbix_get -s 192.168.1.49 -p 10050 -k MySQL.running-slave
问题1:rm: cannot remove ‘/tmp/localhost-mysql_cacti_stats.txt’: Operation not permitted
修改日志权限:
chown zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt
问题2:zabbix_get: command not found
安装zabbix-get:
yum install zabbix-get