当前位置:   article > 正文

自定义监控mysql主从状态与主从延迟_mysql active: inactive (dead)

mysql active: inactive (dead)

一.准备工作

环境部署:

server:ip——192.168.80.128——服务器

agent:ip——192.168.80.130——客户端,从库

master:ip——192.168.80.129——主库

前提:

server主机安装 zabbix_server zabbix_agentd   
agent主机安装zabbix_agent  
在master和agent安装mysql  

  1. //关闭主从端的防火墙和selinux
  2. [root@master ~]# systemctl status firewalld.service
  3. ● firewalld.service - firewalld - dynamic firewall daemon
  4. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  5. Active: inactive (dead)
  6. Docs: man:firewalld(1)
  7. [root@agent ~]# systemctl status firewalld.service
  8. ● firewalld.service - firewalld - dynamic firewall daemon
  9. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  10. Active: inactive (dead) since Thu 2022-07-08 8:04:09 CST; 4 days ago
  11. Docs: man:firewalld(1)
  12. Main PID: 978 (code=exited, status=0/SUCCESS)

1.搭建mysql主从

配置主库

  1. //在主库创建用户repl授权给从库使用
  2. mysql> CREATE USER 'repl'@'192.168.80.130' IDENTIFIED BY 'repl123';
  3. Query OK, 0 rows affected (0.00 sec)
  4. mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.80.130';
  5. Query OK, 0 rows affected (0.00 sec)
  6. mysql> FLUSH PRIVILEGES;
  7. Query OK, 0 rows affected (0.00 sec)

配置主库文件

  1. [root@master ~]# cat /etc/my.cnf
  2. [mysqld]
  3. basedir = /usr/local/mysql
  4. datadir = /opt/data
  5. socket = /tmp/mysql.sock
  6. port = 3306
  7. pid-file = /opt/data/mysql.pid
  8. user = mysql
  9. skip-name-resolve
  10. server-id=10
  11. log-bin=mysql_bin
  12. [root@master ~]# systemctl restart mysqld.service
  13. mysql> show master status; //里面的参数后续会用到
  14. +------------------+----------+--------------+------------------+-------------------+
  15. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  16. +------------------+----------+--------------+------------------+-------------------+
  17. | mysql_bin.000001 | 154 | | | |
  18. +------------------+----------+--------------+------------------+-------------------+
  19. 1 row in set (0.00 sec)

