成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

mysqldump流程舉例分析

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

創(chuàng)新互聯(lián)公司服務項目包括興隆臺網(wǎng)站建設(shè)、興隆臺網(wǎng)站制作、興隆臺網(wǎng)頁制作以及興隆臺網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,興隆臺網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到興隆臺省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

重要參數(shù)

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

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

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

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

對所有表加讀鎖。(默認是打開的;用–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。

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

備份完成后刪除主庫上的日志。這個選項會自動打開 –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;

如果設(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則打開一個事務來讀取數(shù)據(jù)。
我們看下start_transaction()的實現(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)啟動事務

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

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

如果設(shè)置了–master-data 則把當前的Master status打印出來。
接下來再判斷如果啟用了–single-transaction,則可以釋放表鎖的,因為事務已經(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ù)需要導出整個實例或者一個庫或者一個表。

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(默認),則導出之前需要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來導出數(shù)據(jù),因為–lock-tables被自動關(guān)閉,所以導出過程中只會對當前正在做導出操作的表有IS鎖,已經(jīng)完成或沒有開始的表,則不會加鎖。
如果用的是默認–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,不會調(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_*來導出數(shù)據(jù)。
但是因為5.5有了MDL(Meta data lock),所以–single-transaction時,事務內(nèi)操作過的表都會持有MDL,因此不會被DDL破壞。
例如,mysqldump已經(jīng)備份了a,b,c表,因為它們在事務內(nèi),事務還沒提交,它們的MDL不會釋放,因此另外的線程如果做a,b,c中任意一張表的DDL操作,都會出現(xiàn)Waiting for table metadata lock,而還沒備份到的表不會持有MDL,因此還可以做DDL。

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

文章標題:mysqldump流程舉例分析
標題路徑:http://jinyejixie.com/article8/pdcoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作定制網(wǎng)站、營銷型網(wǎng)站建設(shè)、小程序開發(fā)、微信公眾號、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
鄂托克旗| 乐清市| 如皋市| 武乡县| 龙口市| 上蔡县| 大英县| 昭通市| 绵竹市| 方城县| 鸡西市| 乌鲁木齐县| 普兰店市| 平遥县| 旺苍县| 克东县| 岫岩| 孝感市| 永泰县| 道孚县| 故城县| 镶黄旗| 邵东县| 稻城县| 邮箱| 永年县| 宁安市| 崇阳县| 遵义市| 墨竹工卡县| 伊金霍洛旗| 威海市| 明水县| 博客| 门头沟区| 清水河县| 鄂托克旗| 阿巴嘎旗| 南和县| 新津县| 灵宝市|