zabbix-agent如何监控mysql
如今,我们普遍是使用zabbix-agent来监控mysql,因为配置简单
只需要添加zabbix-agent2的模板,然后配置mysql的socket路径,然后,配置用户名密码就可以了
然而,咱们也还有相当一部分服务是跑在系统无法安装zabbix-agent2的系统上,这个时候就只能使用zabbix-agent来监控了
客户端配置
描述
在zabbix-agent模板中,有这么一段描述
Requirements for template operation:
1.Install Zabbix agent and MySQL client.
2.Copy template_db_mysql.conf into folder with Zabbix agent configuration (/etc/zabbix/zabbix_agentd.d/ by default). Don't forget to restart zabbix-agent.
3.Create MySQL user for monitoring. For example:
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
For more information read the MySQL documentation https://dev.mysql.com/doc/refman/8.0/en/grant.html , please.
4.Create .my.cnf in home directory of Zabbix agent for Linux (/var/lib/zabbix by default) or my.cnf in c:\ for Windows. For example:
[client]
user='zbx_monitor'
password='<password>'
You can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/384189-discussion-thread-for-official-zabbix-template-db-mysql
Template tooling version used: 0.42
大概意思就是说,需要先安装zabbix-agent和mysql客户端
然后将 配置文件 template_db_mysql.conf
存放到 /etc/zabbix/zabbix_agentd.d/
目录下,然后重启zabbix-agent
然后在mysql中创建监控用户
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
最后再在zabbix-agent的家目录中放一个 .my.cnf
文件
内容是用户名和密码
[client]
user='zbx_monitor'
password='<password>'
这段描述就已经写的很清楚了
执行
找到配置文件 template_db_mysql.conf
https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/mysql_agent
在这里找到模板
然后当到 /etc/zabbix/zabbix_agentd.d/
目录下
不过需要做一些修改
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default)
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%\my.cnf,C:\my.cnf,BASEDIR\my.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user='zbx_monitor'
#password='<password>'
#
UserParameter=mysql.ping[*], HOME=/etc/zabbix mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], HOME=/etc/zabbix mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], HOME=/etc/zabbix mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*],HOME=/etc/zabbix mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*],HOME=/etc/zabbix mysql -h"$1" -P"$2" -sN -e "SELECT COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH),0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*],HOME=/etc/zabbix mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*],HOME=/etc/zabbix mysql -h"$1" -P"$2" -sNX -e "show slave status"
需要在每条命令前添加一个参数 HOME=/etc/zabbix
明确指定 命令执行时的家目录
因为按照描述的说法,需要把用户名和密码文件存放到zabbix运行的家目录,但是 所谓的 /var/lib/zabbix
目录其实没有,为了方式无法找到的问题,我们直接明确指定家目录,然后把 .my.cnf
文件放在配置文件目录中
配置监控用户
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY 'abc111111';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
然后写配置文件
vim /etc/zabbix/.my.cnf
[client]
user='zbx_monitor'
password='abc111111'
最后重启 zabbix-agent服务
systemctl restart zabbix-agent.service
服务端配置
只需要添加 MySQL by Zabbix agent
模板就好了,然后等待数据的到来
本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/17640960.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具