mysqldump流程举例分析

发布时间:2021-11-18 17:16:43 作者:iii
来源:亿速云 阅读:137

本篇内容主要讲解“mysqldump流程举例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysqldump流程举例分析”吧!

重要参数

首先我们把参数和内部变量对应起来,并且看一下它们的注释:

–single-transaction: opt_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.

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

–master-data: opt_master_data

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。

–lock-all-tables: opt_lock_all_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-tables: lock_tables

Lock all tables for read. (Defaults to on; use –skip-lock-tables to disable.)

对所有表加读锁。(默认是打开的;用–skip-lock-tables来关闭)

–flush-logs: flush_logs

Flush logs file in server before starting dump. Note that if you dump many databases at once (using the option –databases= or –all-databases), 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-master-logs: opt_delete_master_logs

Delete logs on master after backup. This automatically enables –master-data.

备份完成后删除主库上的日志。这个选项会自动打开 –master-data.

–apply-slave-statements: opt_slave_apply(5.5)

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版本主要流程

我们首先看下5.1版本的。

5320 if ((opt_lock_all_tables || opt_master_data) && 5321 do_flush_tables_read_lock(mysql)) 5322 goto err;

如果设置了master-data或lock-all-tables,则做FLUSH TABLES的操作。
来看下do_flush_tables_read_lock()里面是怎么做的,

