码迷,mamicode.com
首页 > 数据库 > 详细

mysql多线程备份修复主从1062错误

时间:2020-06-30 19:03:23      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:表名   int   ESS   date   ase   show   冲突   脚本   $2   

#/bin/bash
##该脚本主要用于修复主从同步中的1062错误
##执行脚本前,先检查是哪一个复制线程出错,即获取WORKER_ID的值
user=root
passwd=Fuck@anquan1.COM
while true
do
mysql -u$user -p$passwd -e "show slave status\G" 2>/dev/null | grep 1062 1>/dev/null
if [ $? -eq 0 ];then
#sql thread状态
sql_status=`mysql -u$user -p$passwd -e "show slave status\G" 2>/dev/null | grep ‘Slave_SQL_Running‘ | awk ‘{print $2}‘ | head -1`
#错误id
error_id=`mysql -u$user -p$passwd -e "show slave status\G" 2>/dev/null | grep Last_Errno | awk ‘{print $2}‘`
if [ "$sql_status" == No ] && [ "$error_id" == 1062 ];then
#获取冲突主键值
value=`mysql -u$user -p$passwd -e "show slave status\G" 2>/dev/null | grep Last_SQL_Error | awk -F ";" ‘{ print $2 }‘ | awk -F "‘" ‘{ print $2}‘`
#获取表名
db_table_name=`mysql -u$user -p$passwd -e "select LAST_ERROR_MESSAGE from performance_schema.replication_applier_status_by_worker where WORKER_ID=0\G" 2> /dev/null | grep "LAST_ERROR_MESSAGE" | awk -F ":" ‘{print $2}‘ | awk -F ";" ‘{print $1}‘ | awk ‘{print $NF}‘`
database_name=`echo $db_table_name | awk -F "." ‘{print $1}‘`
table_name=`echo $db_table_name | awk -F "." ‘{print $2}‘`
#备份数据
mysqldump -u$user -p$passwd --socket=/var/lib/mysql/mysql.sock $database_name $table_name --where="id>=$value" 2>/dev/null | gzip > /data/$(date "+%Y-%m-%d").gz
#删除错误数据
mysql -u$user -p$passwd -e 2>/dev/null "delete from $db_table_name where id >= $value;"
if [ $? -eq 0 ];then
echo "delete from $table_name where id = $value; $(date "+%Y-%m-%d %H:%M:%S")" >> /data/$(date "+%Y-%m-%d").log
fi
mysql -u$user -p$passwd -e 2>/dev/null "stop slave sql_thread;"
mysql -u$user -p$passwd -e 2>/dev/null "start slave sql_thread;"
sleep 5
fi
else
echo "Slave_SQL_Running OK"
exit 0
fi
done

mysql多线程备份修复主从1062错误

标签:表名   int   ESS   date   ase   show   冲突   脚本   $2   

原文地址:https://www.cnblogs.com/lishug/p/13215366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!