您好,欢迎访问一九零五行业门户网

关于mysqldump的实例详解

部分生产环境采用mysqldump --single-transaction的方式在夜间进行数据库备份,而同事恰好在备份期间执行了alter table操作,操作部分成功部分失败,为啥呢?
测试在mysql 5.6.36上执行,该问题存在版本差异!
##========================================================================##
在mysqldump对single-transaction参数的解释为:
creates a consistent snapshot by dumping all tables in a single transaction. works only for tables stored in storage engines which support multiversioning (currently only innodb does); the dump is not guaranteed to be consistent for other storage engines. while a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log position), no other connection should use the following statements: alter table, drop table, rename table, truncate table, as consistent snapshot is not isolated from them. option automatically turns off --lock-tables.
红色字体部分是重点,但是看得有些迷糊,还是动手测试下。
根据《mysqldump的几个主要选项探究》的介绍,我们备份执行的命令mysqldump --single-transaction --master-data相当于执行下面代码:
flush tables; flush tables with read lock;set session transaction isolation level repeatable read; start transaction with consistent snapshot; show master status; unlock tables; show tables like 'xxx'set option sql_quote_show_create=1shwo create table 'xxx'show fields from 'xxx'show table status like 'xxx'select /*!40001 sql_no_cache */ * from xxx quit
场景1:mysqldump开始但尚未备份到表tb001时,另外回话对表tb001进行alter操作,然后mysqldump对表tb001进行导出
alter操作顺利完成,但是mysqldump操作失败。
场景2:mysqldump开始备份并完成tb001的导出,在对其他表进行导出过程中,其他回话对表进行alter操作
alter table操作被阻塞直至mysqldump完成或失败后退出。
使用mysqldump备份时,模拟场景2的环境,报错信息为:
mysqldump: error 1412: table definition has changed, please retry transaction when dumping table `tb1002` at row: 0
查看导出文件,最后内容为:
-- -- dumping data for table `tb1002`--lock tables `tb1002` write;/*!40000 alter table `tb1002` disable keys */;
总结:
single-transaction参数通过innodb的多版本来获得数据一致性,而alter table, drop table, rename table,truncate table等操作会破坏数据一致性,两种操作不能并发执行。
如果修改表操作在 ”mysqldump开启后但还未导出修改表数据前“ 的时间段内开始,则修改表操作成功完成,而mysqldump会执行失败;
如果修改表操作在 “mysqldum已导出修改表数据但还未结束mysqldump操作前”的时间段内开始,则修改表操作被阻塞,mysqldum能成功完成,在mysqldump操作完成后修改表操作方可正常执行。
以上就是关于mysqldump的实例详解的详细内容。
其它类似信息

推荐信息