配置从库

  1. //配置从库文件
  2. [root@agent ~]# cat /etc/my.cnf
  3. [mysqld]
  4. basedir = /usr/local/mysql
  5. datadir = /opt/data
  6. socket = /tmp/mysql.sock
  7. port = 3306
  8. pid-file = /opt/data/mysql.pid
  9. user = mysql
  10. skip-name-resolve
  11. server-id=20
  12. relay-log=mysql-relay-bin
  13. [root@agent ~]# systemctl restart mysqld.service
  14. //连接到从数据库配置主从
  15. mysql> CHANGE MASTER TO
  16. -> MASTER_HOST= '192.168.80.129',
  17. -> MASTER_USER='repl',
  18. -> MASTER_PASSWORD='repl123',
  19. -> MASTER_LOG_FILE='mysql_bin.000001',
  20. -> MASTER_LOG_POS=154;
  21. Query OK, 0 rows affected, 2 warnings (0.03 sec)
  22. mysql> start slave; //启动主从
  23. mysql> show slave status \G
  24. *************************** 1. row ***************************
  25. Slave_IO_State: Waiting for master to send event
  26. Master_Host: 192.168.80.129
  27. Master_User: repl
  28. Master_Port: 3306
  29. Connect_Retry: 60
  30. Master_Log_File: mysql_bin.000001
  31. Read_Master_Log_Pos: 154
  32. Relay_Log_File: mysql-relay-bin.000003
  33. Relay_Log_Pos: 320
  34. Relay_Master_Log_File: mysql_bin.000001
  35. Slave_IO_Running: Yes //此处为双yes 就是成功开启了
  36. Slave_SQL_Running: Yes
  37. Replicate_Do_DB:
  38. Replicate_Ignore_DB:
  39. Replicate_Do_Table:
  40. Replicate_Ignore_Table:
  41. Replicate_Wild_Do_Table:
  42. Replicate_Wild_Ignore_Table:
  43. Last_Errno: 0
  44. Last_Error:
  45. Skip_Counter: 0
  46. Exec_Master_Log_Pos: 154
  47. Relay_Log_Space: 693
  48. Until_Condition: None
  49. Until_Log_File:
  50. Until_Log_Pos: 0
  51. Master_SSL_Allowed: No
  52. Master_SSL_CA_File:
  53. Master_SSL_CA_Path:
  54. Master_SSL_Cert:
  55. Master_SSL_Cipher:
  56. Master_SSL_Key:
  57. Seconds_Behind_Master: 0
  58. Master_SSL_Verify_Server_Cert: No
  59. Last_IO_Errno: 0
  60. Last_IO_Error:
  61. Last_SQL_Errno: 0
  62. Last_SQL_Error:
  63. Replicate_Ignore_Server_Ids:
  64. Master_Server_Id: 10
  65. Master_UUID: 32748ca6-00f6-11ed-a5ea-000c29972037
  66. Master_Info_File: /opt/data/master.info
  67. SQL_Delay: 0
  68. SQL_Remaining_Delay: NULL
  69. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  70. Master_Retry_Count: 86400
  71. Master_Bind:
  72. Last_IO_Error_Timestamp:
  73. Last_SQL_Error_Timestamp:
  74. Master_SSL_Crl:
  75. Master_SSL_Crlpath:
  76. Retrieved_Gtid_Set:
  77. Executed_Gtid_Set:
  78. Auto_Position: 0
  79. Replicate_Rewrite_DB:
  80. Channel_Name:
  81. Master_TLS_Version:
  82. 1 row in set (0.00 sec)

测试

在主库创建一个数据库到从库查看,是否同步

  1. [root@master ~]# mysql -uroot -p'runtime123!' -e 'create database abc;'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. [root@master ~]# mysql -uroot -p'runtime123!' -e 'show databases;'
  4. mysql: [Warning] Using a password on the command line interface can be insecure.
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | abc |
  10. | mysql |
  11. | performance_schema |
  12. | sys |
  13. | test |
  14. +--------------------+

从库查看

  1. [root@agent ~]# mysql -uroot -p'runtime123!' -e 'show databases;'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | abc |
  8. | mysql |
  9. | performance_schema |
  10. | sys |
  11. | test |
  12. +--------------------+

二.自定义监控mysql主从状态

1.编写脚本

因为正常状态为两个yes,所以将两个yes的信息过滤并且做判断即可

  1. [root@agent scropts]# cat mysqlms.sh
  2. #!/bin/bash
  3. count=$( mysql -uroot -pruntime123! -e "show slave status\G" 2>/dev/null | grep -v grep | grep -c 'Yes')
  4. if [ $count -ne 2 ];then //count不等于2打印1 反之打印0
  5. echo '1'
  6. else
  7. echo '0'
  8. fi
  9. ps:
  10. 2>dev/null 去掉告警信息
  11. grep -v grep 将grep本身的进程去掉
  12. grep -c 'Yes' //将匹配yes的行数取出来

2.测试脚本

  1. [root@agent scropts]# chmod +x mysqlms.sh //赋予脚本执行权限
  2. [root@agent scropts]# ll
  3. 总用量 12
  4. -rwxr-xr-x. 1 root root 124 710 21:34 check_process.sh
  5. -rwxr-xr-x. 1 root root 1854 710 22:15 log.py
  6. -rwxr-xr-x. 1 root root 178 711 17:40 mysqlms.sh
  7. [root@agent scropts]# bash mysqlms.sh //打印0 因为主从没问题所以是正常的
  8. 0
  9. [root@agent scropts]#

