mysql xtrabackup 保护模式_MySQL Backup--Xtrabackup备份设置锁等待问题

问题描述

innobackupex备份过程需要保证备份数据一致性,通过刷新表缓存和加全局读锁(FLUSH TABLES WITH READ LOCK)获取备份位点,而为防止锁等待超时,会先设置:

SET SESSION lock_wait_timeout=31536000

在MySQL中,导致FLUSH TABLES WITH READ LOCK操作获取锁超时的场景有:

1、超长事务或超大DML操作

2、超大表DDL操作

如果要解决锁超时问题,可以从加锁和KILL超长查询两方面入手,innobackupex命令有如下参数:

--no-lock

Use this option to disable table lock with "FLUSH TABLES

WITH READ LOCK". Use it only if ALL your tables are

InnoDB and you DO NOT CARE about the binary log position

of the backup. This option shouldn't be used if there are

any DDL statements being executed or if any updates are

happening on non-InnoDB tables (this includes the system

MyISAM tables in the mysql database), otherwise it could

lead to an inconsistent backup. If you are considering to

use --no-lock because your backups are failing to acquire

the lock, this could be because of incoming replication

events preventing the lock from succeeding. Please try

using --safe-slave-backup to momentarily stop the

replication slave thread, this may help the backup to

succeed and you then don't need to resort to using this

option.

--no-backup-locks

This option controls if backup locks should be used

instead of FLUSH TABLES WITH READ LOCK on the backup

stage. The option has no effect when backup locks are not

supported by the server. This option is enabled by

default, disable with --no-backup-locks.

--kill-long-query-type=name

This option specifies which types of queries should be

killed to unblock the global lock. Default is "all".

--kill-long-queries-timeout=#

This option specifies the number of seconds innobackupex

waits between starting FLUSH TABLES WITH READ LOCK and

killing those queries that block it. Default is 0

seconds, which means innobackupex will not attempt to

kill any queries.

如果在从库上执行备份,可以考虑暂停复制进程来防止阻塞:

--safe-slave-backup

Stop slave SQL thread and wait to start backup until

Slave_open_temp_tables in "SHOW STATUS" is zero. If there

are no open temporary tables, the backup will take place,

otherwise the SQL thread will be started and stopped

until there are no open temporary tables. The backup will

fail if Slave_open_temp_tables does not become zero after

--safe-slave-backup-timeout seconds. The slave SQL thread

will be restarted when the backup finishes.

--safe-slave-backup-timeout=#

How many seconds --safe-slave-backup should wait for

Slave_open_temp_tables to become zero. (default 300)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个自动化增量备份MySQL8的脚本,但在运行脚本之前,您需要先安装xtrabackup工具。以下是脚本的内容: ``` #!/bin/bash # 设置备份相关的变量 MYSQL_USER="your_username" MYSQL_PASSWORD="your_password" BACKUP_DIR="/path/to/backup/directory" DATE=$(date +%F_%H-%M-%S) FULL_BACKUP_INTERVAL=7 # 满备份间隔(天) LAST_FULL_BACKUP=$(find $BACKUP_DIR -maxdepth 1 -name "full_backup*" | sort -nr | head -n 1) # 上一次的全量备份 INCREMENTAL_BACKUP_DIR="$BACKUP_DIR/incremental_backup_$DATE" LATEST_INCREMENTAL_BACKUP=$(find $BACKUP_DIR -maxdepth 1 -name "incremental_backup_*" | sort -nr | head -n 1) # 最新的增量备份 # 创建增量备份目录 mkdir -p $INCREMENTAL_BACKUP_DIR # 运行增量备份命令 if [ -z "$LAST_FULL_BACKUP" ]; then # 如果没有全量备份,则进行全量备份 xtrabackup --backup --user=$MYSQL_USER --password=$MYSQL_PASSWORD --target-dir=$INCREMENTAL_BACKUP_DIR else # 如果已有全量备份,则进行增量备份 xtrabackup --backup --user=$MYSQL_USER --password=$MYSQL_PASSWORD --target-dir=$INCREMENTAL_BACKUP_DIR --incremental-basedir=$LAST_FULL_BACKUP fi # 判断是否需要进行全量备份 if [ -z "$LAST_FULL_BACKUP" ] || [ $(expr $(date +%s) - $(date -d $(basename $LAST_FULL_BACKUP | cut -d '_' -f 3) +%s)) -ge $(expr $FULL_BACKUP_INTERVAL \* 86400) ]; then # 如果没有全量备份或者距离上次全量备份已经超过满备份间隔,则进行全量备份 FULL_BACKUP_DIR="$BACKUP_DIR/full_backup_$DATE" mkdir -p $FULL_BACKUP_DIR xtrabackup --backup --user=$MYSQL_USER --password=$MYSQL_PASSWORD --target-dir=$FULL_BACKUP_DIR fi # 删除过期的备份 find $BACKUP_DIR -maxdepth 1 -name "full_backup*" -mtime +$FULL_BACKUP_INTERVAL -exec rm -rf {} \; find $BACKUP_DIR -maxdepth 1 -name "incremental_backup_*" -mtime +$FULL_BACKUP_INTERVAL -exec rm -rf {} \; ``` 请将`your_username`和`your_password`替换为您的MySQL用户名和密码,将`/path/to/backup/directory`替换为您希望存储备份的目录。运行脚本时,将其保存为`.sh`文件并添加执行权限,然后在命令行中运行即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值