mysqldump的single-transaction

先看一下--lock-tables和--lock-all-tables

  --lock-all-tables

一次性锁定所有数据库的所有表,在整个dump期间一直获取global read lock

该选项自动关闭—lock-tables—single-transaction

--lock-tables

database为单位,dump前锁定其下所有表;如果是Myisam表则采用 read local模式,允许同时insert

--opt自动启用—lock-tables

注:--opt包含--add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset

 

再看一下官方对于single-transaction的解释

--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.

When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For example, any MyISAM or MEMORY tables dumped while using this option may still change state.

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.

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.

可以总结出以下几点:

只适用于innodb

需要repeatable read模式开启一个事务

执行期间不阻碍DMLDDL,但是不要人工执行alter/create/drop/rename/truncate table

 不能与lock-tables共用,后者执行的LOCK TABLES会隐式提交所有pending事务

执行流程

1SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ

2START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */

3UNLOCK TABLES

如果mysqldump只指定single-transaction,期间不会加锁也不会阻止任何的事务;

 

--master-data

默认启用—lock-all-tables,如果显示指定—single-transaction则弃用—lock-all-tables,此时只在dump开始时短暂获取global read lock

执行流程

1FLUSH  TABLES

2FLUSH TABLES WITH READ LOCK

3SHOW MASTER STATUS

同时使用Master-datasingle-transaction可以对Innodb进行Online backup

shell> mysqldump --all-databases --master-data --single-transaction > all_databases.sql

执行流程

1FLUSH   TABLES

2FLUSH TABLES WITH READ LOCK

3SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ

4START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */

5SHOW MASTER STATUS

6UNLOCK TABLES

global read lock的持续时间很短,获取当前binlog的位置信息后立即释放;

注意:

  在执行时如果当前有一个事务长时间没有结束,那么FLUSH TABLES WITH READ LOCK将会一直等待,而更加严重的是,阻塞的FLUSH TABLES WITH READ LOCK会进一步阻塞后续的DML,从而造成mysql hang


--dump-slave

5.5引入了dump-slave选项,可对slave执行mysqldump,得出的change master to却是指向master,即使用slave创建一个新的slave;

执行期间会停止slave sql thread,完成后自动恢复;

 

 MDL
5.5有了MDL(Meta data lock),所以–single-transaction时,事务内操作过的表都会持有MDL,因此不会被DDL破坏。
例如,mysqldump已经备份了a,b,c表,因为它们在事务内,事务还没提交,它们的MDL不会释放,因此另外的线程如果做a,b,c中任意一张表的DDL操作,都会出现Waiting for table metadata lock,而还没备份到的表不会持有MDL,因此还可以做DDL。
http://www.penglixun.com/tech/database/the_process_of_mysqldump.html


--本篇文章转自:http://blog.itpub.net/15480802/viewspace-1424248/


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`--master-data` 和 `--single-transaction` 是两个 mysqldump 命令的选项,可以结合使用以实现更一致的备份。 `--master-data` 参数用于在备份文件中添加二进制日志文件和位置信息,以便在恢复时确定备份点。它可以通过设置不同的值来控制额外添加的信息量。 `--single-transaction` 参数用于在备份过程中开启一个事务,并使用一致性读取来获取数据。这意味着备份过程中的数据读取不会被其他事务的修改所影响,确保备份的一致性。 结合使用 `--master-data` 和 `--single-transaction` 可以达到以下效果: 1. 保证备份的一致性:使用 `--single-transaction` 可以确保备份过程中的数据读取一致,避免了其他事务对数据的修改。这对于需要备份大型数据库或者备份过程需要较长时间的情况非常有用。 2. 提供备份点信息:使用 `--master-data` 可以在备份文件中添加二进制日志文件和位置信息。结合 `--single-transaction`,可以在备份时获取一个一致性的备份点,并在恢复时可以方便地确定备份点进行恢复操作。 以下是使用 `--master-data` 和 `--single-transaction` 结合进行备份的示例命令: ``` mysqldump --master-data=2 --single-transaction -u <username> -p <database> > backup.sql ``` 在备份完成后,备份文件 backup.sql 中将包含二进制日志文件和位置信息的注释,并且备份点是在一个一致性的状态下获取的。 综上所述,结合使用 `--master-data` 和 `--single-transaction` 可以实现一致性备份,并提供备份点信息,以便在恢复时进行准确的还原。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值