使用mysqldump導出數據庫
mysqldump是mysql用于轉存儲數據庫的客戶端程序。它主要產生一系列的SQL語句,可以封裝到文件,該文件包含有所有重建您的數據庫所需要的 SQL命令如CREATE DATABASE,CREATE TABLE,INSERT等等。可以用來實現輕量級的快速遷移或恢復數據庫。是mysql數據庫實現邏輯備份的一種方式。本文描述了mysqldump的一些重要參數以及給出了相關示例供大家參考。
1、獲取mysqldump的幫助信息
[root@SZDB ~]# mysqldump --help|more
mysqldump Ver 10.13 Distrib 5.6.12, for Linux (x86_64)
#以下為MySQL dump調用的幾種常用方式
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]
#如果沒有指定任何表或使用了---database或--all--database選項,則轉儲整個數據庫
--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.
-q, --quick Don't buffer query, dump directly to stdout.
(Defaults to on; use --skip-quick to disable.)
#以上2個參數未使用的情況下,在轉儲結果之前會把全部內容載入到內存中,對于較大的數據庫轉儲將嚴重影響性能。
#缺省情況下這2個參數為開啟狀態。有些類似于Oracle的繞過PGA而直接寫direct write。
--skip-opt Disable --opt. Disables --add-drop-table, --add-locks,
--create-options, --quick, --extended-insert,
--lock-tables, --set-charset, and --disable-keys.
#skip-opt與前2個參數相反,在轉儲之前先load到內存中。
--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.
#產生與其它數據庫系統或舊版本MySQL服務器相兼容的輸出。用于跨數據庫,跨版本之間的遷移。
#其值可以為ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options或者no_field_options。
#如果要使用多個值,用逗號將它們隔開。該選項不能保證同其它數據庫服務器之間的完全兼容。如Oracle的數據類型等。
--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.
#該選項使得輸出的文件更小,啟用后等用于使用一些skip項等。
-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.
#該選項一次導出多個數據庫所有名字參量看作數據庫名,更重要的是會生成CREATE DATABASE IF NOT EXISTS dbname
--default-character-set=name
Set the default character set.
#設置導出腳本的字符集,未指定的情況下為UTF8。
--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.
#在dump mysql數據庫以及依賴于mysql數據庫恢復時建議使用該選項生成FLUSH PRIVILEGES語句
-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.
#在啟動dump前會flush日志,此方式可以用于實現增量備份
-d, --no-data No row information.
#不輸出數據行,僅導出結構
-f, --force Continue even if we get an SQL error.
#在碰到錯誤時,依舊強制dump
--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.
#添加二進制日志位置到輸出。1表示輸出change master命令,2則注釋輸出change master命令。
-R, --routines Dump stored routines (functions and procedures).
#導出函數和過程以及觸發器,缺省情況下,這些不會被導出
-t, --no-create-info
Don't write table creation info.
#不生成建表語句
--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.
#創建一致性快照,僅僅針對innodb引擎
#不能存在其他操作:ALTER TABLE, DROP TABLE, RENAME TABLE,TRUNCATE TABLE,關閉--lock-tables。
-w, --where=name Dump only selected records. Quotes are mandatory.
#使用where子句只導出符合條件的記錄
# Author : Leshami
# Blog : http://blog.csdn.net/leshami
2、dump的相關示例
備份單個數據庫
shell> mysqldump -uroot -pxxx sakila >sakila.sql
帶壓縮方式備份數據庫
shell> mysqldump -uroot -pxxx sakila |gzip >sakila.sql.gz
備份數據庫上的特定表
shell> mysqldump -uroot -pxxx sakila actor >sakila_actor.sql
備份表上特定的記錄
shell> mysqldump -uroot -pxxx sakila actor -w "first_name='NICK'" >sakila_actor_row.sql
備份數據庫的結構
shell> mysqldump -uroot -pxxx sakila --no-data >sakila_structure.sql
同時備份多個數據庫
shell> mysqldump -uroot -pxxx --database sakila tempdb test >multidb.sql
備份服務器上的所有數據庫
shell> mysqldump -uroot -pxxx --all-databases --opt --compact --flush-privileges >alldb.sql
只導出數據庫中的存儲過程,函數,觸發器
shell> mysqldump -uroot -pxxx sakila --no-create-db --no-data --no-tablespaces --no-create-info --routines >sakila.sql
全量備份與增量備份
#下面首先對數據庫做全量備份并在備份前flush日志
shell> mysqldump -uroot -pxxx --single-transaction --flush-logs --master-data=2 sakila >sakila_full.sql
#假定備份時flush的日志為mysql-bin.000004,則使用如下方式恢復
shell> mysql -uroot -pxxx < sakila_full.sql
shell> mysqlbinlog mysql-bin.000004 | mysql -uroot -pPwd
3、其它
a、對比--database與直接數據庫備份,如下,也就是說使用--database會生成建庫語句
shell> mysqldump -uroot -pxxx --database tempdb >tempdb1.log
shell> mysqldump -uroot -pxxx --opt tempdb >tempdb2.log
shell> grep DATABASE tempdb1.log
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET utf8 */;
shell> grep DATABASE tempdb2.log
b、性能相關
使用--quick或者--opt有助于加快dump的過程,減少dump所需的時間。
mysqldump不適用于大型數據庫備份與恢復,速度慢,不支持并行,其次SQL重放將耗用大量的I/O。對于這種情形,建議使用物理備份方式。
如果mysql數據庫中使用的存儲引擎主要為innodb或myisam,或者2者的混合,可以考慮使用mysql企業版更高效的mysqlbackup工具。
如果mysql數據庫中主要的表為myisam,更好的性能建議使用mysqlhotcopy方式。
詳細參考:http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
來自:http://blog.csdn.net/leshami/article/details/40144349