zabbix监控MySQL(windows和linux环境)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

一、linux环境下监控MySQL

Zabbix Server自带了MySQL插件来监控mysql数据库的模板,只需要配置好agent客户端,然后在web端给主机增加模板就行了

监控项目:

Com_update:     mysql执行的更新个数

Com_select:     mysql执行的查询个数

Com_insert:     mysql执行插入的个数

Com_delete:     执行删除的个数

Com_rollback:   执行回滚的操作个数

Bytes_received:  接受的字节数

Bytes_sent:     发送的字节数

Slow_queries:   慢查询语句的个数

Com_commit:     确认的事物个数

Com_begin:      开始的事物个数

Uptime:         服务器已启动的秒数

Questions:      客户端发送到服务器的语句个数

监控模板下载地址:http://www.zabbix.org/wiki/Zabbix_Templates#External_template_resources

脚本下载地址:https://github.com/itnihao/zabbix-book


1)创建zabbix链接MySQL的用户名,密码并授予权限。

1
2
3
mysql> grant all on *.* to zabbix@ 'localhost'
identified by "123456”;
mysql> flush privileges;

2)在zabbix_agent服务目录下创建 .my.cnf 连接文件

1
2
3
4
5
cd  /usr/local/zabbix/etc/
vim .my.cnf
[client]
user=zabbix
password=123456

注意:
如果在数据库grant授权时,针对的是localhost,这个.my.cnf里面就不用加host参数了【如上配置】
但如果grant授权时针对的是本机的ip(如192.168.1.25),那么在.my.cnf文件里就要加上host参数进行指定:host=192.168.1.25


3)配置MySQL的key文件

这个可以从zabbix安装时的解压包里拷贝过来:

1
2
3
cp 
/usr/local/src/zabbix3 .0.3 /conf/
zabbix_agentd /userparameter_mysql .conf  /usr/local/zabbix/etc/zabbix_agentd .conf.d/


4)替换zabbix安装路径,注意如果MySQL没配置好环境变量可能找不到MySQL命令,可以用MySQL全路径

看到类似 HOME=/var/lib/zabbix 的路径设置,把路径全都替换为 /usr/local/zabbix/etc/,也就是上面的.my.cnf文件所在的目录路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cd  /usr/local/zabbix/etc/zabbix_agentd .conf.d/
vim userparameter_mysql.conf
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*], echo  "show global status where Variable_name='$1';"  | HOME= /usr/local/zabbix/etc/  mysql -N |  awk  '{print $$2}'
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
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=/usr/local/zabbix/etc/ mysql -N'
UserParameter=mysql. ping ,HOME= /usr/local/zabbix/etc/  mysqladmin  ping  grep  -c alive
UserParameter=mysql.version,mysql -V
 
%s #/var/lib/zabbix#/usr/local/zabbix/etc/#      #用命令替换

也可以用这个shell脚本监控,包括主从监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### MySQL DB Infomation
UserParameter=mysql.status[*], echo "show global status where Variable_name='$1';" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| awk  '{print $$2}'
UserParameter=mysql.variables[*], echo "show global variables where Variable_name='$1';" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| awk  '{print $$2}'
UserParameter=mysql. ping ,mysqladmin--defaults- file = /usr/local/zabbix/etc/ .my.cnf  ping | grep  -c  alive
UserParameter=mysql.version, echo "select version();" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N
  
#### MySQL Master Information
UserParameter=mysql.master.Slave_count, echo "show slave hosts;" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| wc  -l
UserParameter=mysql.master.Binlog_file, echo "show master status;" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| awk  '{print $1}' | awk  -F. '{print $1}'
UserParameter=mysql.master.Binlog_number, echo "show master status;" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| awk  '{print $1}' | awk  -F. '{print $2}'
UserParameter=mysql.master.Binlog_position, echo "show master status;" |mysql --defaults- file = /usr/local/zabbix/etc/ .my.cnf-N| awk  '{print $2}'
UserParameter=mysql.master.Binlog_count, echo "show binary logs;" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| wc  -l
UserParameter=mysql.master.Binlog_total_size, echo "show binary logs;" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf -N| awk  '{sum+=$NF}END{print  sum}'
  
