MySQL Backup mysqldump 常用選項與主要用法
正文
The mysqldump client utility performs logical backups, producing a set of SQL statements that can be executed to reproduce the original database object definitions and table data. It dumps one or more MySQL databases for backup or transfer to another SQL server. The mysqldump command can also generate output in CSV, other delimited text, or XML format.
mysqldump是MySQL官方自帶的邏輯備份工具。備份結果是生成一系列的文字SQL語句,可以很方便地用作資料備份,也可以用於資料遷移。平時邏輯備份時用mysqldump是最多的,但也沒好好進行學習總結,本文主要就mysqldump常用選項和主要用法作個學習總結。
關於mysqldump更詳細的資訊可以參考官方文件:https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
本文使用的MySQL版本為官方社群版 5.7.24
。
([email protected]) [test] > select version(); +------------+ | version() | +------------+ | 5.7.24-log | +------------+ 1 row in set (0.00 sec)
主要說明
# mysqldump --help mysqldump Ver 10.13 Distrib 5.7.24, for linux-glibc2.12 (x86_64) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Dumping structure and contents of MySQL databases and tables. Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf The following groups are read: mysqldump client The following options may be given as the first argument: --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file, except for login file. --defaults-file=# Only read default options from the given file #. --defaults-extra-file=# Read this file after the global files are read. --defaults-group-suffix=# Also read groups with concat(group, suffix) --login-path=# Read this path from the login file. -A, --all-databases Dump all the databases. This will be same as --databases with all databases selected. -Y, --all-tablespaces Dump all the tablespaces. -y, --no-tablespaces Do not dump any tablespace information. --add-drop-database Add a DROP DATABASE before each create. --add-drop-table Add a DROP TABLE before each create. (Defaults to on; use --skip-add-drop-table to disable.) --add-drop-trigger Add a DROP TRIGGER before each create. --add-locks Add locks around INSERT statements. (Defaults to on; use --skip-add-locks to disable.) --allow-keywords Allow creation of column names that are keywords. --apply-slave-statements Adds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'START SLAVE' to bottom of dump. --bind-address=name IP address to bind to. --character-sets-dir=name Directory for character set files. -i, --comments Write additional information. (Defaults to on; use --skip-comments to disable.) --compatible=name Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires MySQL server version 4.1.0 or higher. This option is ignored with earlier server versions. --compact Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs. Enables options --skip-add-drop-table --skip-add-locks --skip-comments --skip-disable-keys --skip-set-charset. -c, --complete-insert Use complete insert statements. -C, --compress Use compression in server/client protocol. -a, --create-options Include all MySQL specific create options. (Defaults to on; use --skip-create-options to disable.) -B, --databases Dump several databases. Note the difference in usage; in this case no tables are given. All name arguments are regarded as database names. 'USE db_name;' will be included in the output. -#, --debug[=#] This is a non-debug version. Catch this and exit. --debug-check This is a non-debug version. Catch this and exit. --debug-info This is a non-debug version. Catch this and exit. --default-character-set=name Set the default character set. --delete-master-logs Delete logs on master after backup. This automatically enables --master-data. -K, --disable-keys '/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and '/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put in the output. (Defaults to on; use --skip-disable-keys to disable.) --dump-slave[=#] This causes the binary log position and filename of the master to be appended to the dumped data output. Setting the value to 1, will printit as a CHANGE MASTER command in the dumped data output; 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. -E, --events Dump events. -e, --extended-insert Use multiple-row INSERT syntax that include several VALUES lists. (Defaults to on; use --skip-extended-insert to disable.) --fields-terminated-by=name Fields in the output file are terminated by the given string. --fields-enclosed-by=name Fields in the output file are enclosed by the given character. --fields-optionally-enclosed-by=name Fields in the output file are optionally enclosed by the given character. --fields-escaped-by=name Fields in the output file are escaped by the given character. -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. --flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql database. This option should be used any time the dump contains the mysql database and any other database that depends on the data in the mysql database for proper restore. -f, --force Continue even if we get an SQL error. -?, --help Display this help message and exit. --hex-blob Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format. -h, --host=name Connect to host. --ignore-error=name A comma-separated list of error numbers to be ignored if encountered during dump. --ignore-table=name Do not dump the specified table. To specify more than one table to ignore, use the directive multiple times, once for each table. Each table must be specified with both database and table names, e.g., --ignore-table=database.table. --include-master-host-port Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' in dump produced with --dump-slave. --insert-ignore Insert rows with INSERT IGNORE. --lines-terminated-by=name Lines in the output file are terminated by the given string. -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.) --log-error=name Append warnings and errors to given file. --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. --max-allowed-packet=# The maximum packet length to send to or receive from server. --net-buffer-length=# The buffer size for TCP/IP and socket communication. --no-autocommit Wrap tables with autocommit/commit statements. -n, --no-create-db Suppress the CREATE DATABASE ... IF EXISTS statement that normally is output for each dumped database if --all-databases or --databases is given. -t, --no-create-info Don't write table creation info. -d, --no-data No row information. -N, --no-set-names Same as --skip-set-charset. --opt Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt. --order-by-primary Sorts each table's rows by primary key, or first unique key, if such a key exists. Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer. -p, --password[=name] Password to use when connecting to server. If password is not given it's solicited on the tty. -P, --port=# Port number to use for connection. --protocol=name The protocol to use for connection (tcp, socket, pipe, memory). -q, --quick Don't buffer query, dump directly to stdout. (Defaults to on; use --skip-quick to disable.) -Q, --quote-names Quote table and column names with backticks (`). (Defaults to on; use --skip-quote-names to disable.) --replace Use REPLACE INTO instead of INSERT INTO. -r, --result-file=name Direct output to a given file. This option should be used in systems (e.g., DOS, Windows) that use carriage-return linefeed pairs (\r\n) to separate text lines. This option ensures that only a single newline is used. -R, --routines Dump stored routines (functions and procedures). --set-charset Add 'SET NAMES default_character_set' to the output. (Defaults to on; use --skip-set-charset to disable.) --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. --dump-date Put a dump date to the end of the output. (Defaults to on; use --skip-dump-date to disable.) --skip-opt Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. -S, --socket=name The socket file to use for connection. --secure-auth Refuse client connecting to server if it uses old (pre-4.1.1) protocol. Deprecated. Always TRUE --ssl-mode=name SSL connection mode. --ssl Deprecated. Use --ssl-mode instead. (Defaults to on; use --skip-ssl to disable.) --ssl-verify-server-cert Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead. --ssl-ca=name CA file in PEM format. --ssl-capath=name CA directory. --ssl-cert=name X509 cert in PEM format. --ssl-cipher=name SSL cipher to use. --ssl-key=name X509 key in PEM format. --ssl-crl=name Certificate revocation list. --ssl-crlpath=name Certificate revocation list path. --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1 -T, --tab=name Create tab-separated textfile for each table to given path. (Create .sql and .txt files.) NOTE: This only works if mysqldump is run on the same machine as the mysqld server. --tables Overrides option --databases (-B). --triggers Dump triggers for each dumped table. (Defaults to on; use --skip-triggers to disable.) --tz-utc SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones. (Defaults to on; use --skip-tz-utc to disable.) -u, --user=name User for login if not current user. -v, --verbose Print info about the various stages. -V, --version Output version information and exit. -w, --where=name Dump only selected records. Quotes are mandatory. -X, --xml Dump a database as well formed XML. --plugin-dir=name Directory for client-side plugins. --default-auth=name Default authentication client-side plugin to use. --enable-cleartext-plugin Enable/disable the clear text authentication plugin.
常用選項
連線選項(Connection Options)
--bind-address=ip_address
當MySQL伺服器有多個網路介面,指定mysqldump
連線MySQL伺服器使用的網路介面。--compress, -C
當服務端和客戶端都支援壓縮時,指定壓縮傳輸的資料。--host=host_name, -h host_name
指定需要dump資料的MySQL伺服器,預設是localhost。--port=port_num, -P port_num
指定連線MySQL伺服器的埠。--user=user_name, -u user_name
指定連線MySQL伺服器的使用者名稱。- --password[=password], -p[password]
指定連線MySQL伺服器的使用者密碼。- 如果命令列使用 -p 選項指定密碼,則選項與密碼串之間不允許有空格,不過命令列中密碼串明文是不安全的;
- 如果命令列選項後面沒有指定密碼串,則
mysqldump
在連線前會進行提示輸入。
--pipe, -W
在Windows環境下,指定命名管道連線MySQL伺服器。此選項僅當伺服器支援命名管道連線才生效。--socket=path, -S path
在Unix系統下指定本地連線使用socket檔案,類似於Windows環境下的命名管道。
引數檔案選項(Option-File Options)
--defaults-file=file_name
指定讀取的引數檔案。如果這個檔案不存存或是無法訪問則會報錯。--no-defaults
不讀取任何的引數檔案。--print-defaults
顯示mysqldump
讀取的引數檔案。
DDL選項(DDL Options)
--add-drop-database
在每行CREATE DATABASE
語句前新增DROP DATABASE
語句。這個選項通常與 --all-databases或 --databases選項一起使用,因為如果沒有指定這兩個選項之一也不會有CREATE DATABASE
語句。--add-drop-table
在dump輸出中每行CREATE TABLE
語句前新增DROP TABLE
語句。--add-drop-trigger
在dump輸出中每行CREATE TRIGGER
語句前新增DROP TRIGGER
語句。--no-create-db, -n
在dump輸出中禁止生成CREATE DATABASE
語句,即使指定了 --all-databases或 --databases選項。--no-create-info, -t
每張表dump的時候都不生成CREATE TABLE
語句。--no-tablespaces, -y
在dump輸出中都禁止生成CREATE LOGFILE GROUP
和CREATE TABLESPACE
語句。--replace
在dump輸出中使用REPLACE
語句代替INSERT
語句。
除錯選項(Debug Options)
--comments, -i
在dump輸出中增加額外的資訊,如mysqldump版本,MySQL版本,MySQL主機名等。預設是開啟狀態,如果要禁止,則可以指定選項 --skip-comments。--debug-info
在mysqldump
退出時列印除錯資訊以及CPU和記憶體的使用統計資訊。--dump-date
當啟用選項 --comments時,在dump輸出中的末尾新增結束時間。可以使用選項 --skip-dump-date禁止新增。--force, -f
忽略dump表過程當中的SQL錯誤。這個選項適用於當指定dump檢視,檢視所對應的表被清除而變成invaild狀態時,mysqldump
會記錄檢視定義以及錯誤資訊並繼續執行dump操作。如果未指定該選項,則mysqldump
報錯並退出。--log-error=file_name
指定警告和錯誤資訊記錄的檔案,預設不記錄。--verbose, -v
詳細模式,列印mysqldump
執行更詳細的資訊。
國際化選項(Internationalization Options)
--character-sets-dir=dir_name
指定字符集安裝所在的目錄。--default-character-set=charset_name
指定預設的字符集,如果沒指定,預設為utf8。--set-charset
新增SET NAMES default_character_set
語句到dump檔案輸出中。預設是開啟的,可以使用選項 --skip-set-charset禁止。--no-set-names, -N
關閉選項 --set-charset。
複製選項(Replication Options)
--apply-slave-statements
指定選項 --dump-slave的從庫dump過程中,在CHANGE MASTER TO
語句之前新增STOP SLAVE
語句,在dump結束之後再新增START SLAVE
語句。--delete-master-logs
在主伺服器上,在完成dump之後傳送語句PURGE BINARY LOGS
到主伺服器執行。此選項會自動開啟 --master-data。--dump-slave[=value]
這個選項類似於 --master-data,只不過作用於mysqldump
在從庫生成的dump檔案,即mysqldump
在從庫進行備份。這個選項的作用是為了產生一個與備份從庫相同主庫的從庫,記錄新的從庫需要從主庫開啟複製讀取的binlog檔案和binlog位置,生成新的CHANGE MASTER TO
語句,binlog檔案和binlog位置分別讀取來自於SHOW SLAVE STATUS
狀態語句當中的Relay_Master_Log_File
和Exec_Master_Log_Pos
值。該選項的取值與 --master-data一致。
該選項會使mysqldump
在開始dump之前停止SQL thread,而在dump結束之後再重新開啟。--include-master-host-port
指定選項 --dump-slave的從庫dump過程中,生成CHANGE MASTER TO
語句新增MASTER_HOST
和MASTER_PORT
資訊以便可以通過新dump檔案建立新的從庫開啟主從複製。- --master-data[=value]
在主伺服器完成mysqldump
指定該選項匯出備份檔案可以用來搭建從庫。在dump檔案中包含了CHANGE MASTER TO
語句,該語句表示新搭建的從庫需要從主庫哪個binlog檔案和位置開始進行同步。
該選項需要RELOAD
許可權和MySQL中開啟binlog,會關閉選項 --lock-tables並開啟選項 --lock-all-tables,除非指定了選項 --single-transaction。
value取值如下:- 2:則
CHANGE MASTER TO
語句是以註釋的形式存在dump檔案中,在dump檔案匯入的過程當中並不會生效; - 1:則
CHANGE MASTER TO
語句是以非註釋的形式存在dump檔案中,在dump檔案匯入的過程中會生效; - 未指定:則預設值為1。
- 2:則
- --set-gtid-purged=value
指定是否在dump檔案中寫入SET @@GLOBAL.gtid_purged
語句,同時會導致dump檔案在被過載的時候不會記錄binlog。
value取值如下:- OFF:不新增
SET @@GLOBAL.gtid_purged
語句,同時不新增SET @@SESSION.SQL_LOG_BIN=0
語句; - ON:新增
SET @@GLOBAL.gtid_purged
語句,如果沒開啟GTID則報錯,同時新增SET @@SESSION.SQL_LOG_BIN=0
語句; - AUTO:如果開啟GITD則新增
SET @@GLOBAL.gtid_purged
語句,同時新增SET @@SESSION.SQL_LOG_BIN=0
語句,預設選項。
- OFF:不新增
格式化選項(Format Options)
--compact
更加緊湊的輸出dump檔案。該選項同時會開啟 --skip-add-drop-table,--skip-add-locks,--skip-comments,--skip-disable-keys和 --skip-set-charset選項。--complete-insert, -c
指定dump檔案中的INSERT語句為包含所有欄位名稱的完整語句。--create-options
指定dump檔案中CREATE TABLE
語句包含所有MySQL表選項。--hex-blob
轉儲二進位制列為十六進位制格式。涉及的型別有BINARY
、VARBINARY
、BLOB
和BIT
。--quote-names, -Q
指定dump檔案中資料庫、表和欄位的引用識別符號為反引號 `,預設開啟,通過選項 --skip-quote-names來禁用。如果SQL_MODE當中有ANSI_QUOTES,則識別符號為雙引號 "。--result-file=file_name, -r file_name
指定dump輸出到給定檔案。--tz-utc
指定dump檔案中表TIMESTAMP
欄位匯出格式以適應不同時區伺服器匯入時值的準確性。預設是開啟,使用選項 --skip-tz-utc禁用。
過濾選項(Filtering Options)
--all-databases, -A
指定dump出所有庫當中的所有表。--databases, -B
指定dump一個或多個數據庫。該選項會在dump每個資料庫之前新增CREATE DATABASE
和USE
語句,如果沒有指定該選項,則mysqldump
將第一個引數當成是資料庫,第二個引數當成是表。通常用來備份INFORMATION_SCHEMA和performance_schema庫,預設情況下是不匯出這兩個庫的。--no-data, -d
指定只dump表結構,而不dump表資料。--tables
指定dump一張或多張表。會覆蓋選項 --databases, -B,把該選項之後所有引數當成是表。--ignore-table=db_name.tbl_name
指定忽略dump某張表。格式必須是db.table
,如果有多張表需要多次指定該選項,也適用於檢視。--where='where_condition', -w 'where_condition'
指定where條件dump資料。--events, -E
指定dump資料庫中的計劃事件。需要對庫有EVENT
許可權。--routines, -R
指定dump資料庫中的函式和儲存過程。--triggers
指定dump表中的觸發器。需要對錶有TRIGGER
許可權。
效能選項(Performance Options)
--disable-keys, -K
指定dump檔案裡INSERT
表時新增/*!40000 ALTER TABLE tbl_name DISABLE KEYS */
語句,完成後再新增/*!40000 ALTER TABLE tbl_name ENABLE KEYS */
語句,這樣可以加快表資料匯入速度,但只對有非唯一索引的MyISAM表有效果。--extended-insert, -e
指定dump檔案當中的INSERT
語句為多行格式,降低dump檔案大小。--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
對於大表的dump很有幫助。預設開啟。該選項使mysqldump
直接從表中獲取資料輸出而不經過MySQL的快取。
事務選項(Transactional Options)
--add-locks
指定dump檔案中每張表INSERT語句之前新增LOCK TABLES
語句,INSERT語句之後新增UNLOCK TABLES
語句。這樣可以提高匯入效率。--flush-logs, -F
在dump開始之前切換日誌。該選項需要RELOAD
許可權。如果與選項 --all-databases聯合使用的話會導致每dump一個庫之前都要進行日誌切換。如果有使用選項 --lock-all-tables、--master-data或者 --single-transaction,則在dump過程當中只進行一次日誌切換。如果希望dump與日誌的切換同時發生,則使用 --flush-logs與選項 --lock-all-tables、--master-data或者 --single-transaction一同使用。--flush-privileges
在dump完mysql資料庫之後新增FLUSH PRIVILEGES
語句。--lock-all-tables, -x
在dump過程中鎖住所有資料庫中所有表。該選項會自動關閉 --single-transaction和 --lock-tables選項。--lock-tables, -l
在dump每個庫時鎖定庫中的表。這個選項只能保證庫中表的狀態一致,因為針對每個庫進行加鎖,所以無法保證所有庫都是一致狀態。--no-autocommit
指定dump檔案中每條INSERT
語句包含在SET autocommit = 0
和COMMIT
語句之間。--order-by-primary
在dump每張表的時候以表的主鍵或第一個唯一索引為序。對於匯出是MyISAM表而匯入是InnoDB表很有用。--single-transaction
在dump開始之前將會話事務隔離級別設定成REPEATABLE READ
並執行語句START TRANSACTION
,只適用於事務引擎的表如InnoDB。當指定該選項進行dump的過程中,應該避免其它會話連線執行如下語句:ALTER TABLE,CREATE TABLE,DROP TABLE,RENAME TABLE,TRUNCATE TABLE
,因為一致性讀並不能隔離以上語句,所以當mysqldump
執行SELECT
語句進行dump表資料時,其它會話執行以上語句容易產生資料不一致或者報錯。
該選項與 --lock-tables是互斥的,因為LOCK TABLES
語句會導致在執行的事務隱式提交。
用法示例
- 備份所有庫
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 -A > test_all.sql
- 備份一個或多個庫
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 -B test1 test2 > test1_test2.sql
- 備份一張表或多張表
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 t1 > test1_t1.sql
或
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 --tables t1 > test1_t1.sql
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 t1 t2 > test1_t1_t2.sql
或
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 --tables t1 t2 > test1_t1_t2.sql
- 備份庫結構(同時會包含表結構)
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 --no-data -B test1 test2 > test1_test2.sql
- 只備份表結構
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 --no-data test1 --tables t1 t2 > test1_t1_t2.sql
- 只備份表資料(不包含建表語句)
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 --no-create-info test1 --tables t1 t2 > test1_t1_t2.sql
- 忽略一張或多張表備份
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 --ignore-table=test1.t1 --ignore-table=test1.t2 > test1_ig_t1_t2.sql
- 輸出檔案通過管道傳到gzip命令進行壓縮
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 -A | gzip > test_all.sql.gz
- 指定where條件備份表資料
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 test1 --tables t1 --where='c1>1000000' > test1_t1_w.sql
不包含建表語句
# mysqldump -uadmin -p -h192.168.58.3 -P3306 -E -R --triggers --single-transaction --master-data=2 --no-create-info test1 --tables t1 --where='c1>1000000' > test1_t1_w.sql
注意事項
- 預設情況下,mysqldump是不會匯出INFORMATION_SCHEMA、performance_schema和sys的,如果需要匯出,則在命令列中顯示指定選項 --databases, -B;
- mysqldump不會匯出InnoDB建立表空間
CREATE TABLESPACE
語句; - 不建議使用當前版本的mysqldump匯出檔案匯入MySQL5.6.9(或更早)開啟GTID的版本;
- 如果general_log和slow_query_log都是指定存儲存在表中,mysqldump匯出檔案包含general_log和slow_query_log表的建立語句,但並不包含表資料。
參考
- https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
- https://www.xp8.net/data/788.html
- http://www.ywnds.com/?p=3867
☆〖本人水平有限,文中如有錯誤還請留言批評指正!〗☆