#/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
相关文章
- 09-05MySQL备份1356错误提示修复办法
- 09-05mysql多线程备份修复主从1062错误
- 09-05MySQL GTID 主从复制错误修复方法
- 09-05解决mysql 1062 主从错误