3.修改配置文件

  1. [root@agent ~]# vim zabbix_agentd.conf
  2. UserParameter=check_mysqlms,/bin/bash /scropts/mysqlms.sh //添加自定义监控
  3. [root@agent ~]# pkill zabbix_agentd
  4. [root@agent ~]# zabbix_agentd //杀死进程 重新启动让配置文件生效
  5. //服务器测试
  6. [root@server ~]# zabbix_get -s 192.168.80.130 -k check_mysqlms
  7. 0

4.zabbix网页配置

添加监控项

添加触发器

当监控项的结果为1时就触发报警

测试

打开主库防火墙

  1. [root@master ~]# systemctl start firewalld.service
  2. [root@master ~]# systemctl status firewalld.service
  3. ● firewalld.service - firewalld - dynamic firewall daemon
  4. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  5. Active: active (running) since Mon 2022-07-11 06:10:40 EDT; 32s ago
  6. Docs: man:firewalld(1)
  7. Main PID: 258109 (firewalld)
  8. Tasks: 2 (limit: 11160)
  9. Memory: 28.8M
  10. CGroup: /system.slice/firewalld.service
  11. └─258109 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
  12. //连接到从库
  13. [root@agent ~]# mysql -u root -p'runtime123!' //进到数据库 重启主从 让其刷新
  14. mysql> stop slave;
  15. Query OK, 0 rows affected (0.00 sec)
  16. mysql> start slave;
  17. Query OK, 0 rows affected (0.01 sec)

查看此时最新数据

数字打印为1,并且触发报警

 三.自定义监控mysql主从延迟

查看mysql主从延迟

  1. mysql> show slave status \G
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.80.129
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql_bin.000001
  9. Read_Master_Log_Pos: 469
  10. Relay_Log_File: mysql-relay-bin.000007
  11. Relay_Log_Pos: 320
  12. Relay_Master_Log_File: mysql_bin.000001
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 469
  25. Relay_Log_Space: 693
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0 //取出这个值 这个是mysql主从延迟
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 10
  43. Master_UUID: 32748ca6-00f6-11ed-a5ea-000c29972037
  44. Master_Info_File: /opt/data/master.info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. 1 row in set (0.00 sec)

1.编写脚本

  1. [root@agent scropts]# cat mysql_delay.sh
  2. #!/bin/bash
  3. delay=$( mysql -uroot -pruntime123! -e "show slave status\G" 2>/dev/null | grep 'Seconds_Behind_Master'| awk '{print $2}')
  4. echo $delay

测试脚本

  1. [root@agent scropts]# chmod +x mysql_delay.sh
  2. [root@agent scropts]# ll
  3. 总用量 16
  4. -rwxr-xr-x. 1 root root 124 79 19:34 check_process.sh
  5. -rwxr-xr-x. 1 root root 1854 79 22:15 log.py
  6. -rwxr-xr-x. 1 root root 151 711 18:36 mysql_delay.sh
  7. -rwxr-xr-x. 1 root root 178 711 17:40 mysqlms.sh
  8. [root@agent scropts]# bash mysql_delay.sh //因为是在虚拟机中所以延迟为0
  9. 0

修改配置文件

  1. [root@agent etc]# cd /usr/local/etc/
  2. [root@agent etc]# cat zabbix_agentd.conf
  3. UserParameter=mysql_delay,/bin/bash /scropts/mysql_delay.sh
  4. [root@agent etc]# pkill zabbix_agentd //杀掉进程让文件生效
  5. [root@agent etc]# zabbix_agentd

服务端测试

  1. [root@server ~]# zabbix_get -s 192.168.80.128 -k mysql_delay
  2. 0

2.zabbix网页配置

添加监控项

添加触发器

 测试

因为虚拟机环境内延迟是0,所以直接触发报警

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/960696
推荐阅读
相关标签
  

闽ICP备14008679号