mysqldump命令参数
mysql
mysqldump客户端可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器)。转储包含创建表和/或装载表的SQL语句。
如果你在服务器上进行备份,并且表均为MyISAM表,应考虑使用mysqlhotcopy,因为可以更快地进行备份和恢复。
有3种方式来调用mysqldump:
shell> mysqldump [options] db_name [tables]shell> mysqldump [options] ---database DB1 [DB2 DB3...]shell> mysqldump [options] --all--database
如果没有指定任何表或使用了---database或--all--database选项,则转储整个数据库。
要想获得你的版本的mysqldump支持的选项,执行mysqldump ---help。
如果运行mysqldump没有--quick或--opt选项,mysqldump在转储结果前将整个结果集装入内存。如果转储大数据库可能会出现问题。该选项默认启用,但可以用–skip-opt禁用。
如果使用最新版本的mysqldump程序生成一个转储重装到很旧版本的MySQL服务器中,不应使用–opt或-e选项。
mysqldump支持下面的选项:
1、--print-defaults Print the program argument list and exit.
打印程序参数列表并退出
2、--no-defaults Don't read default options from any option file
已不从任何选项文件读取默认选项
3、--defaults-file=# Only read default options from the given file #.
只有给指定的文件读取默认选项
4、--defaults-extra-file=# Read this file after the global files are read.
在全局下面读取这个文件
5、-A, --all-databases Dump all the databases. This will be same as --databases
所有的数据库
6、-Y, --all-tablespaces Dump all the tablespaces.
导出全部表空间
7、-y, --no-tablespaces Do not dump any tablespace information.
不导出任何表空间
8、--add-drop-database Add a DROP DATABASE before each create.
每个数据库创建之前添加drop数据库语句
9、--add-drop-table Add a DROP TABLE before each create.
每个数据表创建之前添加drop数据表语句。(默认为打开状态,使用–sjip-add-drop-table取消选项)(Defaults to on; use --skip-add-drop-table to disable.)
10、--add-locks Add locks around INSERT statements.(Defaults to on; use --skip-add-locks to disable.)
在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE(为了使得更快地插入到MySQL)。默认开启。
11、--allow-keywords Allow creation of column names that are keywords.
允许创建是关键词的列明子。这由表名前缀每个列名做到
12、 --apply-slave-statementsAdds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'STARTSLAVE' to bottom of dump
在CHANGE MASTER前添加STOP SLAVE,并且在导出的最后添加START SLAVE.
13、--character-sets-dir=nameDirectory for character set files.
字符集文件的目录
14、-i, --comments Write additional information.
附加注释信息。默认为打开,可以用--skip-comments取消
15、--compatible=name Change the dump to be compatible with a given mode. Bydefault tables are dumped in a format optimized forMySQL. Legal modes are: ansi, mysql323, mysql40,postgresql, oracle, mssql, db2, maxdb, no_key_options,no_table_options, no_field_options. One can use severalmodes separated by commas. Note: Requires MySQL serverversion 4.1.0 or higher. This option is ignored withearlier server versions.
导出数据将和其他数据库和旧版本的MySQL 相兼容.值可以为ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_opt、no_tables_options、no_field
例子:
mysqldump -uroot -p --all-databases --compatible=ansi
16、 --compact Give less verbose output (useful for debugging). Disablesstructure comments and header/footer constructs. Enablesoptions --skip-add-drop-table --skip-add-locks--skip-comments --skip-disable-keys --skip-set-charset.
导出更少的输出信息(用于调试)去掉注释和头尾等结构。可以使用选项:
--skip-add-drop-table--skip-add-locks--skip-comments--skip-disable-key
例子:mysqldump -uroot -p --all-databases --compact
17、 -c, --complete-insertUse complete insert statements
使用完成的insert语句(包含列名称),这么做能提高插入效率,但是可能会受到max_allowed_packer参数的影响而导致插入失败
例子:mysqldump -uroot -p --all-databases --complete-insert
18、 -C, --compress Use compression in server/client protocol.
在客户端和服务器之间启用压缩传递所有信息
例子:mysqldump -uroot -p --all-databases --compress
19、-a, --create-optionsInclude all MySQL specific create options.(Defaults to on; use --skip-create-options to disable.)
在CREATE TABLE语句中包括所有MySQL特性选项。(默认为打开状态)
例子:mysqldump -uroot -p --add-databases
20、-B, --databases Dump several databases. Note the difference in usage; inthis case no tables are given. All name arguments areregarded as database names. 'USE db_name;' will beincluded in the output.
导出数据里面添加创建数据库的语句和切入数据库
21、-#, --debug[=#] This is a non-debug version. Catch this and exit.
输出debug信息,用于调试。默认值为d:t:o,/tmp/mysqldump.trace
例子:
mysqldump —uroot -p --add-databases --debugmysqldump -uroot -p --add-databases --debug="d:t:o,/tmp/debug.trace"
————-分割线————
22、--debug-check Check memory and open file usage at exit.
检查内存和打开文件使用说明并退出
例子:mysqldump -uroot -p --add-databases --debug-check
23、--debug-info Print some debug info at exit.
输出调试信息并推出
例子:mysqldump -uroot -p --add-databases --debug-info
24、--default-character-set=nameSet the default character set.
设置默认字符集,默认值为utf8
mysqldump -uroot -p --add-databases --default-character-set=latin1
25、--delayed-insert Insert rows with INSERT DELAYED.例子:mysqldump -uroot -p --add-databases --delayed-insert
采用延时插入方式(INSERT DELAYED)导出数据
26、--delete-master-logsDelete logs on master after backup. This automaticallyenables --master-data.例子:mysqldump -uroot -p --add-databases --delete-master-logs
master备份后删除日志,这个参数将自动激活--master-data
27、-K, --disable-keys '/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and'/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be putin the output.(Defaults to on; use --skip-disable-keys to disable.)例子:mysqldump -uroot -p --add-databases
对于每个表,用/*!40000 ALTER TABLE tbl_name DISABLE KEYS */;
和/*!40000 ALTER TABLE tbl_name ENABLE KEYS */;语句引用INSERT 语句。这样可以更快地导出dump出来的文件,因为它是在插入所有行后创建索引.该选项只适合MyISAM表,默认为打开状态.
28、--dump-slave[=#] This causes the binary log position and filename of themaster to be appended to the dumped data output. Settingthe value to 1, will printit as a CHANGE MASTER commandin the dumped data output; if equal to 2, that commandwill be prefixed with a comment symbol. This option willturn --lock-all-tables on, unless --single-transaction isspecified too (in which case a global read lock is onlytaken a short time at the beginning of the dump - don'tforget to read about --single-transaction below). In allcases any action on logs will happen at the exact momentof the dump.Option automatically turns --lock-tables off.------------------例子:mysqldump -uroot -p --add-databases --dump-slave=1mysqldump -uroot -p --all-databases --dump-slave=2
该选项导致主的binlog位置和文件名追加到导出数据的文件中。设置为1时,将会以CHANGE MASTER命令输出到数据文件;
设置为2时,在命令前增加说话信息。该选项将会打开--lock-all-tables,除非--single-transaction被指定。该选项会自动关闭–lock-tables选项。默认值为0
29、-E, --events Dump events.例子:mysqldump -uroot -p --add-databases --events
导出事件
30、-e, --extended-insertUse multiple-row INSERT syntax that include severalVALUES lists.(Defaults to on; use --skip-extended-insert to disable.)例子:mysqldump -uroot -p --all-databasesmysqldump -uroot -p --all-databases --skip-extended-insert(取消选项)
使用具有多个VALUES列的INSERT语句.并加速导入时的速度。默认为打开状态,使用--skip-extended-insert取消选项。
31、--fields-terminated-by=nameFields in the output file are terminated by the givenstring.例子:mysqldump -uroot -p test test --tab="/home/mysql" --fields-terminated-by="#"
导出文件中忽略给定字段。与--tab选项一起使用,不能用于--databases和--all-databases选项
32、--fields-enclosed-by=nameFields in the output file are enclosed by the givencharacter.例子:mysqldump -uroot -p test test --tab="/home/mysql" --fields-enclosed-by="#"
输出文件中的各个字段用给定字符包括。与--tab选项一起使用,不能用于--databases和--all-databases选项
33、--fields-optionally-enclosed-by=nameFields in the output file are optionally enclosed by thegiven character.例子:mysqldump -uroot -p test test --tab="/home/mysql"--fields-enclosed-by="#" --fields-optionally-enclosed-by ="#"
输出文件中的各个字段用给定字符选项性包括。与--tab选项一起使用,不能用于--databases和--all-databases选项
34、--fields-escaped-by=nameFields in the output file are escaped by the givencharacter.例子:mysqldump -uroot -p mysql user --tab="/home/mysql" --fields-escaped-by="#"
输出文件中的各个字段忽略给定字符.与–tab选项一起使用,不能用于--databases和--all-databases选项
35、-F, --flush-logs Flush logs file in server before starting dump. Note thatif you dump many databases at once (using the option--databases= or --all-databases), the logs will beflushed for each database dumped. The exception is whenusing --lock-all-tables or --master-data: in this casethe logs will be flushed only once, corresponding to themoment all tables are locked. So if you want your dumpand the log flush to happen at the same exact moment youshould use --lock-all-tables or --master-data with--flush-logs.例子:mysqldump -uroot -p --add-databases --flush-privileges
开始导出之前刷新日志
请注意:加入一次导出多个数据库(使用选项--databases或者--all-databases)将会逐个数据库刷新日志.除使用--lock-all-tables或者--master-data外。在这种情况下,日志将会被刷新一次,相应的所有表同时被锁定。因此,如果打算同时导出和刷新日志应该使用--lock-all-tables或者--master-data和--flush-logs
36、--flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysqldatabase. This option should be used any time the dumpcontains the mysql database and any other database thatdepends on the data in the mysql database for properrestore.例子:mysqldump -uroot -p --all-databases --flush-privilege
在导出mysql数据库之后,发出一条FLUSH PRIVILEGES语句.为了正确恢复,该选项应该用于导出mysql数据库和依赖mysql数据库数据的任何时候。
37、-f, --force Continue even if we get an SQL error.例子:mysqldump -uroot -p --add-databases --force
在导出过程中忽略出现的SQL错误
38、-?, --help Display this help message and exit.例子:mysqldump --help
显示帮助信息并退出
39、--hex-blob Dump binary strings (BINARY, VARBINARY, BLOB) inhexadecimal format.例子:mysqldump -uroot -p --all-databases --hex-blob
使用十六进制格式导出二进制字符串字段。如果有二进制数据就必须使用该选项。影响到的字段类型有BINARY、VARBINARY、BLOB.
40、-h, --host=name Connect to host.例子:mysqldump -uroot -p host=localhost --all-databases
需要导出的主机信息
41、--ignore-table=name Do not dump the specified table. To specify more than onetable to ignore, use the directive multiple times, oncefor each table. Each table must be specified with bothdatabase and table names, e.g.,--ignore-table=database.table.例如: --ignore-table=database.table1 --ignore-table=database.table2.....例子:mysqldump -uroot -p --host=localhost --all-databases --ignore-tanle=mysql.user
不导出指定表,指定忽略多个表时,需要重复多次。每次一个表,每个表必须同时指定数据库和表名
42、 --include-master-host-portAdds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGEMASTER TO..' in dump produced with --dump-slave.
在--dump-slave产生的CHANGE MASTER TO ....语句中增加'MASTER_HOST=<host>,MASTER_PROT=<prot>'
mysqldump -uroot -p –host=localhost –all-databases –include-master-host-port
43、--insert-ignore Insert rows with INSERT IGNORE.例子:mysqldump -uroot -p --host=localhost --all-databases --insert-ignore
在插入行使用INSERT IGNORE语句
44、--lines-terminated-by=nameLines in the output file are terminated by the givenstring.------例子:mysqldump -uroot -p --host=localhost test test --tab="/tmp/mysql" --lines-terminated-by="##"
输出文件的每行用定字符串划分.与--tab选项一起使用,不能用于--database和--all-databases选项。
45、-x, --lock-all-tablesLocks all tables across all databases. This is achievedby taking a global read lock for the duration of thewhole dump. Automatically turns --single-transaction and--lock-tables off.
提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭--single-transaction和--lock-tables选项
46、-l, --lock-tables Lock all tables for read.(Defaults to on; use --skip-lock-tables to disable.)-----------mysqldump -uroot -p --host=localhost --all-databases --lock-tables
开始导出前,多订所有表。用READ LOCAL锁定表以允许MyISAM表并行插入.对于支持事务的表例如InnoDB和BDB,--single-transaction是一个更好的选择,因为它根本不需要锁定表。
请注意当导出多个数据库时,--lock-tables分别为每个数据库锁定表。因此,该选项不能保证导出文件的表在数据库之间的逻辑一致性。不同数据库表的导出状态完全不同。
47、--log-error=name Append warnings and errors to given file.-------------例子:mysqldump -uroot -p --host=localhost --add-databases --log-error=/tmp/err_log
附加警告和错误信息到给定文件
48、--master-data[=#] This causes the binary log position and filename to beappended to the output. If equal to 1, will print it as aCHANGE MASTER command; if equal to 2, that command willbe prefixed with a comment symbol. This option will turn--lock-all-tables on, unless --single-transaction isspecified too (in which case a global read lock is onlytaken a short time at the beginning of the dump; don'tforget to read about --single-transaction below). In allcases, any action on logs will happen at the exact momentof the dump. Option automatically turns --lock-tablesoff.
该选项将binlog的位置和文件名追加到输出文件中。如果为1,将会输出CHANGE MASTER命令;如果为2,输出的CHANGE MASTER命令前添加注释信息。该选项打开--lock-all-tables 选项,除非--single-transaction也被锁定。
(在这种情况下,全局读锁在开始导出时获得很短的时间;其他内容参考下面的--single-transaction选项)。该选项自动关闭--lock-tables
例子:mysqldump -uroot -p --host=localhost --all-databases --master-data=1;mysqldump -uroot -p --host=localhost --all-databases --master-data=2;
——————分割线————–
49、--max-allowed-packet=#The maximum packet length to send to or receive fromserver.例子:mysqldump -uroot-p --host=localhost --all-database --max_allowed_packer=100240
服务器发送和接受的最大包长度。
50、--net-buffer-length=#The buffer size for TCP/IP and socket communication.例子:mysqldump -uroot -p --host=localhost --all-databases --net_buffer_length=10024
TCP/IP和socket连接的缓存大小
51、--no-autocommit Wrap tables with autocommit/commit statements.例子:mysqldump -uroot -p --host=localhost --all-databases --no-autocommit
使用autocommit/commit语句包裹表。
52、-n, --no-create-db Suppress the CREATE DATABASE ... IF EXISTS statement thatnormally is output for each dumped database if--all-databases or --databases is given.例子:mysqldump -uroot -p --host=localhost --all-databases --no-create-db
只导出数据,而不添加CREATE DATABASE语句
53、-t, --no-create-infoDon't write table creation info.例子:mysqldump -uroot -p --host=localhost --all-databases --no-create-info
只导出数据,而不添加CREATE TABLE 语句
54、-d, --no-data No row information.例子:mysqldump -uroot -p --host=localhost --all-databases --no-data
只导出数据,而不添加CREATE TABLE语句
55、-N, --no-set-names Same as --skip-set-charset.例子:mysqldump -uroot -p --host=localhost --all-databases --no-set-names
等同于--skip-set-charset
56、--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.--------例子:mysqldump -uroot -p --host=localhost --all-databases --opt
等同于--add-drop-table,--add-locks,--create-options,--quick,--extended-insert,--lock-tables,--set-charset,--disable-keys 该选项默认开启,可以用--skip-opt禁用
57、--order-by-primary Sorts each table's rows by primary key, or first uniquekey, if such a key exists. Useful when dumping a MyISAMtable to be loaded into an InnoDB table, but will makethe dump itself take considerably longer.例子:mysqldump -uroot -p --host=localhost --all-database --order-by-primary
如果存在主键,或者第一个唯一键,对每个表的记录进行排序。在导出MyISAM表到InnoDB表时有效,但会使得导出工作花费很长时间。
58、-p, --password[=name]Password to use when connecting to server. If password isnot given it's solicited on the tty.
连接数据库密码
59、-P, --port=# Port number to use for connection.
连接数据库端口号
60、--protocol=name The protocol to use for connection (tcp, socket, pipe,memory).例子:mysqldump -uroot -p --host=localhost --all-databases --protocol=tcp
使用的连接协议,包括:tcp、socker、pipe、memory
61、-q, --quick Don't buffer query, dump directly to stdout.(Defaults to on; use --skip-quick to disable.)------例子:mysqldump -uroot -p --host=localhost --all-databasemysqldump -uroot -p --host=localhost --all-database --skip-quick
不缓冲查询,直接导出到标准输出。默认为打开状态,使用--skip-quick取消该选项。
62、-Q, --quote-names Quote table and column names with backticks (`).(Defaults to on; use --skip-quote-names to disable.)-------mysqldump -uroot -p --host=localhost --all-databasemysqldump -uroot -p --host=localhost --all-database --skip-quote-names
使用(`)引起表和列名。默认为打开状态,使用--skip-quote-names取消该选项。
63、--replace Use REPLACE INTO instead of INSERT INTO.----------mysqldump -uroot -p --host=localhost --all-database --replace
使用REPLACE INTO 取代INSERT INTO.
64、-r, --result-file=nameDirect output to a given file. This option should be usedin systems (e.g., DOS, Windows) that use carriage-returnlinefeed pairs (\r\n) to separate text lines. This optionensures that only a single newline is used.----------------mysqldump -uroot -p --host=localhost --all-databases --result-file=/tmp/mysqldump_result_file.txt
直接输出到指定文件中。该选项应该用在使用回车换行对(\\r\\n)换行的系统上(例如:DOS,Windows)。该选项确保只有一行被使用
65、-R, --routines Dump stored routines (functions and procedures).------mysqldump -uroot -p --host=localhost --all-databases --routines
导出存储过程以及自定义函数。
66、--set-charset Add 'SET NAMES default_character_set' to the output.(Defaults to on; use --skip-set-charset to disable.)-----------------mysqldump -uroot -p --host=localhost --all-databasesmysqldump -uroot -p --host=localhost --all-database --skip-set-charset
添加'SET NAMES default_character_set'到输出文件。默认为打开状态,使用--skip-set-charset关闭选项。
67、--single-transactionCreates a consistent snapshot by dumping all tables in asingle transaction. Works ONLY for tables stored instorage engines which support multiversioning (currentlyonly InnoDB does); the dump is NOT guaranteed to beconsistent for other storage engines. While a--single-transaction dump is in process, to ensure avalid dump file (correct table contents and binary logposition), no other connection should use the followingstatements: ALTER TABLE, DROP TABLE, RENAME TABLE,TRUNCATE TABLE, as consistent snapshot is not isolatedfrom them. Option automatically turns off --lock-tables------------------mysqldump -uroot -p --host=localhost --all-database --single-transaction
改选项在导出数据之前提交一个BEGIN SQL语句,BEGIN不会阻塞任何应用程序且能保证导出时的数据库的一致性状态,他只使用与多版本存储引擎,仅InnoDB 本选项和--lock-tables 选项是互相互斥的,因为LOCK TABLES 会使任何挂起的事务隐含提交。要想导出大表的话,应结合使用--quick选项。
68、 --dump-date Put a dump date to the end of the output.(Defaults to on; use --skip-dump-date to disable.)例子:mysqldump -uroot -p --host=localhost --all-databasemysqldump -uroot -p --host=localhost --all-databases --skip-dump-date
将导出时间添加到输出文件中。默认为打开状态,使用--skip-dump-data关闭选项
69、 --skip-opt Disable --opt. Disables --add-drop-table, --add-locks,--create-options, --quick, --extended-insert,--lock-tables, --set-charset, and --disable-keys.---------------mysqldump -uroot -p --host=localhost --all-databases --skip-opt
禁用-opt选项
70、-S, --socket=name The socket file to use for connection.---------mysqldump -uroot -p --host=localhost --all-databases --skip-opt
指定连接mysql的socket文件位置,默认路径/tmp/mysql.sock
71、 -T, --tab=name Create tab-separated textfile for each table to givenpath. (Create .sql and .txt files.) NOTE: This only worksif mysqldump is run on the same machine as the mysqldserver.mysqldump -uroot -p --host=localhost test test --tab="/home/mysql"
为每个表在给定路径创建tab分隔的文本文件。注意:仅仅用于mysqldump和mysqld服务器运行在相同机器上,
72、--tables Overrides option --databases (-B).---------------mysqldump -uroot -p --host=localhost --database test --tables test
覆盖--database (-B)参数,需要导出的表名。
73、--triggers Dump triggers for each dumped table.(Defaults to on; use --skip-triggers to disable.)----------------mysqldump -uroot -p --host=localhost --all-database --trigger
导出发出器。该选项默认启用,用--skip-trigger禁用它。
74、--tz-utc SET TIME_ZONE='+00:00' at top of dump to allow dumping ofTIMESTAMP data when a server has data in different timezones or data is being moved between servers withdifferent time zones.(Defaults to on; use --skip-tz-utc to disable.)--------------mysqldump -uroot -p --host=localhost --all-databases --tz-utc
在导出顶部设置时区TIME_ZONE='+00:00' 以保证不同时区导出的TIMESTAMP 数据或数据被移动其他时区时的正确性。
75、-u, --user=name User for login if not current user.
指定连接的用户名
76、-v, --verbose Print info about the various stages.
输出多种平台信息
77、-V, --version Output version information and exit.
输出mysqldump版本信息并退出
78、-w, --where=name Dump only selected records. Quotes are mandatory.-----------------mysqldump -uroot -p --host=localhost --all-databases --where="user='root'"
只转储给定的WHERE条件选择的记录。请注意如果条件包含命令解释符专用空格或字符,一定要将条件引用起来
79、-X, --xml Dump a database as well formed XML.mysqldump -uroot -p --host=localhost --all-database --xml
导出XML格式
80、--plugin-dir=name Directory for client-side plugins.-------------------mysqldump -uroot -p --host=localhost --all-databases --plug_dir="/usr/local/lib/"
客户端插件的目录,用于兼容不同的插件版本
81、--default-auth=name Default authentication client-side plugin to use.---------------mysqldump -uroot -p --host=localhost --all-database --default-auth="/usr/local/lib/plugin/<PLUGIN>"
客户端插件默认使用
82、--enable-cleartext-pluginEnable/disable the clear text authentication plugin.
启用/禁用 明文的身份验证插件