mysqldump 簡介
mysqldump 是一種用於邏輯備份的客戶端工具,它會產生一套能夠重新構建數據庫或表的SQL語句。所謂邏輯備份:是利用SQL語言從數據庫中抽取數據並存於二進制文件的過程。邏輯備份文件只能用來對數據庫進行邏輯恢復,即數據導入,而不能按數據庫原來的存儲特征進行物理恢復。
用mysqldump進行備份的用戶賬號至少需要SELECT權限來dump表、SHOW VIEW權限dump視圖、TRIGGER權限dump觸發器、如果沒有使用--single-transaction選項還需要LOCK TABLES權限(后面會講到--single-transaction選項不會鎖表),另外某些選項可能還需要其他權限,可在官方手冊查詢。
mysqldump 備份格式大概可分為3種:
shell> mysqldump [options] db_name [tbl_name ...] #備份指定數據庫或表 shell> mysqldump [options] --databases db_name ... #備份指定數據庫 shell> mysqldump [options] --all-databases #備份所有數據庫
mysqldump 選項(來源於mysql 5.7手冊)
--add-drop-database
Add DROP DATABASE statement before each CREATE DATABASE statement (每個數據庫創建之前添加drop數據庫語句)
--add-drop-table
Add DROP TABLE statement before each CREATE TABLE statement (每個數據表創建之前添加drop table語句。默認為打開狀態,使用--skip-add-drop-table取消選項)
--add-drop-trigger
Add DROP TRIGGER statement before each CREATE TRIGGER statement
--add-locks
Surround each table dump with LOCK TABLES and UNLOCK TABLES statements (在每個表導出之前增加LOCK TABLES並且之后UNLOCK TABLES。默認為打開狀態,使用--skip-add-locks取消選項)
--all-databases (-A)
Dump all tables in all databases (備份所有數據庫的所有表)
--allow-keywords
Allow creation of column names that are keywords
--apply-slave-statements Include
STOP SLAVE prior to CHANGE MASTER statement and START SLAVE at end of output
--bind-address
Use specified network interface to connect to MySQL Server
--character-sets-dir
Directory where character sets are installed
--comments
Add comments to dump file
--compact
Produce more compact output
--compatible
Produce output that is more compatible with other database systems or with older MySQL servers
--complete-insert, -c
Use complete INSERT statements that include column names(使用完整的insert語句(包含列名稱)。這么做能提高插入效率,但是可能會受到 max_allowed_packet 參數的影響而導致插入失敗)
--compress
Compress all information sent between client and server
--create-options
Include all MySQL-specific table options in CREATE TABLE statements
--databases (-B)
Interpret all name arguments as database names (將選項后的所有名稱參數解釋為數據庫名稱,以空格隔開)
注意:如果dump時不加 --databases 參數而直接跟數據庫名,則備份的sql文件中不會打印 "CREATE DATABASE ..." 語句和 "USE `db_name`;" 語句。
--debug
Write debugging log
--debug-check
Print debugging information when program exits
--debug-info
Print debugging information, memory, and CPU statistics when program exits
--default-auth
Authentication plugin to use
--default-character-set
Specify default character set
--defaults-extra-file
Read named option file in addition to usual option files
--defaults-file
Read only named option file
--defaults-group-suffix
Option group suffix value
--delete-master-logs
On a master replication server, delete the binary logs after performing the dump operation
--disable-keys
For each table, surround INSERT statements with statements to disable and enable keys
--dump-date
Include dump date as "Dump completed on" comment if --comments is given
--dump-slave
Include CHANGE MASTER statement that lists binary log coordinates of slave's master (取值0,1,2,與master-data作用相同)
# mysqldump -uroot -proot --single-transaction --dump-slave=2 -A > all.sql
在從庫進行備份時,指定--dump-slave會在備份的sql中生成帶有主庫binlog位置的CHANGE MASTER語句;如果要給主庫新增從庫,可以在從庫這樣進行備份。
--enable-cleartext-plugin
Enable cleartext authentication plugin 5.7.10
--events
Dump events from dumped databases
--extended-insert, -e
Use multiple-row INSERT syntax (擴展插入,一條 insert 包含多條數據)
--fields-enclosed-by
This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE
--fields-escaped-by
This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE
--fields-optionally-enclosed-by
This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE
--fields-terminated-by
This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE
--flush-logs
Flush MySQL server log files before starting dump
--flush-privileges
Emit a FLUSH PRIVILEGES statement after dumping mysql database
--force
Continue even if an SQL error occurs during a table dump
--help
Display help message and exit
--hex-blob
Dump binary columns using hexadecimal notation
--host (-h)
Host to connect to (IP address or hostname) mysqldump 支持遠程備份和遠程恢復(可遠程導表)。
--ignore-error
Ignore specified errors 5.7.1
--ignore-table
Do not dump given table (不導出指定表。指定忽略多個表時,需要重復多次,每次一個表。每個表必須同時指定數據庫和表名。例如:--ignore-table=database.table1 --ignore-table=database.table2 ……)
--include-master-host-port
Include MASTER_HOST/MASTER_PORT options in CHANGE MASTER statement produced with --dump-slave
--insert-ignore
Write INSERT IGNORE rather than INSERT statements
--lines-terminated-by
This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE
--lock-all-tables (-x)
Lock all tables across all databases (請求鎖定所有數據庫中的所有表,以保證數據的一致性。這是一個全局讀鎖,並且自動關閉--single-transaction 和--lock-tables 選項)
--lock-tables
Lock all tables before dumping them
--log-error
Append warnings and errors to named file
--login-path
Read login path options from .mylogin.cnf
--master-data
Write the binary log file name and position to the output (該選項將binlog的位置和文件名追加到輸出文件中。如果為1,將會輸出CHANGE MASTER 命令;如果為2,輸出的CHANGE MASTER命令前添加注釋信息。該選項將打開--lock-all-tables 選項,但當--single-transaction也被指定時,在開始導出時會獲得很短的時間的全局讀鎖;其他內容參考下面的--single-transaction選項。該選項自動關閉--lock-tables選項)
--max_allowed_packet
Maximum packet length to send to or receive from server
--net_buffer_length
Buffer size for TCP/IP and socket communication
--no-autocommit
Enclose the INSERT statements for each dumped table within SET autocommit = 0 and COMMIT statements
--no-create-db (-n)
Do not write CREATE DATABASE statements (只導出數據,而不添加CREATE DATABASE 語句)
--no-create-info (-t)
Do not write CREATE TABLE statements that re-create each dumped table (只導出數據,而不添加CREATE TABLE 語句)
--no-data (-d)
Do not dump table contents (只生成創建表結構的語句,不導出數據)
--no-defaults
Read no option files (不會讀取任何配置文件。如果由於從配置文件讀取未知選項而導致程序啟動失敗,可以使用--no-defaults來防止它們被讀取)
--no-set-names
Same as --skip-set-charset
--no-tablespaces
Do not write any CREATE LOGFILE GROUP or CREATE TABLESPACE statements in output
--opt
Shorthand for --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset.
--order-by-primary
Dump each table's rows sorted by its primary key, or by its first unique index (mysqldump 導出時根據主鍵或唯一索引排序,以加快導入速度)
--password
Password to use when connecting to server
--pipe
On Windows, connect to server using named pipe
--plugin-dir
Directory where plugins are installed
--port
TCP/IP port number to use for connection
--print-defaults
Print default options
--protocol
Connection protocol to use
--quick
Retrieve rows for a table from the server a row at a time (不緩沖查詢,直接導出到標准輸出。默認為打開狀態,使用--skip-quick取消該選項)
--quote-names
Quote identifiers within backtick characters
--replace
Write REPLACE statements rather than INSERT statements
--result-file
Direct output to a given file
--routines
Dump stored routines (procedures and functions) from dumped databases
--secure-auth
Do not send passwords to server in old (pre-4.1) format
--set-charset
Add SET NAMES default_character_set to output
--set-gtid-purged = [off | on]
Whether to add SET @@GLOBAL.GTID_PURGED to output (--set-gtid-purged=OFF 設置備份文件中不存儲GTIDs,如果不設置,恢復時可能報錯:ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty. 這時需要執行 reset master,清除GTID_EXECUTED的值)
--shared-memory-base-name
The name of shared memory to use for shared-memory connections
--single-transaction
Issue a BEGIN SQL statement before dumping data from server (該選項在導出數據之前提交一個BEGIN SQL語句,且不鎖表,BEGIN 不會阻塞任何應用程序且能保證導出時數據庫的一致性狀態。它只適用於多版本存儲引擎,僅InnoDB。本選項和--lock-tables 選項是互斥的,因為LOCK TABLES 會使任何掛起的事務隱含提交。要想導出大表的話,應結合使用--quick 選項。)
--skip-add-drop-table
Do not add a DROP TABLE statement before each CREATE TABLE statement
--skip-add-locks
Do not add locks
--skip-comments
Do not add comments to dump file
--skip-compact
Do not produce more compact output
--skip-disable-keys
Do not disable keys
--skip-extended-insert
Turn off extended-insert (關閉擴展插入,即sql文件中,一行只有一個insert....value())
--skip-opt
Turn off options set by --opt
--skip-quick
Do not retrieve rows for a table from the server a row at a time
--skip-quote-names
Do not quote identifiers
--skip-set-charset
Do not write SET NAMES statement
--skip-triggers
Do not dump triggers
--skip-tz-utc
Turn off tz-utc
--socket
For connections to localhost, the Unix socket file to use
--ssl
Enable secure connection
--ssl-ca
Path of file that contains list of trusted SSL CAs
--ssl-capath
Path of directory that contains trusted SSL CA certificates in PEM format
--ssl-cert
Path of file that contains X509 certificate in PEM format
--ssl-cipher
List of permitted ciphers to use for connection encryption
--ssl-crl
Path of file that contains certificate revocation lists
--ssl-crlpath
Path of directory that contains certificate revocation list files
--ssl-key
Path of file that contains X509 key in PEM format
--ssl-mode
Security state of connection to server 5.7.11
--ssl-verify-server-cert
Verify server certificate Common Name value against host name used when connecting to server
--tab=dir_name (-T dir_name)
Produce tab-separated data files (生成制表符分隔的文本格式數據文件。對於每個dump表,mysqldump會創建一個 tbl_name.sql 文件,其中包含創建表的CREATE TABLE語句,並且會創建一個包含該表數據的 tbl_name.txt 文件,選項值是寫入文件的目錄。即表結構與數據分離。默認情況下,.txt數據文件使用制表符分隔每列,換行分隔每行。也可以使用--fields-terminated-by=',' 和 --lines-terminated-by='\n\n' 明確指定分隔符)
--tables
Override --databases or -B option (覆蓋--databases或-B選項。mysqldump將該選項后面的所有名稱參數視為表名)
--tls-version
Protocols permitted for secure connections 5.7.10
--triggers
Dump triggers for each dumped table
--tz-utc
Add SET TIME_ZONE='+00:00' to dump file
--user
MySQL user name to use when connecting to server
--verbose
Verbose mode
--version
Display version information and exit
--where (-w)
Dump only rows selected by given WHERE condition (根據條件備份指定的行,如 --where="uid=123 and name='xxx'")
--xml
Produce XML output