#### MySQL Slave Information
UserParameter=mysql.slave.Seconds_Behind_Master, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep  "Seconds_Behind_Master" | awk '{print $2}'
UserParameter=mysql.slave.Slave_IO_Running, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep "Slave_IO_Running" | awk  '{print $2}'
UserParameter=mysql.slave.Slave_SQL_Running, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep "Slave_SQL_Running" | awk  '{print $2}'
UserParameter=mysql.slave.Relay_Log_Pos, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep "Relay_Log_Pos" | awk  '{print $2}'
UserParameter=mysql.slave.Exec_Master_Log_Pos, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep "Exec_Master_Log_Pos" | awk  '{print $2}'
UserParameter=mysql.slave.Read_Master_Log_Pos, echo "show slave status\G" |mysql--defaults- file = /usr/local/zabbix/etc/ .my.cnf| grep "Read_Master_Log_Pos" | awk  '{print $2}'



5)修改zabbix_agentd.conf配置文件,开启额外加载,就是去掉前面的#号

1
2
vim zabbix_agentd.conf
Include= /usr/local/zabbix/etc/zabbix_agentd .conf.d/

6)重启zabbix_agentd服务

1
/etc/init .d /zabbix_agentd  restart

7)测试看能否获取到数据

1
2
# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.status[Com_select]
200661

8)登陆zabbix监控界面,在“配置”里为主机添加模板,完成监控。

wKioL1kDCfnzGAvoAAErWK8wki4296.png-wh_50

wKiom1kDCfrxfjaPAAEhZQbl6As617.png-wh_50

wKioL1kDCfugFXQpAADs2vrL1NI866.png-wh_50


二、windows下的MySQL监控

要想在Windows上取得MySQL的状态数据,可以用vbs脚本运行mysql命令

1)在d:\Zabbix\Scripts\目录下新建两个脚本文件内容如下:

mysql_ping.vbs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Set  objFS =CreateObject( "Scripting.FileSystemObject" )
Set  objArgs = WScript.Arguments
str1 = getCommandOutput( "D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-ucactiuser -pcactiuser ping" )     //修改对应数据库路径,用户名和密码
  
If  Instr(str1, "alive" ) > 0Then
WScript.Echo 1
Else
WScript.Echo 0
End  If
  
Function  getCommandOutput(theCommand)
  
Dim  objShell, objCmdExec
Set  objShell =CreateObject( "WScript.Shell" )
Set  objCmdExec = objshell.exec(thecommand)
getCommandOutput =objCmdExec.StdOut.ReadAll
end  Function


 MYSQL-status.vbs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Set  objFS = CreateObject( "Scripting.FileSystemObject" )
Set  objArgs = WScript.Arguments
str1 = getCommandOutput( "D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-u cactiuser -pcactiuser extended-status" )      //修改对应数据库路径,用户名和密码
Arg = objArgs(0)
str2 = Split(str1, "|" )
  
For  i = LBound(str2) to UBound(str2)
  
If  Trim(str2(i)) = Arg  Then   
WScript.Echo TRIM(str2(i+1))
Exit  For
End  If
next
  
  
Function  getCommandOutput(theCommand)
  
Dim  objShell, objCmdExec
Set  objShell =CreateObject( "WScript.Shell" )
Set  objCmdExec = objshell.exec(thecommand)
getCommandOutput =objCmdExec.StdOut.ReadAll
  
end  Function



2)修改windows上的zabbix_agentd.comf文件,设置key值。在UserParameter下面加两句;

