1:zabbix监控主从
部署mysql主从
环境用到两台centos8的操作系统
一台为master一台为slave
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]#
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
[root@slave ~]#
首先将阿里云的源仓库下载下来
[root@slave ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@slave ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@master ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
下载mariadb
[root@master ~]# yum -y install mariadb-server mariadb
[root@slave ~]# yum -y install mariadb-server mariadb
初始化数据库
[root@master ~]# systemctl start mariadb.service
[root@master ~]# mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n]
New password:
Re-enter new password:
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]
[root@slave ~]# systemctl start mariadb.service
[root@slave ~]# mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n]
New password:
Re-enter new password:
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]
关闭selinux和防火墙
[root@slave ~]# systemctl stop firewalld.service
sys[root@slave ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# vim /etc/selinux/config
SELINUX=disabled
[root@slave ~]# setenforce 0
[root@master ~]# systemctl stop firewalld.service
syst[root@master ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vim /etc/selinux/config
SELINUX=disabled
[root@master ~]# setenforce 0
修改数据库配置文件
[root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf
log-bin=g
server-id=2000
[root@master ~]# systemctl restart mariadb.service
[root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf
log-bin=j
server-id=2001
[root@slave ~]# systemctl restart mariadb.service
进数据库配置主从
主
[root@master ~]# mysql -uroot -p123456
MariaDB [(none)]> show master status;
+----------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------+----------+--------------+------------------+
| g.000001 | 320 | | |
+----------+----------+--------------+------------------+
MariaDB [(none)]> create user 'xuanning'@'192.168.226.158' identified by '123456';
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'xuanning'@'192.168.226.158';
MariaDB [(none)]> flush privileges;
从
[root@slave ~]# mysql -uroot -p123456
MariaDB [(none)]> change master to
-> master_host='192.168.226.139' ,
-> master_user = 'xuanning' ,
-> master_password = '123456' ,
-> master_log_file = 'g.000001' ,
-> master_log_pos = 320;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status \G
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
主从配置完毕将slave加入到监控平台中
首先下载zabbix的源这里我zabbix服务端有源仓库故直接拷贝过来
[root@slave ~]# scp [email protected]:/etc/yum.repos.d/* /etc/yum.repos.d/
修改/etc/hosts
[root@slave ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.139 server
192.168.226.158 agent1
192.168.226.162 master
192.168.226.163 slave
下载zabbix-agent软件包
[root@slave ~]# yum -y install zabbix-agent
修改zabbix-agent配置文件
[root@slave ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.226.139
ServerActive=192.168.226.139
Hostname=slave
[root@slave ~]# systemctl restart zabbix-agent.service
创建主机
在slave上创建脚本脚本用于检测主从状态
[root@slave ~]# mkdir /etc/zabbix/script
[root@slave ~]# cd /etc/zabbix/script/
[root@slave script]# vim mysql.sh
[root@slave script]# cat mysql.sh
#!/bin/bash
USER="root"
PASSWD="123456"
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@slave script]# chown -R zabbix.zabbix /etc/zabbix/script/
[root@slave script]# chmod +x mysql.sh
创建自定义键值
[root@slave script]# cd /etc/zabbix/zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# cat userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql.sh $1
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service
创建监控项
测试
将主从复制停止
[root@slave ~]# mysql -uroot -p123456
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)
将主从复制开启
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
2:监控主从延迟
编写脚本
[root@slave ~]# cd /etc/zabbix/script/
[root@slave script]# vim mysql_deley.sh
[root@slave script]# cat mysql_deley.sh
#!/bin/bash
delay=$(mysql -uroot -p123456 -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@slave script]# chown zabbix.zabbix mysql_deley.sh
[root@slave script]# chmod +x mysql_deley.sh
自定义键值
[root@slave script]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.c onf
[root@slave script]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.c onf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql.sh $1
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_deley.sh
[root@slave script]# systemctl restart zabbix-agent.service
创建监控项
此处由于环境无法做出延迟故让他等于0一直报警测试
3:zabbix创建用户和组
创建主机群组
创建主机
创建用户组
创建用户
用户的权限有三种
1:用户,默认情况下用户无任何权限,必须明确指定对主机组的任何权限
2:管理员,用户可以访问配置页面
3:超级管理员,用户可以对所以主机拥有所有权限
用户权限
登录
界面没有配置选项,无法进行配置
管理员权限
界面
可以看见能够对主机组内的主机进行配置
超级管理员权限