do_flush_tables_read_lock() 4665 return 4666 ( mysql_query_with_error_report(mysql_con, 0, 4667 ((opt_master_data != 0) ? // 如果设置了--master-data 4668 "FLUSH /*!40101 LOCAL */ TABLES" : // 那么用FLUSH LOCAL TABLES  4669 "FLUSH TABLES")) || // 如果没设置那么使用FLUSH TABLE 4670 mysql_query_with_error_report(mysql_con, 0, 4671 "FLUSH TABLES WITH READ LOCK") ); // 如果上面的语句执行成功了,再执行这个

先FLUSH TABLES,成功后用FLUSH TABLES WITH READ LOCK加全局读锁。
再往下会判断single-transaction,

5323 if (opt_single_transaction && start_transaction(mysql)) 5324 goto err;

如果定义了–single-transaction则打开一个事务来读取数据。
我们看下start_transaction()的实现,

start_transaction() 4741 return (mysql_query_with_error_report(mysql_con, 0, 4742 "SET SESSION TRANSACTION ISOLATION " 4743 "LEVEL REPEATABLE READ") || // 先设置会话的隔离级别为RR 4744 mysql_query_with_error_report(mysql_con, 0, 4745 "START TRANSACTION " 4746 "/*!40100 WITH CONSISTENT SNAPSHOT */")); // 再用一致性快照模式(RR)启动事务

会先设置隔离级别为RR,然后START TRANSACTION加上一致性快照的Hint。
接下来是获取Master的状态,

5338 if (opt_master_data && do_show_master_status(mysql)) 5339 goto err;

如果设置了–master-data 则把当前的Master status打印出来。
接下来再判断如果启用了–single-transaction,则可以释放表锁的,因为事务已经启动了。

5340 if (opt_single_transaction && do_unlock_tables(mysql)) /* unlock but no commit! */ 5341 goto err;

do_unlock_tables()里面就发一条UNLOCK TABLES语句释放全局表锁。

do_unlock_tables() 4677 return mysql_query_with_error_report(mysql_con, 0, "UNLOCK TABLES");

然后开始调用dump_*函数根据需要导出整个实例或者一个库或者一个表。

dump_all_databases()->dump_all_tables_in_db() 4307 if (lock_tables) 4308 { 4309 DYNAMIC_STRING query; 4310 init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024); 4311 for (numrows= 0 ; (table= getTableName(1)) ; ) 4312 { 4313 char *end= strmov(afterdot, table); 4314 if (include_table((uchar*) hash_key,end - hash_key)) 4315 { 4316 numrows++; 4317 dynstr_append_checked(&query, quote_name(table, table_buff, 1)); 4318 dynstr_append_checked(&query, " READ /*!32311 LOCAL */,"); 4319 } 4320 } 4321 if (numrows && mysql_real_query(mysql, query.str, query.length-1)) 4322 DB_error(mysql, "when using LOCK TABLES"); 4323 /* We shall continue here, if --force was given */ 4324 dynstr_free(&query); 4325 } /* 如果设置了--lock-tables(默认),则导出之前需要LOCK TABLES tables_name READ。*/ ... 4332 while ((table= getTableName(0))) 4333 { 4334 char *end= strmov(afterdot, table); 4335 if (include_table((uchar*) hash_key, end - hash_key)) 4336 { 4337 dump_table(table,database); // 导出一张表 4338 my_free(order_by, MYF(MY_ALLOW_ZERO_PTR)); 4339 order_by= 0; 4340 if (opt_dump_triggers && mysql_get_server_version(mysql) >= 50009) 4341 { 4342 if (dump_triggers_for_table(table, database)) // 导出 trigger 4343 { 4344 if (path) 4345 my_fclose(md_result_file, MYF(MY_WME)); 4346 maybe_exit(EX_MYSQLERR); 4347 } 4348 } 4349 } 4350 } /* 先dump_table来导出表,然后再看是不是配置了--triggers来决定是不是导出Trigger,dump_triggers_for_table。*/ ... 4366 if (lock_tables) 4367 VOID(mysql_query_with_error_report(mysql, 0, "UNLOCK TABLES")); /* 导出完成之后,释放表锁 */

所以我们可以知道,如果用–master-data和–single-transaction来导出数据,因为–lock-tables被自动关闭,所以导出过程中只会对当前正在做导出操作的表有IS锁,已经完成或没有开始的表,则不会加锁。
如果用的是默认–lock-tables打开的选项,则会先把所有库的锁加上,再进行导出操作,最后一次性释放所有锁。

5.5版本主要流程

接下来我们再比较一下,5.5的mysqldump有哪些变化。

5464 if ((opt_lock_all_tables || opt_master_data || 5465 (opt_single_transaction && flush_logs)) && 5466 do_flush_tables_read_lock(mysql)) 5467 goto err;

这里有所不同,增加了flush_logs的判断,如果只是单纯的–single-transaction,不会调用do_flush_tables_read_lock(),必须同时制定–flush-logs。

5469 /*
5470     Flush logs before starting transaction since
5471     this causes implicit commit starting mysql-5.5.
5472   */ 5473 if (opt_lock_all_tables || opt_master_data || 5474 (opt_single_transaction && flush_logs) || 5475 opt_delete_master_logs) 5476 { 5477 if (flush_logs || opt_delete_master_logs) 5478 { 5479 if (mysql_refresh(mysql, REFRESH_LOG)) 5480 goto err; 5481 verbose_msg("-- main : logs flushed successfully!\n"); 5482 } 5483 5484 /* Not anymore! That would not be sensible. */ 5485 flush_logs= 0; 5486 }

5.5里面会尝试FLUSH LOGS。

5488 if (opt_delete_master_logs) 5489 { 5490 if (get_bin_log_name(mysql, bin_log_name, sizeof(bin_log_name))) 5491 goto err; 5492 }

5.5新增的变量,删除master上的log,这里先获取binlog的文件名。

5494 if (opt_single_transaction && start_transaction(mysql)) 5495 goto err;

这一段没有变化

5497 /* Add 'STOP SLAVE to beginning of dump */ 5498 if (opt_slave_apply && add_stop_slave()) 5499 goto err; 5500 if (opt_master_data && do_show_master_status(mysql)) 5501 goto err; 5502 if (opt_slave_data && do_show_slave_status(mysql)) 5503 goto err; 5504 if (opt_single_transaction && do_unlock_tables(mysql)) /* unlock but no commit! */ 5505 goto err;

这里有新加的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流程举例分析”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. mysql举例分析
  2. 逻辑备份mysqldump和物理备份xtrabackup的流程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysqldump

上一篇:如何搭建ansible roles

下一篇:swarm如何搭建docker集群

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》