mysql8主从及自恢复脚本

2022-03-24  本文已影响0人  我已不是少年郎

主从需要安装相同的mysql8版本。

1、配置主机

在主机上创建用户

create user lucien@'%' identified with mysql_native_password by '123456';

grant all privileges on *.* to lucien@'%' with grant option;

flush privileges;

修改配置
vim /etc/my.cnf

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

port=3306
server-id=6
gtid-mode=on
log-slave-updates
enforce-gtid-consistency
log-bin=/var/lib/mysql/lucien_logbin
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema

重启
systemctl restart mysqld

查看主机的节点信息
show master status\G

2、配置从机

修改配置
vim /etc/my.cnf

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

port=3306
server-id=81
gtid-mode=on
log-slave-updates
enforce-gtid-consistency
skip-slave-start
max_connections=1000
read-only=1
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema

重启
systemctl restart mysqld

根据主机的节点信息修改以下指令
change master to master_host='192.168.1.81',master_user='lucien',master_password='123456',master_port=3306,master_log_file='lucien_logbin.000001',master_log_pos=34484,master_connect_retry=30;

启动
start slave

3、自动恢复脚本

定时脚本,每1小时执行一次
vim /etc/cron.hourly/lucien_hour.sh

/home/lucien/auto_script/mysql_slave_auto_fix.sh

chmod +x lucien_hour.sh

自恢复脚本

#!/bin/bash

mysql_command=/usr/bin/mysql
mysql_user=root
mysql_pass='#Lucien123!'
mysql_sockfile=/var/lib/mysql/mysql.sock
datetime=`date +"%Y-%m-%d_%H:%M:%S"`
mysql_slave_logfile=/home/lucien/auto_script/check_mysql_slave.log
logs=/home/lucien/auto_script/check.log
slave_name=local81

$mysql_command -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running" &> $mysql_slave_logfile
Slave_IO_Running=`grep Slave_IO_Running $mysql_slave_logfile| awk ' {print $2}'`
Slave_SQL_Running=`grep Slave_SQL_Running: $mysql_slave_logfile | awk '{print $2}'`
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
then
        echo "$datetime $slave_name Slave is Running!" >> $logs
else
        echo " $datetime $slave_name Slave is not running!" >> $logs
cat | $mysql_command -u$mysql_user -p$mysql_pass -S $mysql_sockfile << EOF
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
exit
EOF
fi
上一篇下一篇

猜你喜欢

热点阅读