mysqldump流程举例分析


本篇内容主要讲解“mysqldump流程举例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysqldump流程举例分析”吧!
首先我们把参数和内部变量对应起来,并且看一下它们的注释:
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。
This causes the binary log position and filename to be appended to the output. If equal to 1, will print it as a CHANGE MASTER command; if equal to 2, that command will be prefixed with a comment symbol. This option will turn –lock-all-tables on, unless –single-transaction is specified too (in which case a global read lock is only taken a short time at the beginning of the dump; don’t forget to read about –single-transaction below). In all cases, any action on logs will happen at the exact moment of the dump. Option automatically turns –lock-tables off.
这个选项可以把binlog的位置和文件名添加到输出中,如果等于1,将会打印成一个CHANGE MASTER命令;如果等于2,会加上注释前缀。并且这个选项会自动打开–lock-all-tables,除非同时设置了–single-transaction(这种情况下,全局读锁只会在开始dump的时候加上一小段时间,不要忘了阅读–single-transaction的部分)。在任何情况下,所有日志中的操作都会发生在导出的准确时刻。这个选项会自动关闭–lock-tables。
Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns –single-transaction and –lock-tables off.
锁定所有库中所有的表。这是通过在整个dump的过程中持有全局读锁来实现的。会自动关闭–single-transaction 和 –lock-tables。
Lock all tables for read. (Defaults to on; use –skip-lock-tables to disable.)
对所有表加读锁。(默认是打开的;用–skip-lock-tables来关闭)
Flush logs file in server before starting dump. Note that if you dump many databases at once (using the option –databases= or –all-data开发云主机域名bases), the logs will be flushed for each database dumped. The exception is when using –lock-all-tables or –master-data: in this case the logs will be flushed only once, corresponding to the moment all tables are locked. So if you want your dump and the log flush to happen at the same exact moment you should use –lock-all-tables or –master-data with –flush-logs。
在开始导出前刷新服务器的日志文件。注意,如果你一次性导出很多数据库(使用 –databases= 或 –all-databases 选项),导出每个库时都会触发日志刷新。例外是当使用了 –lock-all-tables 或 –master-data 时:日志只会被刷新一次,那个时候所有表都会被锁住。所以如果你希望你的导出和日志刷新发生在同一个确定的时刻,你需要使用–lock-all-tables,或者 –master-data 配合 –flush-logs。
Delete logs on master after backup. This automatically enables –master-data.
备份完成后删除主库上的日志。这个选项会自动打开 –master-data.
Adds ‘STOP SLAVE’ prior to ‘CHANGE MASTER’ and ‘START SLAVE’ to bottom of dump.
在’CHANGE MASTER’前加上’STOP SLAVE’,在导出文件的末尾加上’START SLAVE’.
我们分别看一下5.1和5.5的代码,都基于最新的trunk(5.1-rev.3909; 5.5-rev.4148)。
我们首先看下5.1版本的。
如果设置了master-data或lock-all-tables,则做FLUSH TABLES的操作。
来看下do_flush_tables_read_lock()里面是怎么做的,
先FLUSH TABLES,成功后用FLUSH TABLES WITH READ LOCK加全局读锁。
再往下会判断single-transaction,
如果定义了–single-transaction则打开一个事务来读取数据。
我们看下start_transaction()的实现,
会先设置隔离级别为RR,然后START TRANSACTION加上一致性快照的Hint。
接下来是获取Master的状态,
如果设置了–master-data 则把当前的Master status打印出来。
接下来再判断如果启用了–single-transaction,则可以释放表锁的,因为事务已经启动了。
do_unlock_tables()里面就发一条UNLOCK TABLES语句释放全局表锁。
然后开始调用dump_*函数根据需要导出整个实例或者一个库或者一个表。
所以我们可以知道,如果用–master-data和–single-transaction来导出数据,因为–lock-tables被自动关闭,所以导出过程中只会对当前正在做导出操作的表有IS锁,已经完成或没有开始的表,则不会加锁。
如果用的是默认–lock-tables打开的选项,则会先把所有库的锁加上,再进行导出操作,最后一次性释放所有锁。
接下来我们再比较一下,5.5的mysqldump有哪些变化。
这里有所不同,增加了flush_logs的判断,如果只是单纯的–single-transaction,不会调用do_flush_tables_read_lock(),必须同时制定–flush-logs。
5.5里面会尝试FLUSH LOGS。
5.5新增的变量,删除master上的log,这里先获取binlog的文件名。
这一段没有变化
这里有新加的opt_slave_apply和opt_slave_data部分,添加STOP SLAVE语句和显示SHOW SALVE STATUS的结果。
之后也是调用dump_*来导出数据。
但是因为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。到此,相信大家对“mysqldump流程举例分析”有了更深的了解,不妨来实际操作一番吧!这里是开发云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

相关推荐: mysqldump如何备份脚本

这篇文章将为大家详细讲解有关mysqldump如何备份脚本,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。mysql_full_backup.sh#!/bin/sh#Created by Clark 201710131716s…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/28 07:29
下一篇 06/28 07:29

相关推荐