1、部署mysql主从,使用mariadb进行操作,将server、agent ,master、slave主机的/etc/hosts文件全部设置为
192.168.253.139 server.example.com server
192.168.253.140 agent1.example.com agent
192.168.253.129 master.example.com master
192.168.253.130 slave.example.com slave
2. master和slave操作系统是centos8 将centos8的安装源下载下来,然后两台主机都安装mariadb mariadb-server
[root@129-master yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https:
[root@129-master yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d'
[root@129-master yum.repos.d]# cd
[root@129-master ~]# yum -y install mariadb-server mariadb
[root@129-master ~]# systemctl restart mariadb
[root@129-master ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/
[root@130-slave ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https:
[root@130-slave ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/Ce
[root@130-slave ~]# yum -y install mariadb-server mariadb
[root@130-slave ~]# systemctl restart mariadb
[root@130-slave ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/
3. 两台主机都虚拟化MySQL数据库
[root@129-master ~]# mysql_secure_installation
[root@130-slave ~]# mysql_secure_installation
4. 修改数据库配置文件,然后两台主机都重启mariadb服务
master
[root@129-master ~]# cat /etc/my.cnf
添加两行数据
[mysqld]
log_bin=mysql-bin
server_id=20
[root@129-master ~]# systemctl restart mariadb
slave
[root@130-slave ~]# vim /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=30
[root@130-slave ~]# systemctl restart mariadb
6.进入数据库配置主从
master:
[root@129-master ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB-log 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)]> grant all privileges on *.* to root@'%' identified by "1";
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by '1';
Query OK, 0 rows affected (0.000 sec)
slave
[root@130-slave ~]# mysql -u root -p
Enter password:
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "1";
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='1';
Query OK, 0 rows affected (0.004 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: master
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
6. 在slave主机中安装zabbix-agent软件包,将slave添加到zabbix web监控平台中
将server主机的zabbix.repo复制过来,然后将yum源中的8改成7,接着安装zabbix-agent
[root@130-slave ~]# cat /etc/yum.repos.d/zabbix.repo
[aliyun]
name=aliyun
baseurl=https:
enable=1
gpgcheck=0
[qinghua]
name=Zabbix Official Repository - $basearch
#baseurl=http:
baseurl=https:
enabled=1
gpgcheck=0
gpgkey=file:
[root@130-slave ~]# yum -y install zabbix-agent
7.修改 /etc/zabbix/zabbix_agentd.conf,重启服务
[root@130-slave ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.253.139
ServerActive=192.168.253.139
Hostname=slave
[root@130-slave ~]# systemctl restart zabbix-agent.service
[root@130-slave ~]# systemctl enable zabbix-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
创建主机
9. 在slave主机上配置脚本
[root@130-slave script]# vim mysql_slave_status.sh
#!/bin/bash
USER="root"
PASSWD="1"
NAME=$1
function IO {
Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
function SQL {
Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0
else
echo 1
fi
}
case $NAME in
io)
IO
;;
sql)
SQL
;;
*)
echo -e "Usage: $0 [io | sql]"
esac
[root@130-slave zabbix]# chmod +x mysql_slave_status.sh
[root@130-slave zabbix]# chown -R zabbix.zabbix /etc/zabbix/script/
10. 编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务
[root@130-slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
[root@130-slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@130-slave zabbix_agentd.d]# systemctl restart zabbix-agent.service
创建监控项 创建触发器 创建图形
11.将mysql主从关闭,查看zabbix告警信息,验证邮箱是否能接收到邮件
将mysql主从停止
测试
[root@130-slave ~]# mysql -uroot -p1
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)
将mysql主从开启
测试
MariaDB [(none)]> start slave;
zabbix监控主从延迟
1、配置库脚本
[root@130-slave script]# vim mysql_deley.sh
[root@130-slave script]# cat mysql_deley.sh
#!/bin/bash
delay=$(mysql -uroot -predhat -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
if [ $delay == "NULL" ];then
echo 0
elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
echo 0
else
echo $delay
fi
[root@130-slave script]# chown zabbix.zabbix mysql_deley.sh
[root@130-slave script]# chmod +x mysql_deley.sh
2.配置agentd文件,并重启服务
[root@130-slave script]# systemctl restart zabbix-agent.service
[root@130-slave script]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh
[root@130-slave script]# chown -R zabbix.zabbix mysql_delay.sh
[root@130-slave script]# chmod +x mysql_delay.sh
[root@130-slave script]# systemctl restart zabbix-agent.service
[root@130-slave script]# ./mysql_delay.sh
0
创建监控项
创建触发器
zabbix创建主机组
1、创建主机–以master主机为例
Master:
[root@master ~]# scp root@192.168.253.139:/etc/yum.repos.d
2、在zabbix web平台创建主机组
3.创建主机
创建用户组
创建用户
用admin重新登录修改rww用户的权限为管理员
再使用rww登录
|