MySQL入門篇之mysqldump參數說明


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 option causes mysqldump to stop the slave SQL thread before the dump and restart it again after. In conjunction with --dump-slave, the --apply-slave-statements and --include-master-host-port options can also be used. --apply-slave-statements 在slave庫中使用--dump-slave導出文件的時候,可以在CHANGE MASTER TO的前面加上STOP SLAVE語句,並且在導出文件的最后加上START SLAVE語句。這樣可以在重新導入的時候stop slave,導入完成后start slave For a slave dump produced with the --dump-slave option, add a STOP SLAVE statement before the CHANGE MASTER TO statement and a START SLAVE statement at the end of the output. --include-master-host-port 在slave庫中使用--dump-slave導出文件的時候, 添加MASTER_HOST 和 MASTER_PORT選項配置,指明主庫master的host name和tcp/ip端口。 For the CHANGE MASTER TO statement in a slave dump produced with the --dump-slave option, add MASTER_HOST and MASTER_PORT options for the host name and TCP/IP port number of the slave's master. 

格式化選項 Format Options

--compact
產生較為緊湊的輸出,該選項相當於使用了 --skip-add-drop-table, --skip-add-locks, --skip-comments, --skip-disable-keys, 和 --skip-set-charset options選項的功能。 --compatible=name 導出兼容其他數據庫系統,或舊版本的mysql語句的文件,值可以是 ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, or no_field_options. 多個值可以使用','分開。該選項和SQL mode相關 This option does not guarantee compatibility with other servers. It only enables those SQL mode values that are currently available for making dump output more compatible. For example, --compatible=oracle does not map data types to Oracle types or use Oracle comment syntax. 至少需要4.1.0上版本才能使用該選項 --complete-insert, -c 導出完整的插入語句,包括列名。 --create-options Include all MySQL-specific table options in the CREATE TABLE statements. --tab=dir_name, -T dir_name 指定一個目錄(該目錄針對運行mysql進程的系統帳戶要有寫入權限,且執行導出的mysql帳戶要有FILE權限),用來存儲導出的mysql文件。將為每張表tbl_name.sql文件,包含表的創建語句。同時創建 一個tbl_name.txt文件,包含表數據的內容。 其中tbl_name.txt表的數據內容,字段之間,默認用tab分開,行默認使用\n分開。 可以通過指定--fields-terminated-by=..., --fields-enclosed-by=..., --fields-optionally-enclosed-by=..., --fields-escaped-by=... --lines-terminated-by=...設置分隔符。這些選項的含義和 LOAD DATA INFILE的使用是相關的。 字段的內容是按--default-character-set 選項指定的編碼進行存儲的。 --hex-blob 導出二進制數據使用十六進制的方式存儲(如將'abc' 變成 0x616263)受影響的數據類型有 BINARY, VARBINARY, the BLOB types, and BIT. --quote-names, -Q 如database, table, and column 使用"`"字符,默認是開啟的。--skip-quote-names跳過該功能。如果使用 ANSI_QUOTES SQL mode模式,將使用雙引號'"' --result-file=file_name, -r file_name 該選項輸出指定的文件,並覆蓋之前存在的內容,即使產生了錯誤。 該選項在windows中可用來避免產生\r\n格式的換行。 --tz-utc 導出文件中將包含 SET TIME_ZONE='+00:00' 類似的信息,可以避免不同服務器時區不同的問題 --xml, -X 導出XML格式的文件 NULL, 'NULL', 空字符 ``` NULL (unknown value) <field name="column_name" xsi:nil="true" /> '' (empty string) <field name="column_name"></field> 'NULL' (string value) <field name="column_name">NULL</field> ``` 

過濾選項 Filtering Options

以下選項控制哪種模式對象寫入到轉儲文件:按類別,如觸發或事件;按名稱如,選擇哪個數據庫和表轉儲;甚至從表數據過濾行使用WHERE子句。

