mysql的主从配置+shell脚本的检测和邮箱报警(版本centos7)

1.关闭防火墙

systemctl stop firewalld
setenforce 0

2.安装mysql

yum -y install mariadb mariadb-server

3.mysql的主从配置(主)

vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
systemctl start mariadb

进入mysql中,授权一个从连接主的用户tom

grant all on *.* to tom@'%' identified by '123';

查看二进制日志

MariaDB [(none)]> show master status;

在这里插入图片描述

4.mysql的主从配置(从)

vim /etc/my.cnf
[mysqld]
server-id=2
relay-log=mysql-relay
systemctl start mariadb

停掉主从同步

MariaDB [(none)]> stop slave;

同步的主机IP,用户名,密码,binlog文件,binlog位置等信息

MariaDB [(none)]> change master to
    -> master_host='192.168.116.129',
    -> master_user='tom',
    -> master_password='123',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=245;
Query OK, 0 rows affected (0.03 sec)

开启主从同步

MariaDB [(none)]> start slave;

验证主从是否同步正常(双Yes)

MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                    Master_Host: 192.168.116.129
                    Master_User: tom
                    Master_Port: 3306
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000001
            Read_Master_Log_Pos: 451
                 Relay_Log_File: mysql-relay.000002
                  Relay_Log_Pos: 735
          Relay_Master_Log_File: mysql-bin.000001
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes

5.安装配置mail

yum -y install mailx
vim /etc/mail.rc
#########################
set from=xxxxxxxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxxxxxxx@qq.com
set smtp-auth-password=xxxxxxxxxxx(此处为邮箱授权码)
set smtp-auth=login
########################

6.编写脚本

 vim /shell/check_mysql.sh
#!/bin/bash
num=`mysql -e "show slave status \G" | grep Yes | wc -l`
if [ $num -eq 2 ];then
        echo "mysql 主从正常!"
else
        echo "mysql 主从异常,请及时修正!" | mail -s "check mail" xxxxxxxxx@qq.com
fi

7.进行脚本测试

[root@localhost ~]#chmod +x /shell/check_mysql.sh
[root@localhost shell]#cd /shell/
[root@localhost shell]#./check_mysql.sh
[root@localhost ~]# sh /shell/check_mysql.sh 
mysql 主从正常!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值