UserParameter=mysql.status[*], cscript/nologo d:\Zabbix\Scripts\MySQL_Ext-Status_Script.vbs $1 
UserParameter=mysql.ping, cscript /nologo d:\Zabbix\Scripts\MySql_Ping.vbs

3)重启zabbix_agentd,并给主机添加MySQL模版,查看items状态。

wKioL1kDCfugFXQpAADs2vrL1NI866.png-wh_50



本文转自 80后小菜鸟 51CTO博客,原文链接:http://blog.51cto.com/zhangxinqi/1920584

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
关系型数据库 应用服务中间件 Linux
Linux云服务器如何搭建LNMP环境
LNMP环境是Linux系统中常用的Web服务架构,由Linux、Nginx、MySQL/MariaDB和PHP组成,适用于高效托管动态网站。本文以CentOS 7为例,详细介绍了LNMP环境的搭建步骤,包括Nginx、MariaDB和PHP的安装与配置,以及最终通过创建`index.php`文件验证环境是否成功部署。具体操作涵盖配置YUM仓库、安装服务、编辑配置文件、启动服务等关键步骤,确保用户能够顺利搭建并运行LNMP环境。
25 1
Linux云服务器如何搭建LNMP环境
|
2月前
|
Ubuntu Linux Shell
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
(已成功解决)Linux环境报错—bash: wget: command not found;常见Linux发行版本,Linux中yum、rpm、apt-get、wget的区别;Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
669 68
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
|
9天前
|
存储 缓存 监控
|
1月前
|
监控 Java 应用服务中间件
tomcat相关概念与部署tomcat多实例-zabbix监控(docker部署)
通过上述步骤,您可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使您能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
61 23
|
1月前
|
监控 Java 应用服务中间件
tomcat相关概念与部署tomcat多实例-zabbix监控(docker部署)
通过上述步骤,您可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使您能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
53 22
|
1月前
|
监控 Java 应用服务中间件
tomcat相关概念与部署tomcat多实例-zabbix监控(docker部署)
通过上述步骤,您可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使您能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
70 25
|
2月前
|
消息中间件 Java Kafka
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
本文介绍了Kafka集群的搭建过程,涵盖从虚拟机安装到集群测试的详细步骤。首先规划了集群架构,包括三台Kafka Broker节点,并说明了分布式环境下的服务进程配置。接着,通过VMware导入模板机并克隆出三台虚拟机(kafka-broker1、kafka-broker2、kafka-broker3),分别设置IP地址和主机名。随后,依次安装JDK、ZooKeeper和Kafka,并配置相应的环境变量与启动脚本,确保各组件能正常运行。最后,通过编写启停脚本简化集群的操作流程,并对集群进行测试,验证其功能完整性。整个过程强调了自动化脚本的应用,提高了部署效率。
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
|
2月前
|
关系型数据库 MySQL 数据库
【MySQL基础篇】MySQL概述、Windows下载MySQL8.0超详细图文安装教程
在这一章节,主要介绍两个部分,数据库相关概念及MySQL数据库的介绍、下载、安装、启动及连接。接着,详细描述了MySQL 8.0的版本选择与下载,推荐使用社区版(免费)。安装过程包括自定义安装路径、配置环境变量、启动和停止服务、以及客户端连接测试。此外,还提供了在同一台电脑上安装多个MySQL版本的方法及卸载步骤。最后,解释了关系型数据库(RDBMS)的特点,即基于二维表存储数据,使用SQL语言进行操作,格式统一且便于维护。通过具体的结构图展示了MySQL的数据模型,说明了数据库服务器、数据库、表和记录之间的层次关系。
【MySQL基础篇】MySQL概述、Windows下载MySQL8.0超详细图文安装教程
|
2月前
|
安全 关系型数据库 MySQL
Windows Server 安装 MySQL 8.0 详细指南
安装 MySQL 需要谨慎,特别注意安全配置和权限管理。根据实际业务需求调整配置,确保数据库的性能和安全。
302 9
|
2月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
233 7

热门文章

最新文章