--all-databases, -A 導出全部數據庫中的所有表 --databases, -B 導出部分數據庫,通常情況導出數據,第一個參數是數據庫名,之后的是表名,使用該選項,所有的參數都是數據庫名 --events, -E 導出事件調度到文件,該選項需要EVENT權限。導出的文件將包含CREATE EVENT語句,不包含event 創建和修改的時間戳timestamps,如果被重新導入,將是載入時的時間戳。如果需要導入時間戳,使用mysql.event表中的內容 --ignore-error=error[,error]... 忽略錯誤,也可以使用--force --ignore-table=db_name.tbl_name 忽略表名,不導出指定的表名,必需指定數據庫名和表名,如果要忽略多個表,使用多個該選項配置,如--ignore-table=db_name.tbl_name1 --ignore-table=db_name.tbl_name2。 該選項還可用來忽略VIEW視圖 --no-data, -d 不導出數據表的內容,如果只想導出表結構,可以使用該選項。 --routines, -R 導出包括存儲程序(過程和函數)的數據。使用這個選項需要mysql.proc表的SELECT權限。 導出的數據將包含 CREATE PROCEDURE and CREATE FUNCTION 語句,該選項也不會導出相應的時間戳。如果要包含時間戳,使用mysql.proc 表的內容 -tables 重寫 --databases or -B option ,該參數后的所有參數都是表名。 --where='where_condition', -w 'where_condition' 過濾行選項。 Examples: ``` --where="user='jimf'" -w"userid>1" -w"userid<1" ``` 

性能選項 Performance Options

通常性能受事務選項配置transactional options的影響
--disable-keys, -K 通常在insert語句的前后,寫入 /*!40000 ALTER TABLE tbl_name DISABLE KEYS */; 和 /*!40000 ALTER TABLE tbl_name ENABLE KEYS */; 語句。這樣在導入數據時將變得更快,因為索引是在所有行數據插入后才創建的。這個選項僅對沒有唯一索引的myisam表有效。 --extended-insert, -e 將以INSERT多行的方式導出到文件,這樣生成的文件將更小,且在導入數據的時候速度將更快 --insert-ignore 使用insert-ignore語句而不是insert --opt 該選項默認被啟用,相當於是開啟了 --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset選項。這個選項使導出更快,導入也更快。 使用--skip-opt 選項跳過該功能 --quick, -q 如果導出的表非常大,該選項非常有用,使用一行一行的檢索方式,而不是從緩存中讀取全部行 

事務選項 Transactional Options

--add-locks 在導出的文件中添加LOCK TABLES and UNLOCK TABLES語句,這樣在重新導入的時候更快。 --flush-logs, -F 在導出前刷新logs,該選項需要RELOAD權限。如果配合使用 --all-databases選項,所有庫的日志將被刷新。除非開啟了 --lock-all-tables, --master-data, or --single-transaction,這種情況只刷新一次logs,對應的那一刻,所有表是鎖着的。 如果想讓你導出的文件的那一刻和刷新日志在同一精確的時間,應該使用 --lock-all-tables, --master-data, or --single-transaction. --flush-privileges 為導出的文件添加FLUSH PRIVILEGES語句。如果從舊版mysql升級到新5.7.2或更高版本,不要使用該選項。 --lock-all-tables, -x 鎖住所有庫的所有表,這將在導出數據期間,獲取一個全局的只讀鎖。該選項會自動關閉--single-transaction and --lock-tables選項。 --lock-tables, -l 針對Myisam表,在導出前將鎖住所有表,獲得的是 READ LOCAL鎖 ,期間允許並發寫入。 針對事務型的表,如Innodb,使用--single-transaction 選項更為合適。因為它不需要鎖表。 因為--lock-tables是針對每個表進行鎖表的,並不保證每個導出的數據庫文件在邏輯上的一致性。不同的database可能有不同的狀態。 --no-autocommit 在每個導出的表的INSERT語句的周圍寫入 SET autocommit = 0 and COMMIT statements. --order-by-primary 按表的主鍵轉儲行數據,或者按唯一索引。這在將myisam表導入到Innodb表的時候非常有用,但是會消耗更多的時間。 --single-transaction 這個選項,在導出之前,設置事務隔離模式為可重復讀取( REPEATABLE READ)並且發送一個開始事務( START TRANSACTION )SQL語句。這對事務型的表如Innodb很有用。因為它保持一致的狀態,當使用START TRANSACTION被執行,並不會阻塞其他的應用。 記住,myisam或者MEMORY類型的表的狀態依舊會改變。 當開啟了--single-transaction ,為了確保導出文件有效(正確的表內容,和binary log coordinates),不能有如下的修改表的操作: ALTER TABLE, CREATE TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE 使用這些操作不能保證讀的一致性。會產生讀表內容出錯。 --single-transaction和--lock-tables選項是互斥的,因為lock tables會導致掛起的事務被提交。 The --single-transaction option and the --lock-tables option are mutually exclusive because LOCK TABLES causes any pending transactions to be committed implicitly. 導出大的表,可以使用--single-transaction 結合 --quick 選項。 

