久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

mysqldump流程舉例分析

124次閱讀
沒有評論

共計(jì) 8075 個字符,預(yù)計(jì)需要花費(fèi) 21 分鐘才能閱讀完成。

本篇內(nèi)容主要講解“mysqldump 流程舉例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓丸趣 TV 小編來帶大家學(xué)習(xí)“mysqldump 流程舉例分析”吧!

重要參數(shù)

首先我們把參數(shù)和內(nèi)部變量對應(yīng)起來,并且看一下它們的注釋:

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

通過將導(dǎo)出操作封裝在一個事務(wù)內(nèi)來使得導(dǎo)出的數(shù)據(jù)是一個一致性快照。只有當(dāng)表使用支持 MVCC 的存儲引擎(目前只有 InnoDB)時才可以工作;其他引擎不能保證導(dǎo)出是一致的。當(dāng)導(dǎo)出開啟了–single-transaction 選項(xiàng)時,要確保導(dǎo)出文件有效(正確的表數(shù)據(jù)和二進(jìn)制日志位置),就要保證沒有其他連接會執(zhí)行如下語句:ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE,這會導(dǎo)致一致性快照失效。這個選項(xiàng)開啟后會自動關(guān)閉 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.

這個選項(xiàng)可以把 binlog 的位置和文件名添加到輸出中,如果等于 1,將會打印成一個 CHANGE MASTER 命令;如果等于 2,會加上注釋前綴。并且這個選項(xiàng)會自動打開–lock-all-tables,除非同時設(shè)置了–single-transaction(這種情況下,全局讀鎖只會在開始 dump 的時候加上一小段時間,不要忘了閱讀–single-transaction 的部分)。在任何情況下,所有日志中的操作都會發(fā)生在導(dǎo)出的準(zhǔn)確時刻。這個選項(xiàng)會自動關(guān)閉–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 的過程中持有全局讀鎖來實(shí)現(xiàn)的。會自動關(guān)閉–single-transaction 和 –lock-tables。

–lock-tables: lock_tables

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

對所有表加讀鎖。(默認(rèn)是打開的;用–skip-lock-tables 來關(guān)閉)

–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。

在開始導(dǎo)出前刷新服務(wù)器的日志文件。注意,如果你一次性導(dǎo)出很多數(shù)據(jù)庫(使用 –databases= 或 –all-databases 選項(xiàng)),導(dǎo)出每個庫時都會觸發(fā)日志刷新。例外是當(dāng)使用了 –lock-all-tables 或 –master-data 時:日志只會被刷新一次,那個時候所有表都會被鎖住。所以如果你希望你的導(dǎo)出和日志刷新發(fā)生在同一個確定的時刻,你需要使用–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.

備份完成后刪除主庫上的日志。這個選項(xiàng)會自動打開 –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’,在導(dǎo)出文件的末尾加上’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;

如果設(shè)置了 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) ? //  如果設(shè)置了 --master-data 4668  FLUSH /*!40101 LOCAL */ TABLES  : //  那么用 FLUSH LOCAL TABLES 4669  FLUSH TABLES )) || //  如果沒設(shè)置那么使用 FLUSH TABLE 4670 mysql_query_with_error_report(mysql_con, 0, 4671  FLUSH TABLES WITH READ LOCK) ); //  如果上面的語句執(zhí)行成功了,再執(zhí)行這個

先 FLUSH TABLES,成功后用 FLUSH TABLES WITH READ LOCK 加全局讀鎖。
再往下會判斷 single-transaction,

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

如果定義了–single-transaction 則打開一個事務(wù)來讀取數(shù)據(jù)。
我們看下 start_transaction()的實(shí)現(xiàn),

start_transaction() 4741 return (mysql_query_with_error_report(mysql_con, 0, 4742  SET SESSION TRANSACTION ISOLATION   4743  LEVEL REPEATABLE READ) || //  先設(shè)置會話的隔離級別為 RR 4744 mysql_query_with_error_report(mysql_con, 0, 4745  START TRANSACTION   4746  /*!40100 WITH CONSISTENT SNAPSHOT */)); //  再用一致性快照模式 (RR) 啟動事務(wù)

會先設(shè)置隔離級別為 RR,然后 START TRANSACTION 加上一致性快照的 Hint。
接下來是獲取 Master 的狀態(tài),

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

如果設(shè)置了–master-data 則把當(dāng)前的 Master status 打印出來。
接下來再判斷如果啟用了–single-transaction,則可以釋放表鎖的,因?yàn)槭聞?wù)已經(jīng)啟動了。

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

do_unlock_tables()里面就發(fā)一條 UNLOCK TABLES 語句釋放全局表鎖。

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

然后開始調(diào)用 dump_* 函數(shù)根據(jù)需要導(dǎo)出整個實(shí)例或者一個庫或者一個表。

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 } /*  如果設(shè)置了 --lock-tables(默認(rèn)),則導(dǎo)出之前需要 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); //  導(dǎo)出一張表  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)) //  導(dǎo)出  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 來導(dǎo)出表,然后再看是不是配置了 --triggers 來決定是不是導(dǎo)出 Trigger,dump_triggers_for_table。*/ ... 4366 if (lock_tables) 4367 VOID(mysql_query_with_error_report(mysql, 0,  UNLOCK TABLES)); /*  導(dǎo)出完成之后,釋放表鎖  */

所以我們可以知道,如果用–master-data 和–single-transaction 來導(dǎo)出數(shù)據(jù),因?yàn)楱Clock-tables 被自動關(guān)閉,所以導(dǎo)出過程中只會對當(dāng)前正在做導(dǎo)出操作的表有 IS 鎖,已經(jīng)完成或沒有開始的表,則不會加鎖。
如果用的是默認(rèn)–lock-tables 打開的選項(xiàng),則會先把所有庫的鎖加上,再進(jìn)行導(dǎo)出操作,最后一次性釋放所有鎖。

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,不會調(diào)用 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 的結(jié)果。
之后也是調(diào)用 dump_* 來導(dǎo)出數(shù)據(jù)。
但是因?yàn)?5.5 有了 MDL(Meta data lock),所以–single-transaction 時,事務(wù)內(nèi)操作過的表都會持有 MDL,因此不會被 DDL 破壞。
例如,mysqldump 已經(jīng)備份了 a,b,c 表,因?yàn)樗鼈冊谑聞?wù)內(nèi),事務(wù)還沒提交,它們的 MDL 不會釋放,因此另外的線程如果做 a,b,c 中任意一張表的 DDL 操作,都會出現(xiàn) Waiting for table metadata lock,而還沒備份到的表不會持有 MDL,因此還可以做 DDL。

到此,相信大家對“mysqldump 流程舉例分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是丸趣 TV 網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-19發(fā)表,共計(jì)8075字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 张家口市| 沂源县| 罗平县| 报价| 营山县| 锦屏县| 元朗区| 巴林右旗| 奉节县| 汉源县| 北票市| 达孜县| 卢龙县| 米脂县| 上虞市| 陆良县| 托克托县| 湘潭县| 关岭| 铜山县| 五河县| 广水市| 荥经县| 紫阳县| 长寿区| 云浮市| 凌云县| 蓬安县| 武陟县| 佛山市| 中江县| 昌乐县| 忻州市| 棋牌| 宜兰县| 沅江市| 缙云县| 修文县| 衡南县| 银川市| 应城市|