赞
踩
zabbix监控软件可以用来控制日志,进程,端口等,而mysql主库与从库之间的复制功能状态,
mysql主从复制延迟也可以被zabbix监控,mysql主从复制状态发生故障,zabbix发邮件进行报警。
想要实现zabbix监控mysql主从,必须先搭建mysql主从复制,那么跟随我的脚步去完成这个zabbix监控。
系统 | IP | 主机名 | 安装需求 |
---|---|---|---|
centos8 | 192.168.136.233 | zabbix.example.com | lamp,zabbix |
centos8 | 192.168.136.242 | mysql-master | 二进制安装mysql |
centos8 | 192.168.136.230 | mysql-slave | 二进制安装mysql,zabbix-agent |
可以点击下列链接进行搭建安装:
lamp脚本一键安装(php不能为8.0版本)
zabbix搭建
二进制安装mysql脚本如下:
//分别在mysql-master,mysql-slave俩台主机进行mysql二进制安装 1.脚本目录 [root@mysql-slave ~]# tree /tmp/ /tmp/ ├── install_mysql.sh ├── soft │ └── mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz 2.给执行权限进行安装 [root@mysql-master tmp]# chmod +x install_mysql.sh [root@mysql-slave tmp]# chmod +x install_mysql.sh [root@mysql-master tmp]# bash install_mysql.sh [root@mysql-slave tmp]# bash install_mysql.sh 3.分别查看mysql端口 [root@mysql-master ~]# 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 80 *:3306 *:* LISTEN 0 128 [::]:22 [::]:* [root@mysql-slave ~]# 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 [::]:22 [::]:* LISTEN 0 80 *:3306 *:*
#主库授权用户 [root@mysql-master ~]# mysql -uroot -p mysql> grant replication slave on *.* to 'tom'@'192.168.136.230' identified by 'tom'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #放行3306端口 [root@mysql-master ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent [root@mysql-master ~]# firewall-cmd --reload [root@mysql-slave ~]# firewall-cmd --zone=public --add-port=3306 --permanent [root@mysql-slave ~]# firewall-cmd --reload #从库进行测试连接 [root@mysql-slave ~]# mysql -utom -ptom -h192.168.136.242 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.33 MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> #主库修改配置文件 [root@mysql-master ~]# vim /etc/my.cnf [mysqld] user=mysql port=3306 basedir=/usr/local/mysql datadir=/opt/data pid-file=/opt/data/mysql.pid socket=/tmp/mysql.sock skip-name-resolve log-bin=mysql_bin #启动binlog日志 server-id=10 #数据库服务器唯一标识,id必须比从数据库小 #主库重启 [root@mysql-master ~]# systemctl restart mysqld [root@mysql-master ~]# mysql mysql> show master status; #查看主库状态 +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql_bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) #配置从库文件 [root@mysql-slave ~]# cat /etc/my.cnf [mysqld] user=mysql port=3306 basedir=/usr/local/mysql datadir=/opt/data pid-file=/opt/data/mysql.pid socket=/tmp/mysql.sock skip-name-resolve server-id=20 #服务id比主库的大 relay-log=mysql_relay_log #启动中继日志 #重启从库 [root@mysql-slave ~]# systemctl restart mysqld.service #从库上配置主从复制的功能 [root@mysql-slave ~]# mysql -uroot -p mysql> change master to -> master_host='192.168.136.242', -> master_user='tom', -> master_password='tom', -> master_log_file='mysql_bin.000001', #主库上查看的日志文件 -> master_log_pos=154; #主库上查看的日志情报数值 Query OK, 0 rows affected, 2 warnings (0.01 sec) #开启功能 mysql> slave start; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.136.242 Master_User: tom Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysql_relay_log.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql_bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes #io线程和sql线程都以及开启
搭建mysql主从复制测试
#主库上创建tom库 [root@mysql-master ~]# mysql -uroot -p mysql> create database tom; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | tom | +--------------------+ #从库上查看 [root@mysql-slave ~]# mysql -uroot -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | tom | +--------------------+ //此刻mysql主从复制搭建成功!!!
#创建zabbix用户 [root@mysql-slave ~]#useradd -r -M -s /sbin/nologin zabbix #进行zabbix编译安装 [root@mysql-slave ~]# yum -y install gcc gcc-c++ make pcre-devel openssl-devel [root@mysql-slave ~]# cd /usr/src/ [root@mysql-slave src]# tar -xf zabbix-5.4.4.tar.gz [root@mysql-slave src]# ls debug kernels zabbix-5.4.4 zabbix-5.4.4.tar.gz [root@mysql-slave src]# cd zabbix-5.4.4/ aclocal.m4 bin ChangeLog conf config.sub configure.ac database include install-sh Makefile.am man missing README src AUTHORS build compile config.guess configure COPYING depcomp INSTALL m4 Makefile.in misc NEWS sass ui [root@mysql-slave zabbix-5.4.4]# ./configure --enable-agent --with-openssl *********************************************************** * Now run 'make install' * * * * Thank you for using Zabbix! * * <http://www.zabbix.com> * *********************************************************** [root@mysql-slave zabbix-5.4.4]#make install 略... #修改zabbix_agent配置文件 [root@mysql-slave ~]# vim /usr/local/etc/zabbix_agentd.conf Hostname=V258175X #本机名称标识,只要是唯一就行 Server=192.168.136.233 #被动模式,zabbix服务端端ip ServerActive=192.168.136.233 #主动模式,zabbix服务端端ip #放行10050端口 [root@mysql-slave ~]# firewall-cmd --zone=public --add-port=10050/tcp --permanent success [root@mysql-slave ~]# firewall-cmd --zone=public --add-port=10050/udp --permanent success [root@mysql-slave ~]# firewall-cmd --reload success #开启10050端口 [root@mysql-slave ~]# zabbix_agentd [root@mysql-slave ~]# 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 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:*
#查看zabbix.example.com主机上的端口
[root@zabbix src]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:3000 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
web界面配置mysql从库这个主机
//为了mysq从库上密码的安全性,有俩个方法可以避免看到密码 //方法一:配置从库的.my.cnf [root@mysql-slave ~]# cat .my.cnf [client] user = root password = 8000 #密码写到这个文件中 #mysql从库上配置脚本(方法一) [root@mysql-slave ~]# mkdir /scripts [root@mysql-slave ~]# cd /scripts/ [root@mysql-slave scripts]# vim check_replication.sh #!/bin/bash count=$( mysql -e "show slave status\G;"|grep -v 'State'|grep '_Running'|grep -c 'Yes') if [ $count -eq 2 ];then echo 0 else echo 1 fi //方法二:从库上授权zabbix用户 mysql> grant super,replication client on *.* to 'zabbix'@'localhost' identified by 'zabbix123'; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #mysql从库上配置脚本(我们采用方法二) [root@mysql-slave ~]# mkdir /scripts [root@mysql-slave ~]# cd /scripts/ [root@mysql-slave scripts]# cat check_replication.sh #!/bin/bash count=$( mysql -uzabbix -pzabbix123 -e "show slave status\G;" 2>/dev/null |grep -c 'Yes') if [ $count -eq 2 ];then echo 0 else #取到从库上面的俩个(io|sql线程)均是yes输出0,否则则输出1 echo 1 fi
[root@mysql-slave ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters #开启监控项
UserParameter=check_replication,/scripts/check_replication.sh 指定自定义监控脚本参数
#重启zabbix_agentd
[root@mysql-slave ~]# killall zabbix_agentd
[root@mysql-slave ~]# zabbix_agentd
zabbix主机上进行测试脚本
[root@zabbix ~]# zabbix_get -s 192.168.136.230 -k check_replication
0
#从库上关闭主从复制功能
[root@mysql-slave ~]# mysql -uroot -p
mysql> stop slave;
#再次检查发现为1,则出现问题了
Query OK, 0 rows affected (0.00 sec)
[root@zabbix ~]# zabbix_get -s 192.168.136.230 -k check_replication
1
#从库关闭主从复制功能
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
[root@mysql-slave scripts]# cat check_replication_delay.sh
#!/bin/bash #Behind:落后主库多少秒,存在秒数则出现主库复制之间的延迟
count=$(mysql -uzabbix -pzabbix123 -e "show slave status\G;" 2>/dev/null |grep 'Behind'|awk '{print $2}')
if [ $count == "NULL" ];then
echo 0
elif [ $count -ge 0 ] && [ $count -le 200 ];then
echo 0 #只要当延迟数据为NULL,以及0-200是正常的,否则其它数字输入1表示错误
else
echo $count
fi
[root@mysql-slave scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication,/scripts/check_replication.sh
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh
#重启zabbix_agentd
[root@mysql-slave scripts]# killall zabbix_agentd
[root@mysql-slave scripts]# zabbix_agentd
[root@zabbix ~]# zabbix_get -s 192.168.136.230 -k check_replication_delay
0
//可以看到能够取到值
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。