mysqldump詳解之--master-data


在前一篇文章中,有提到mysqldump的--single-transaction參數。
另外還有個很重要,也是運維中經常用到的參數:--master-data,網上很多關於MySQL不停機備份的實現都有它的身影。
翻譯下man mysqldump中--master-data相關的章節,以便加強理解

? --master-data[=value]

Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. 

It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. 

These are the master server coordinates from which the slave should start replicating after you load the dump file into the slave.

使用此選項dump a master replication server到dump文件,可用於設立另一台服務器作為master的slave。

它會使dump輸出包含CHANGE MASTER TO語句,標記dump源的二進制日志坐標(文件名和位置)。

在把load dump文件加載到slave之后,slave應當從該master坐標開始復制。

 

If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. 

If the option value is 1, the statement is not written as a comment and takes effect when the dump file is reloaded. 

If no option value is specified, the default value is 1.

如果選項賦值為2,那么CHANGE MASTER TO 語句會被寫成一個SQL comment(注釋),從而只提供信息; 

如果選項賦值為1,那么語句不會被寫成注釋並且在dump被載入時生效。

如果沒有指定,默認值為1。

 

This option requires the RELOAD privilege and the binary log must be enabled.

這個選項要求 RELOAD 權限,並且二進制文件必須打開。

 

The --master-data option automatically turns off --lock-tables. 

It also turns on --lock-all-tables, unless --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the beginning of the dump (see the description for --single-transaction). 

In all cases, any action on logs happens at the exact moment of the dump.

--master-data 選項會自動關閉 --lock-tables。

也會打開 --lock-all-tables,除非指定--single-transaction,如果那樣的話,在開始dump時會獲取一個全局read lock 一小段時間(詳見--single-transaction的描述)。

不論什么情況下,日志相關動作發生在dump的確切時刻。——沒懂

 

It is also possible to set up a slave by dumping an existing slave of the master. 

To do this, use the following procedure on the existing slave:

它也可以dump一個存在的slave來設立一個新的slave。

要做到這點,在已存在的slave使用如下的步驟:

 

1. Stop the slave?s SQL thread and get its current status:

停止slave的SQL 線程並獲取它的當前狀態

 

mysql> STOP SLAVE SQL_THREAD;
mysql> SHOW SLAVE STATUS;

2. From the output of the SHOW SLAVE STATUS statement, the binary log coordinates of the master server
from which the new slave should start replicating are the values of the Relay_Master_Log_File and
Exec_Master_Log_Pos fields. Denote those values as file_name and file_pos.

從SHOW SLAVE STATUS語句的輸出,新slave應當開始復制master二進制的坐標就是 Relay_Master_Log_File 和 Exec_Master_Log_Pos 的值。 

用 file_name 和 file_pos 表示這些值。

 

3. Dump the slave server:

shell> mysqldump --master-data=2 --all-databases > dumpfile

Using --master-data=2 works only if binary logging has been enabled on the slave. 

Otherwise, mysqldump fails with the error Binlogging on server not active. 

In this case you must handle any locking issues in another manner, using one or more of --add-locks, --lock-tables, --lock-all-tables, or --single-transaction, as required by your application and environment.

僅在已打開二進制日志的slave上使用--master-data=2。

否則mysqldump會報錯二進制日志未啟動。

這種情況下你必須以另一種方式來處理任何鎖定問題,根據你應用和環境來決定使用--add-locks, --lock-tables, --lock-all-tables,or --single-transaction中的一個或多個。

 

4. Restart the slave:

mysql> START SLAVE;

5. On the new slave, load the dump file:

shell> mysql < dumpfile

6. On the new slave, set the replication coordinates to those of the master server obtained earlier:

在新slave上,設置之前從master獲取的復制坐標

 

mysql> CHANGE MASTER TO
-> MASTER_LOG_FILE = ?file_name?, MASTER_LOG_POS = file_pos;

The CHANGE MASTER TO statement might also need other parameters, such as MASTER_HOST to point the slave to the correct master server host. Add any such parameters as necessary.
CHANGE MASTER TO語句也需要其他參數,比如 MASTER_HOST 來指定slave連接正確的master。按需增加其他參數
------------------------

? --delete-master-logs

On a master replication server, delete the binary logs by sending a PURGE BINARY LOGS statement to the server after performing the dump operation. This option automatically enables --master-data.

在master replication server 執行dump操作后,通過發送一個 PURGE BINARY LOGS 語句到服務器來刪除二進制日志。

該選項自動啟用--master-data。

------------------------

 

? --flush-logs, -F

Flush the MySQL server log files before starting the dump. This option requires the RELOAD privilege. 

If you use this option in combination with the --all-databases option, the logs are flushed for each database dumped. 

The exception is when using --lock-all-tables or --master-data: In this case, the logs are flushed only once, corresponding to the moment that all tables are locked. 

If you want your dump and the log flush to happen at exactly the same moment, you should use --flush-logs together with either --lock-all-tables or --master-data.

在開始dump之前flush mysql 日志文件。該選項需要 RELOAD 權限。

如果你將該選項與 --all-databases 結合使用,每個數據庫的日志都會被flush for dump。

例外是當使用 --lock-all-tables 或 --master-data,這種情況下,日志會被flush一次,對應於所有表鎖定的那一刻。

如果你希望dump 和 log flush 發生在完全相同的時刻,你應該把 --flush-logs 連同 --lock-all-tables 或 --master-data使用。

------------------------

This backup acquires a global read lock on all tables (using FLUSH TABLES WITH READ LOCK) at the beginning of the dump. 

As soon as this lock has been acquired, the binary log coordinates are read and the lock is released.

If long updating statements are running when the FLUSH statement is issued, the MySQL server may get stalled until those statements finish. 

After that, the dump becomes lock free and does not disturb reads and writes on the tables. 

If the update statements that the MySQL server receives are short (in terms of execution time), the initial lock period should not be noticeable, even with many updates.

此備份在dump開始的時候就獲得所有表的全局讀鎖定(使用FLUSH TABLES WITH READ LOCK)。
當鎖一旦被獲取,就讀取二進制日志坐標並釋放鎖。
如果執行FLUSH語句時有很長的update語句正在執行,MySQL會直到等那些語句完成才會get stalled(失速/拋錨,停止?)。
在此之后,dump 變得 lock free 並且不再妨礙讀寫表。

如果MySQL服務器接收update語句很短(在執行時間),初步鎖定期並不明顯,即使有很多的update。

 

For point-in-time recovery (also known as "roll-forward," when you need to restore an old backup and replay the changes that happened since that backup), it is often useful to rotate the binary log (see Section 5.2.4, "The Binary Log") or at least know the binary log coordinates to which the dump corresponds:

對於即時恢復(也稱為“roll-forward”,當你需要恢復舊的備份並重放從備份之后發生的變化),這是經常用於循環二進制日志(參見第5.2.4,“二進制日志”)或至少知道二進制日志坐標到dump對應關系:

 

shell> mysqldump --all-databases --master-data=2 > all_databases.sql

Or:

shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql

The --master-data and --single-transaction options can be used simultaneously, which provides a convenient way to make an online backup suitable for use prior to point-in-time recovery if tables are stored using the InnoDB storage engine.

--master-data 和 --single-transaction 選項可同時使用,如果表使用InnoDB存儲引擎,它提供了簡便的途徑來制作適用於定點恢復的在線備份。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM