mysqldump DBA必知必会
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:
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.
-
-defaults-file=
-
-defaults-extra-file=
-
A, --all-databases 导出全部数据库
-
Y, --all-tablespaces
导出全部表空间.
-
y, --no-tablespaces
Do not dump any tablespace information.
-
-add-drop-database 表示每个数据库创建(create)之前添加drop数据库语句
-
-add-drop-table 表示每个数据表创建(create)之前添加drop数据库语句
默认是打开的,使用--skip-add-drop-table取消选项
-
-add-locks Add locks <font color=red>around INSERT statements</font>.
在每个表<font color=red>执行insert语句</font>之前增加LOCK TABLES并且之后UNLOCK TABLE
默认为打开状态,使用--skip-add-locks取消选项
-
-allow-keywords 表示允许创建是关键词的列名字。这由表名前缀于每个列名做到。
-
-apply-slave-statements
在'CHANGE MASTER'前添加'STOP SLAVE',并且在导出的最后添加'START SLAVE'。
-
-character-sets-dir=name
表示字符集文件的目录
-
i, --comments 参数表示附加注释信息。
默认为打开,可以用--skip-comments取消
-
-compatible=name 参数表示导出的数据将和其它数据库或旧版本的MySQL 相兼容。
-
-compact 参数表示导出更少的输出信息(用于调试)。
去掉注释和头尾等结构。
可以使用选项:
-
-skip-add-drop-table
-
-skip-add-locks
-
-skip-comments
-
-skip-disable-keys
-
c, --complete-insert
参数表示使用完整的insert语句(包含列名称)。
-
C, --compress 参数表示在客户端和服务器之间启用压缩传递所有信息
-
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-check Check memory and open file usage at exit.
-
-debug-info Print some debug info at exit.
-
-default-character-set=name
参数表示设置默认字符集,默认值为utf8
-
-delayed-insert Insert rows with INSERT DELAYED.
-
-delete-master-logs
参数表示master备份后删除日志。
这个参数将自动激活--master-data
-
K, --disable-keys 对于每个表,用 /*!40000 ALTER TABLE tbl_name DISABLE KEYS */;
和 /*!40000 ALTER TABLE tbl_name ENABLE KEYS */; 语句引用INSERT语句。
这样可以更快地导入dump出来的文件,因为它是在插入所有行后创建索引的。
该选项只适合MyISAM表,默认为打开状态。
-
-dump-slave[=
设置为1时,将会以CHANGE MASTER命令输出到数据文件;
设置为2时,在命令前增加说明信息。
该选项将会打开--lock-all-tables,除非--single-transaction被指定。
该选项会自动关闭--lock-tables选项。默认值为0。
-
E, --events 表示导出事件。
-
e, --extended-insert
表示使用具有多个VALUES列的INSERT语法。
这样使导出文件更小,并加速导入时的速度。
默认为打开状态,使用--skip-extended-insert取消选项。
-
-fields-terminated-by=name
参数表示导出文件中忽略给定字段。
与--tab选项一起使用,不能用于--databases和--all-databases选项
-
-fields-enclosed-by=name
输出文件中的各个字段用给定字符包裹。
与--tab选项一起使用,不能用于--databases和--all-databases选项
-
-fields-optionally-enclosed-by=name
参数表示输出文件中的各个字段用给定字符选择性包裹。
与--tab选项一起使用,不能用于--databases和--all-databases选项
-
-fields-escaped-by=name
表示输出文件中的各个字段忽略给定字符。
与--tab选项一起使用,不能用于--databases和--all-databases选项
-
F, --flush-logs 表示开始导出之前刷新日志。
请注意:假如一次导出多个数据库(使用选项--databases=或者--all-databases),
将会逐个数据库刷新日志。
除使用--lock-all-tables或者--master-data外:
在这种情况下,日志将会被刷新一次,相应的所以表同时被锁定。
因此,如果打算同时导出和刷新日志应该使用
-
-lock-all-tables 或者--master-data 和 --flush-logs
-
-flush-privileges 表示在导出mysql数据库之后,发出一条FLUSH PRIVILEGES 语句。
-
f, --force 参数表示在导出过程中忽略出现的SQL错误
-
?, --help Display this help message and exit.
-
-hex-blob 表示使用十六进制格式导出二进制字符串字段。
如果有二进制数据就必须使用该选项。影响到的字段类型有BINARY、VARBINARY、BLOB。
-
h, --host=name Connect to host.
-
-ignore-table=name 参数表示不导出指定表。
指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。
例如:--ignore-table=database.table1 --ignore-table=database.table2 ……
-
-include-master-host-port
在--dump-slave产生的'CHANGE MASTER TO..'语句中增加'MASTER_HOST=<host>,MASTER_PORT=<port>'
-
-insert-ignore 表示在插入行时使用INSERT IGNORE语句.
-
-lines-terminated-by=name
参数表示输出文件的每行用给定字符串划分。
与--tab选项一起使用,不能用于--databases和--all-databases选项。
-
x, --lock-all-tables
提交请求锁定所有数据库中的所有表,以保证数据的一致性。(慎用)
-
l, --lock-tables 表示开始导出前,锁定所有表。表示给所有表加读锁
用READ LOCAL锁定表以允许MyISAM表并行插入。
对于支持事务的表例如InnoDB和BDB,
-
-single-transaction是一个更好的选择,因为它根本不需要锁定表。
请注意当导出多个数据库时,--lock-tables分别为每个数据库锁定表。
因此,该选项不能保证导出文件中的表在数据库之间的逻辑一致性。
不同数据库表的导出状态可以完全不同
-
-log-error=name Append warnings and errors to given file.
-
-master-data[=
如果为1,将会输出CHANGE MASTER 命令;
如果为2,输出的CHANGE MASTER命令前添加注释信息。
该选项将打开--lock-all-tables 选项,除非--single-transaction也被指定
在这种情况下,全局读锁在开始导出时获得很短的时间;
其他内容参考下面的--single-transaction选项。
该选项自动关闭--lock-tables选项。
-
-max-allowed-packet=
表示服务器发送和接受的最大包长度。
-
-net-buffer-length=
表示TCP/IP和socket连接的缓存大小。
-
-no-autocommit Wrap tables with autocommit/commit statements.
-
n, --no-create-db 表示只导出数据,而不添加CREATE DATABASE ... IF EXISTS 语句。
-
t, --no-create-info
参数表示只导出数据,而不添加CREATE TABLE 语句。
-
d, --no-data 参数表示不导出任何数据,只导出数据库表结构。
-
N, --no-set-names Same as --skip-set-charset.
-
-opt 等同于--add-drop-table, --add-locks, --create-options, --quick,
-
-extended-insert, --lock-tables, --set-charset, --disable-keys
该选项默认开启, 可以用--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=
-
-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 参数表示使用REPLACE INTO 取代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 参数表示导出存储过程以及自定义函数。
-
-set-charset Add 'SET NAMES default_character_set' to the output.
(Defaults to on; use --skip-set-charset to disable.)
-
-single-transaction
该选项在导出数据之前提交一个BEGIN SQL语句,
BEGIN 不会阻塞任何应用程序且能保证导出时数据库的一致性状态。
它只适用于多版本存储引擎,仅InnoDB。
本选项和--lock-tables 选项是互斥的,因为LOCK TABLES 会使任何挂起的事务隐含提交。
要想导出大表的话,应结合使用--quick 选项。
-
-dump-date 将导出时间添加到输出文件中。
(Defaults to on; use --skip-dump-date to disable.)
-
-skip-opt 表示禁用–opt选项.
-
S, --socket=name The socket file to use for connection.
-
-ssl Enable SSL for connection (automatically enabled with
other flags).
-
-ssl-ca=name CA file in PEM format (check OpenSSL docs, implies
-
-ssl).
-
-ssl-capath=name CA directory (check OpenSSL docs, implies --ssl).
-
-ssl-cert=name X509 cert in PEM format (implies --ssl).
-
-ssl-cipher=name SSL cipher to use (implies --ssl).
-
-ssl-key=name X509 key in PEM format (implies --ssl).
-
-ssl-verify-server-cert
Verify server's "Common Name" in its cert against
hostname used when connecting. This option is disabled by
default.
-
T, --tab=name 参数表示为每个表在给定路径创建tab分割的文本文件。
注意:仅仅用于mysqldump和mysqld服务器运行在相同机器上。
-
-tables 表示覆盖--databases (-B)参数,指定需要导出的表名
-
-triggers 表示导出触发器。
该选项默认启用,用--skip-triggers禁用它。
-
-tz-utc 在导出顶部设置时区TIME_ZONE='+00:00' ,
以保证在不同时区导出的TIMESTAMP 数据或者数据被移动其他时区时的正确性。
(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 表示导出XML格式.
-
-plugin-dir=name 表示客户端插件的目录,用于兼容不同的插件版本。
-
-default-auth=name 表示客户端插件默认使用权限。
重点选项:
1.连接选项
-
u,--user
-
p,--password
-
h,--host
-
P,--port
2.输出内容选项
-
-add-drop-database 每个数据语句创建之前,添加drop database语句
-
-add-drop-table 每个表创建之前,添加 drop table语句
重要:
-
n,--no-create-db 不包含建立数据库的语句
-
t,--no-create-info 不包含建表语句
-
d,--no-data 不包含数据
3.输出格式选项
-
-compact 没有注解信息更加简化
-
c,--compact-insert 包含values字段
-
T,建表语句和插入数据分成两个文件
4.字符集选项
-
-default-charactaer-set=
5.其他重要选项
-
-flush-logs 备份前刷新日志
-
l,--lock-tables 给所有表加读锁
-
-tz-utc 时区