mysql多线程备份修复主从1062错误
Posted lishug
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql多线程备份修复主从1062错误相关的知识,希望对你有一定的参考价值。
#/bin/bash
##该脚本主要用于修复主从同步中的1062错误
##执行脚本前,先检查是哪一个复制线程出错,即获取WORKER_ID的值
user=root
passwd=Fuck@anquan1.COM
while true
do
mysql -u$user -p$passwd -e "show slave statusG" 2>/dev/null | grep 1062 1>/dev/null
if [ $? -eq 0 ];then
#sql thread状态
sql_status=`mysql -u$user -p$passwd -e "show slave statusG" 2>/dev/null | grep ‘Slave_SQL_Running‘ | awk ‘{print $2}‘ | head -1`
#错误id
error_id=`mysql -u$user -p$passwd -e "show slave statusG" 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 statusG" 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=0G" 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错误的主要内容,如果未能解决你的问题,请参考以下文章