zabbix自定义监控mysql主从状态和延迟及模板

zabbix自定义监控mysql主从状态和延迟及模板

监控mysql主从状态

配置mysql主从同步

 
//安装数据库

[root@master ~]# dnf install -y mariadb*

[root@web ~]# dnf install -y mariadb*


//启动并修改密码

//主库配置

[root@master ~]# systemctl enable --now mariadb

[root@master ~]# mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 10.3.28-MariaDB MariaDB Server


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> set password = password('123com');

Query OK, 0 rows affected (0.000 sec)


MariaDB [(none)]> quit

Bye

[root@master ~]# mysql -uroot -p123com //验证密码


//从库配置

[root@web ~]# systemctl enable --now mariadb

[root@web ~]# mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 10.3.28-MariaDB MariaDB Server


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> set password = password('123com');

Query OK, 0 rows affected (0.000 sec)


MariaDB [(none)]> quit

Bye

[root@web ~]# mysql -uroot -p123com //验证密码


//在主库上创建同步账号

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.171.135' identified by 'repl123!';

Query OK, 0 rows affected (0.001 sec)


MariaDB [(none)]> quit

Bye

//写配置文件

[root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf

22 log-bin = mysql_bin

23 server-id = 10

[root@master ~]# systemctl restart mariadb

[root@master ~]# mysql -uroot -p123com

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql_bin.000001 | 328 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.000 sec)


//关闭防火墙和selinux

[root@master ~]# systemctl disable --now firewalld

Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@master ~]# setenforce 0


//配置从库

[root@web ~]# vim /etc/my.cnf.d/mariadb-server.cnf

22 server-id = 20

23 relay-log = myrelay

[root@web ~]# systemctl restart mariadb

[root@web ~]# mysql -uroot -p123com

MariaDB [(none)]> change master to

-> master_host='192.168.171.134',

-> master_user='repl',

-> master_password='repl123!',

-> master_log_file='mysql_bin.000001',

-> master_log_pos=328;

Query OK, 0 rows affected (0.003 sec)


MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.001 sec)


MariaDB [(none)]> show slave status\G;

······

Slave_IO_Running: Yes

Slave_SQL_Running: Yes


······


//测试验证主从是否同步

[root@master ~]# mysql -uroot -p123com

MariaDB [(none)]> create database george;

Query OK, 1 row affected (0.000 sec)


MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| george |

| information_schema |

| mysql |

| performance_schema |

+--------------------+

4 rows in set (0.000 sec)

[root@web ~]# mysql -uroot -p123com

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| george |

| information_schema |

| mysql |

| performance_schema |

+--------------------+

4 rows in set (0.000 sec)
 
//在从库修改配置文件

[root@web ~]# vim /usr/local/etc/zabbix_agentd.conf

UserParameter=check_mysql_status,/bin/bash /scripts/mysql_status.sh //在末行加入此配置

//重启生效

[root@web ~]# pkill zabbix_agentd

[root@web ~]# zabbix_agentd

[root@web ~]# ss -antl

State Recv-Q Send-Q Local Address:Port Peer Address:Port Process

LISTEN 0 128 0.0.0.0:22 0.0.0.0:*

LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*

LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*

LISTEN 0 128 [::]:22 [::]:*

//编写脚本

[root@web ~]# vim /scripts/mysql_status.sh

#!/bin/bash

yes=`mysql -uroot -p123com -e " show slave status\G" 2> /dev/null |grep "Running:"|grep -c "Yes"`

if [ $yes == 2 ]; then

echo "0"

else

echo "1"

fi


[root@web ~]# chmod +x /scripts/mysql_status.sh


//在服务端检测key是否可用

[root@zabbix ~]# zabbix_get -s 192.168.171.136 -k check_mysql_status

0

添加监控项

 

 

 

添加触发器

 

 

 

手动触发告警

 
[root@web ~]# mysql -uroot -p123com -e "stop slave;"

 

监控mysql主从延迟

配置监控脚本

 
//在从库添加配置

[root@web ~]# vim /usr/local/etc/zabbix_agentd.conf

UserParameter=check_mysqlyc,/bin/bash /scripts/mysqlyc.sh


//重启生效

[root@web ~]# pkill zabbix_agentd

[root@web ~]# zabbix_agentd


//编写脚本

[root@web ~]# vim /scripts/mysqlyc.sh

#!/bin/bash

yc=`mysql -uroot -p123com -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `

echo $yc


[root@web ~]# chmod +x /scripts/mysqlyc.sh


//在服务端检测key是否可用

[root@zabbix ~]# zabbix_get -s 192.168.171.135 -k check_mysqlyc

0

添加监控项

 

 

 

 

 

用户和组

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值