【MySQL】mysqldump參數分析

mysqldump重要參數分析:

-F, --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.  
                      

—使用此參數會在導出前刷新日誌文件,如果導出多個庫,則在每個庫導出前都會刷新一次日誌;例外的是如果用了 --lock-all-tables or --master-data,則僅會刷新一次日誌,對應此時刻所有表都會被鎖上。

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

—此參數爲1時dump文件中有包含change master命令,若爲2則change master語句是註釋掉的;
此參數會自動觸發–lock-all-tables啓動,除非同時使用了–single-transaction;
即如果–master-data和–single-transaction同時使用的情況下,–lock-all-tables是不會被觸發啓動的,此時在dump開始的很短一段時間會有一個全局的讀鎖,因爲–single-transaction就是會觸發全局讀鎖。
在任何情況下,使用–master-data都會觸發–lock-tables關閉。

  --set-gtid-purged[=name] 
                      Add 'SET @@GLOBAL.GTID_PURGED' to the output. Possible
                      values for this option are ON, OFF and AUTO. If ON is
                      used and GTIDs are not enabled on the server, an error is
                      generated. If OFF is used, this option does nothing. If
                      AUTO is used and GTIDs are enabled on the server, 'SET
                      @@GLOBAL.GTID_PURGED' is added to the output. If GTIDs
                      are disabled, AUTO does nothing. If no value is supplied
                      then the default (AUTO) value will be considered.
  --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.

—使用此參數在一個單事務中創建一個一致性快照,目前只對InnoDB有效。當使用此參數進行備份時,確保沒有其它的DDL語句執行,因爲一致性讀並不能隔離DDL語句。
此參數自動觸發–lock-tables關閉。

  -x, --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.

—對所有架構中的所有表上鎖,則存在問題:是否鎖的時間會更長?生產環境適用嗎?

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

—此參數在備份過程中依次鎖住每個架構下的所有表,一般用於MyISAM引擎,當備份時只能對數據庫進行讀取操作,不過依然可以保證備份的一致性。
對於InnoDB引擎,不需要使用此參數,用–single-transaction即可,並且–lock-tables和–single-transaction是互斥的,不能同時使用,若同時使用了,則–single-transaction會觸發–lock-tables關閉。
如果同時備份InnoDB和MyISAM引擎,則只能使用–lock-tables了,–lock-tables只能保證每個架構下的表備份的一致性,不能保證所有架構下的表一致性。(問:那是否用–lock-all-tables就好了?)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章