Zabbix自定义脚本监控MySQL数据库

一、MySQL数据库配置

1.1 创建Mysql数据库用户

root@mysql \~# mysql -uroot -p

create user zabbix@'127.0.0.1' identified by '123456';

flush privileges;

1.2 添加用户密码到mysql client的配置文件中

root@mysql \~# vim /etc/my.cnf.d/client.cnf

client

host='127.0.0.1'

user='zabbix'

password='123456'

二、创建自定义监控脚本

root@mysql \~# vim /etc/zabbix/script/check_mysql.sh

bash 复制代码
#!/bin/bash
#Mysql主机地址
MYSQL_HOST='127.0.0.1'
#Mysql端口
MYSQL_PORT='3306'
#数据连接
MYSQL_CONN="/usr/bin/mysqladmin  -h${MYSQL_HOST} -P${MYSQL_PORT}"

#参数是否正确
if [ $# -ne "1" ];then
    echo "arg error!" 
fi

#获取数据
case $1 in
    Uptime)
        result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
        echo $result 
        ;;
    Com_update)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
        echo $result 
        ;;
    Slow_queries)
        result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
        echo $result 
        ;;
    Com_select)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
        echo $result 
                ;;
    Com_rollback)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
                echo $result 
                ;;
    Questions)
        result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
                echo $result 
                ;;
    Com_insert)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
                echo $result 
                ;;
    Com_delete)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
                echo $result 
                ;;
    Com_commit)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
                echo $result 
                ;;
    Bytes_sent)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
                echo $result 
                ;;
    Bytes_received)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
                echo $result 
                ;;
    Com_begin)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
                echo $result 
                ;;
        *)
        echo "Unknown options." 
        ;;
esac

三、zabbix agent添加自定义键值

root@mysql \~# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

UserParameter=mysql.status\*,bash /etc/zabbix/script/check_mysql.sh $1

UserParameter=mysql.ping,/usr/bin/mysqladmin ping 2>/dev/null | grep -c alive

UserParameter=mysql.version,/usr/bin/mysql -V

root@mysql \~# systemctl restart zabbix-agent

四、链接Mysql模板

配置-->主机-->添加模板-->选择"Template DB MySQL"

五、数据查看

相关推荐
睡不醒男孩0308239 分钟前
生产环境故障销账:PostgreSQL 突发连接数暴涨与死锁,如何利用 CLup 秒级定位与解锁?
运维·数据库
2601_9620549511 分钟前
终端与IDE形态的vibe coding实测:两款AI编程工具迭代能力对比
数据库·ide·ai编程
万岳科技22 分钟前
教育培训系统开发流程详解:平台建设关键环节解析
数据库·后端·学习
Nturmoils24 分钟前
线上修一批脏数据,先别急着全量重来
数据库·后端
吴声子夜歌26 分钟前
SQL经典实例——处理字符串
数据库·sql
睡不醒男孩0308231 小时前
PostgreSQL 数据库运维转型:从传统模式到 CLup 平台的 25 个核心 FAQ
运维·数据库·postgresql
凡人叶枫1 小时前
Effective C++ 条款40:明智而审慎地使用多重继承
java·数据库·c++·嵌入式开发·effective c++
至此流年莫相忘1 小时前
Spring 依赖注入三剑客:@Autowired、@Resource 与 @RequiredArgsConstructor 深度对比与实战指南
java·数据库·spring
Rain5091 小时前
2.2 数据基础:数据库集成与 ORM(TypeORM / Prisma)
数据库·人工智能·ai·数据分析·node.js·自动化·ai编程
杨云龙UP2 小时前
Oracle/ODA RAC /u01 空间告警处理指南:grid 用户监听日志清理_2026-06-15
linux·数据库·oracle·oracle linux·oda·监听日志·在线清理