A Database Backup Program
mysqldump客戶端是一款實用的mysql備份程序,可以對數據庫的定義及數據表內容,進行備份生成相應的SQL語句。它可以對一個或多個數據庫進行備份,或轉數據移到另一個SQL Server。mysqldump命令可以生成輸出CSV,其他分隔符的文本,或XML格式。
推薦使用mysql5.7.9以后的mysqldump工具,之前的工具,對表的列定義有一些BUG,可以使用INFORMATION_SCHEMA.COLUMNS 表來確定需要生成列的表。
mysqldump需要SELECT權限來導出表,SHOW VIEW來導出視圖,TRIGGER來導出觸發器,如果沒有使用--single-transaction 選項,需要LOCK TABLES鎖表,其他的導出功能還需要其他的權限。 導入一個dump導出的文件,需要具有執行它所包含的語句的權限,例如語句中包含CREATE等。 如果在windows中導出,默認的文件格式是UTF-16,它是不允許的連接字符集。這種格式載入的時候將產生錯誤。 mysqldump [options] > dump.sql 加上--result-file導出ASCII格式 mysqldump [options] --result-file=dump.sql
性能和可擴展方面的考慮 Performance and Scalability Considerations
mysqldump的優點是它提供一個較為方便和靈活的方式,可以在導出前進行查看和編輯。你可以將數據庫進行拷備,用來做為開發環境的,或是一些其他的用途。
如果進行備份大量的數據,它不是一個好的方案。備份步驟需要一些時間,數據恢復會很慢,因為重新導入SQL語句涉及磁盤I/O插入,創建索引,等等。
對於大規模的備份和還原,物理備份是比較合適的,以他們的原始格式復制數據文件,可以快速恢復
mysqldump可以檢索和備份數據以一行一行的方式。或者它可以檢索在內存中的表,在導出之前。但是如果表是很大的話,使用內存中的表可能會有一些問題。默認是一行一行的方式讀取數據的,即啟用了--quick選項(--opt),如果要使用內存表,使用--skip-quick選項。 如果使用較新的mysqldump導出了數據,但是恢復到一個較老的mysql版本中,使用 --skip-opt選項代替--opt,或使用--extended-insert選項
調用語法 Invocation Syntax
一般有以下三種方式使用mysqldump,調用一個庫的一張或多張表,一個或多個庫,或者全部的數據庫。
```
shell> mysqldump [options] db_name [tbl_name ...] shell> mysqldump [options] --databases db_name ... shell> mysqldump [options] --all-databases ```
選項配置
連接服務器相關配置 Connection Options
--bind-address=ip_address 如果一台計算機上有多個網絡接口,使用bind-address=ip_address選項,還指定連接到服務器的哪個接口 --compress, -C 如果支持壓縮,客戶端與服務端之間的信息傳輸,將被壓縮。 --default-auth=plugin A hint about the client-side authentication plugin to use. See Section 7.3.8, “Pluggable Authentication”. --enable-cleartext-plugin Enable the mysql_clear_password cleartext authentication plugin. (See Section 7.5.1.8, “The Cleartext Client-Side Authentication Plugin”.) This option was added in MySQL 5.7.10. --host=host_name, -h host_name 指定從哪台mysql服務器dump數據,默認是localhost --login-path=name Read options from the named login path in the .mylogin.cnf login path file. A “login path” is an option group containing options that specify which MySQL server to connect to and which account to authenticate as. To create or modify a login path file, use the mysql_config_editor utility. See Section 5.6.6, “mysql_config_editor — MySQL Configuration Utility”. --password[=password], -p[password] 連接mysql的密碼,可以使用縮寫-p選項,如果使用-p,則密碼與參數之間不能有空格,可以在配置文件中[mysqldump]指定password --pipe, -W 在windows中使用--pipe On Windows, connect to the server using a named pipe. This option applies only if the server supports named-pipe connections. --port=port_num, -P port_num mysql 的 TCP/IP 端口,默認是3306 --protocol={TCP|SOCKET|PIPE|MEMORY} 連接協議,有TCP|SOCKET|PIPE|MEMORY幾種可供使用 --secure-auth 不要以舊的密碼格式發送到服務器,這將阻止連接,除非使用新的密碼格式。5.7.4新增。5.7.5被丟棄。 --socket=path, -S path 在本地localhost連接mysql是,將使用socket連接,windows使用的是pipe --ssl* 是否使用ssl連接到mysql服務 --tls-version=protocol_list The protocols permitted by the client for encrypted connections. The value is a comma-separated list containing one or more protocol names. The protocols that can be named for this option depend on the SSL library used to compile MySQL. For details, see Section 7.4.3, “Secure Connection Protocols and Ciphers”. This option was added in MySQL 5.7.10. --user=user_name, -u user_name 連接服務的mysql帳戶名 You can also set the following variables by using --var_name=value syntax: max_allowed_packet The maximum size of the buffer for client/server communication. The default is 24MB, the maximum is 1GB. net_buffer_length client/server連接通信時的初始化buffer大小,當使用--extended-insert or --opt選項創建多行插入語句時,mysqldump會創建行達到net_buffer_length字節長度。 The initial size of the buffer for client/server communication. When creating multiple-row INSERT statements (as with the --extended-insert or --opt option), mysqldump creates rows up to net_buffer_length bytes long. If you increase this variable, ensure that the MySQL server net_buffer_length system variable has a value at least this large.
配置文件相關的選項 Option-File Options
--defaults-extra-file=file_name 額外的配置文件,將會在讀取全局配置文件之后,用戶配置文件之前,讀取該配置文件內容。如果指定的文件不存在或不可訪問,將發生一個錯誤。 --defaults-file=file_name mysql配置文件,如果指定的文件不存在或不可訪問,將發生一個錯誤。 --defaults-group-suffix=str 通常情況下mysqldump會讀取[client],[mysqdump]組的配置,使用該選項,可以指定讀取其他組配置,如--defaults=group-suffix=other,將還會讀取[client_other],[mysqldump_other]組配置。 --no-defaults 不讀取任何配置文件 如果存在.mylogin.cnf文件,不管什么情況都會讀取該配置文件。它將阻止password被使用在命令行方式。而使用一個更加安全的方式。 The exception is that the .mylogin.cnf file, if it exists, is read in all cases. This permits passwords to be specified in a safer way than on the command line even when --no-defaults is used. (.mylogin.cnf is created by the mysql_config_editor utility. See Section 5.6.6, “mysql_config_editor — MySQL Configuration Utility”.) --print-defaults 打印從配置文件讀取的所有配置信息。
DDL選項
--add-drop-database 在每個CREATE DATABASE之前,增加DROP DATABASE語句,通常和 --all-databases or --databases 一起使用,因為如果不使用這其中之一的選項,將不會創建CREATE DATABASE語句 --add-drop-table 添加DROP TABLE 語句在每個 CREATE TABLE之前 --add-drop-trigger 添加DROP TRIGGER 觸發器語句,在每個 CREATE TRIGGER 之前 --all-tablespaces, -Y 該選項是和NDB集群相關的,其他類型無效。每個表需要創建tablespaces --no-create-db, -n 不創建CREATE DATABASE語句,和 --all-databases or --databases 一起使用才生效。 --no-create-info, -t 不創建CREATE TABLE語句 This option does not exclude statements creating log file groups or tablespaces from mysqldump output; however, you can use the --no-tablespaces option for this purpose. --no-tablespaces, -y 不輸出所有 CREATE LOGFILE GROUP 和 CREATE TABLESPACE語句 --replace 使用REPLACE而不是INSERT
調試選項配置 Debug Options
--allow-keywords 允許創建帶mysql系統關鍵字的列字段,但是會在字段前加上表名前綴。如table_cloumn --comments, -i 在導出的文件中添加如version,host等信息,默認啟用,可以使用 --skip-comments不輸出這些信息 --debug[=debug_options], -# [debug_options] Write a debugging log. A typical debug_options string is d:t:o,file_name. The default value is d:t:o,/tmp/mysqldump.trace. --debug-check Print some debugging information when the program exits. --debug-info Print debugging information and memory and CPU usage statistics when the program exits. --dump-date 如果--comments開戶,則使用該選項,將會在結尾,輸出導出的時間,格式如下 -- Dump completed on DATE --force, -f 忽略所有錯誤,繼續執行導出,如果同時指定了--ignore-error ,那么 --force優先 --log-error=file_name 輸出警告和錯誤信息到日志文件 --skip-comments 不顯示相關版本信息等 --verbose, -v print顯示程序的詳細信息
幫助選項 Help Options
The following options display information about the mysqldump command itself.
--help, -?
Display a help message and exit. --version, -V Display version information and exit.
國際化選項 Internationalization Options
--character-sets-dir=dir_name
The directory where character sets are installed. See Section 11.5, “Character Set Configuration”. --default-character-set=charset_name 設置默認的字符集,默認是utf8 --no-set-names, -N 不寫入SET NAMES default_character_set到文件。和--skip-set-charset一樣。 --set-charset 寫入 SET NAMES default_character_set 到導出文件,默認是開啟的。使用--skip-set-charset關閉
主從復制相關 Replication Options
--master-data[=value] 使用--master-data這個選項將主復制服務器生成轉儲文件,可用於建立另一個maste的slave服務器,它會使導出語句包含CHANGE MASTER TO語句,它說明了二進制日志的坐標(file name和position),這些坐標信息將告訴slave服務器,在導入數據之后,從哪里開始復制maste服務器的信息, 如果master-data被設置為2,CHANGE MASTER TO語句將被導出為注釋語句,將不會生效,如果設置為1,在導入文件后將立即生效。默認值是1 該選項需要數據庫的RELOAD權限,並且 binary log被啟用。 master-data選項,會自動關閉--lock-tables。並且會使用--lock-all-tables, 除非設置了--single-transaction選項,在這種情況下,在導出開始前,會在短時間里,獲得一個全局的只讀lock鎖。 在所有情況下,日志中的任何操作都會發生在轉儲的確切時刻。 也可以使用--dump-slave選項,從一個slave導出數據,它會覆蓋--master-data配置,如果2個選項都都使用,會使--master-data被忽略。 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. 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. This option requires the RELOAD privilege and the binary log must be enabled. 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. It is also possible to set up a slave by dumping an existing slave of the master, using the --dump-slave option, which overrides --master-data and causes it to be ignored if both options are used. --delete-master-logs 在一個主復制服務器,在執行dump操作后,通過向服務器發送 PURGE BINARY LOGS語句,刪除binary logs,該選項自動啟用--master-data 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. --set-gtid-purged=value 這個選項可以控制全局事務ID(GTID)信息寫入到轉儲文件,通過指示是否添加一組@@global.gtid_purged語句輸出。這個選項也可能導致輸出一組SQL語句到文件,有可能禁用二進制日志,當轉儲文件被重新加載。 該選項有3個值:OFF,ON,AUTO,默認是AUTO(即ON) --set-gtid-purged=OFF:不會向導出文件寫入SET @@SESSION.SQL_LOG_BIN=0;(不禁用二進制日志) --set-gtid-purged=ON:向導出文件寫入SET @@SESSION.SQL_LOG_BIN=0;(禁用二進制日志) This option enables control over global transaction ID (GTID) information written to the dump file, by indicating whether to add a SET @@global.gtid_purged statement to the output. This option may also cause a statement to be written to the output that disables binary logging while the dump file is being reloaded. The following table shows the permitted option values. The default value is AUTO. Value Meaning OFF Add no SET statement to the output. ON Add a SET statement to the output. An error occurs if GTIDs are not enabled on the server. AUTO Add a SET statement to the output if GTIDs are enabled on the server. The --set-gtid-purged option has the following effect on binary logging when the dump file is reloaded: --set-gtid-purged=OFF: SET @@SESSION.SQL_LOG_BIN=0; is not added to the output. --set-gtid-purged=ON: SET @@SESSION.SQL_LOG_BIN=0; is added to the output. --set-gtid-purged=AUTO: SET @@SESSION.SQL_LOG_BIN=0; is added to the output if GTIDs are enabled on the server you are backing up (that is, if AUTO evaluates to ON). --dump-slave[=value] 該選項和 --master-data 選項類似,但是它可以用來從一個slave導出文件,並且將導出的文件導入另一個mysql服務器,用來啟動另一個slave服務器,這樣它們所屬的maste是一樣的。這樣導出的文件,將會包含 CHANGE MASTER TO 語句,它包含了slave服務器所屬的maste的坐標(file name 和position)。 CHANGE MASTER TO 會讀取Relay_Master_Log_File 和Exec_Master_Log_Pos 的值(SHOW SLAVE STATUS 可以顯示),將它們的值分別賦值給 MASTER_LOG_FILE 和 MASTER_LOG_POS respectively.這些值是告訴slave該從master的哪個位置開始復制信息。 注意!如果已執行的中繼日志(relay log)中的事務序列不一致,會導致錯誤的位置被使用 --dump-slave 使用的是master主服務器的坐標被使用,而不是導出它的那台服務器。它和--master-data是一樣的。另外使用--dump-slave將導致master-data覆蓋 警告!如果使用了 gtid_mode=ON and MASTER_AUTOPOSITION=1這2個選項,將不能使用該選項。 該選項的值和--master-data是一樣的。 如果被設置為2,CHANGE MASTER TO語句將被導出為注釋語句,將不會生效,如果設置為1,在導入文件后將立即生效。默認值是1,鎖表機制也和master-data是一樣的。 該選項將停止slave thread線程,在導出前停止,導完后重新啟動。 結合 --dump-slave選項,dump-slave——apply-slave-statements和include-master-host-port選項也可以使用。 This option is similar to --master-data except that it is used to dump a replication slave server to produce a dump file that can be used to set up another server as a slave that has the same master as the dumped server. 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 slave's master. The CHANGE MASTER TO statement reads the values of Relay_Master_Log_File and Exec_Master_Log_Pos from the SHOW SLAVE STATUS output and uses them for MASTER_LOG_FILE and MASTER_LOG_POS respectively. These are the master server coordinates from which the slave should start replicating. Note Inconsistencies in the sequence of transactions from the relay log which have been executed can cause the wrong position to be used. See Section 18.4.1.34, “Replication and Transaction Inconsistencies” for more information. --dump-slave causes the coordinates from the master to be used rather than those of the dumped server, as is done by the --master-data option. In addition, specfiying this option causes the --master-data option to be overridden, if used, and effectively ignored. Warning This option should not be used if the server where the dump is going to be applied uses gtid_mode=ON and MASTER_AUTOPOSITION=1. The option value is handled the same way as for --master-data (setting no value or 1 causes a CHANGE MASTER TO statement to be written to the dump, setting 2 causes the statement to be written but encased in SQL comments) and has the same effect as --master-data in terms of enabling or disabling other options and in how locking is handled. This