mysqldump流程举例分析

38次阅读
没有评论

共计 8075 个字符,预计需要花费 21 分钟才能阅读完成。

本篇内容主要讲解“mysqldump 流程举例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“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 流程举例分析”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计8075字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)