mysql-1-mysqldump之--single-transaction

来源:互联网 发布:创维电视网络唤醒 编辑:程序博客网 时间:2024/06/08 07:54

innodb引擎的数据库,在dump时加--single-transaction参数用来保证数据完整一致。

实际工作原理是设定本次会话的隔离级别为:REPEATABLE READ,来确保本次dump时,不会看到其他会话提交的数据。

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ

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.


通过将导出操作封装在一个事务内来使得导出的数据是一个一致性快照。只有当表使用支持MVCC的存储引擎(目前只有InnoDB)时才可以工作;其他引擎不能保证导出是一致的。当导出开启了--single-transaction选项时,要确保导出文件有效(正确的表数据和二进制日志位置),就要保证没有其他连接会执行如下语句:ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE,这会导致一致性快照失效。这个选项开启后会自动关闭--lock-tables。