選項組 Option Groups

--opt --compact 如果想選擇性的禁用或啟用組選項中的設置,順序是很重要的,指令將從頭到尾的被執行。 如果使用 --disable-keys --lock-tables --skip-opt這3個參數,則無法禁用keys和鎖表,因為最后一條跳過了。 

例子

1.備份某個庫的所有內容 ``` shell> mysqldump db_name > backup-file.sql ``` 2.從備份中恢復 ``` shell> mysql db_name < backup-file.sql ``` 3.另外一種方式恢復數據 ``` shell> mysql -e "source /path-to-backup/backup-file.sql" db_name ``` 4.使用mysqldump導出數據到其他數據庫 ``` shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name ``` 5.備份多個數據庫的 ``` shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql ``` 6.備份所有數據庫 ``` shell> mysqldump --all-databases > all_databases.sql ``` 7.在線備份Innodb表 ``` shell> mysqldump --all-databases --master-data --single-transaction > all_databases.sql ``` 這種備份方式,需要為所有的表,獲取一個全局的只讀鎖(在dump操作前,使用FLUSH TABLES WITH READ LOCK獲取)。一旦全局只讀鎖獲取,binary log coordinates將被獲取,此時鎖被釋放。如果在使用FLUSH操作期間,有一個update操作占用了較長時間,mysql服務器將會無響應,直到update操作結束。此后鎖被釋放,可以進行對表的讀寫操作。 為了恢復到某一個時間點的備份,通常使用 binary log,或者要知道 在導出數據時,binary log的坐標。 ``` shell> mysqldump --all-databases --master-data=2 > all_databases.sql 或者 shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql ``` 如果是Innodb表備份,可以使用同時使用--master-data --single-transaction選項備份某個時間點的數據。 如果想使用--opt的全部選項,除了extended-insert,quick 。使用--skip,如 --opt --skip-extended-insert --skip-quick(--opt可省略,因為默認啟用) 如果想跳過--opt,只使用disable-keys,lock-tables功能。使用--skip-opt --disable-keys --lock-tables 

限制

mysqldump操作,默認不導出以下數據庫,INFORMATION_SCHEMA, performance_schema, or (as of MySQL 5.7.8) sys schema 如果想導出,則使用明確說明的方式 --databases INFORMATION_SCHEMA等。對INFORMATION_SCHEMA 和 performance_schema, 通常使用 --skip-lock-tables 選項。 mysqldump不會導出 MySQL Cluster ndbinfo信息 不推薦在5.6.9之前使用GTIDs功能導出 mysqldump包含了mysql庫中的general_log and slow_query_log 表,Log表內容不會被導出。 

參考:

http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html http://dev.mysql.com/doc/refman/5.7/en/using-mysqldump.html


免責聲明!

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



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