暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mysqldump中的master-data与single-transaction

残雪的平凡之路 2017-08-10
1506

如今mysql数据库中InnoDB已成为主流,针对库的备份,里面全是InnoDB引擎的表,通常使用master-data与single-transaction,但以前没仔细研究


查看官网文档,发现两个参数的作用,整理如下:


master-data:

    1、默认是1,用在dump时是否有如下语句生成:

        --

        -- Position to start replication or point-in-time recovery from

        --


        -- CHANGE MASTER TO MASTER_LOG_FILE='二进制文件', MASTER_LOG_POS=位置;


    2、若设置为1,则CHANGE MASTER TO语句不会被注释,为2会被注释,此时提供参考

    3、This option requires the RELOAD privilege and the binary log must be enabled.

    4、It automatically turns off --lock-tables.(因为--lock-tables不能保证dump所有schema的一致性,只能保证同一个schema里面的数据一致性,所以通常用于单库或单库里面部分表) 

    5、It also turns on --lock-all-tables(可以避免--lock-tables的问题), unless --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the beginning of the dump. In all cases, any action on logs happens at the exact moment of the dump.


single-transaction

    This option sets the transaction isolation mode to REPEATABLE READ and sends a START TRANSACTION SQL statement to the server before dumping data.这两个就保证了同一事务内的可重复读。It is useful only with transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time when START TRANSACTION was issued without blocking any applications.只有当表全部是事务引擎才可以用

    While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log coordinates), no other connection should use the following statements: ALTER TABLE, CREATE TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.即dump期间不能有DDL语句

    The --single-transaction option and the --lock-tables option are mutually exclusive because LOCK TABLES causes any pending transactions to be committed implicitly.

    To dump large tables, combine the --single-transaction option with the --quick option.


所以single-transaction是为了获取一致性,但表数据还是会变的,所以为了后面的恢复参考了mysqldump的-F选项,针对InnoDB引入了master-data,用于获取事务开启时二进制日志文件信息,因为是InnoDB所以引入master-data后备份是并不会启用-x选项,所以不会阻塞数据表的写操作,实现了热备份


正如上面single-transaction的解释中说明一样,备份期间不能有DDL语句,这样才能保证一致性,一般线上数据库的DDL操作只能是DBA操作,所以较好控制


所以通常使用的方式如下:不建议设置 --master-data=1,恢复时先进行确认,若没问题去掉注释即可

mysqldump -uxxx -pxx --master-data=2 --single-transaction -BER db1 db2 > xxx.sql


下面是msyql官方版本中使用general_log跟踪mysqldump时--master-data与--single-transaction作用


1、--single-transaction --master-data比--single-transaction多了如下3句用于获取二进制日志信息

 

2、--single-transaction --master-data比--master-data多了如下3句用于控制事务信息

 

3、--single-transaction 与--master-data各自执行的3条语句



文章转载自残雪的平凡之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论