MySQL命令之mysqldump的选项详解

本文详细介绍了mysqldump工具的各种选项,包括--opt、--add-drop-table、--add-locks、--skip-add-locks等,涉及数据一致性、锁表、压缩、日志刷新等方面,帮助理解如何高效且安全地导出数据库内容。
摘要由CSDN通过智能技术生成

--opt

指定选项 --opt 等同于指定
--add-drop-table
--add-locks
--create-options
--disable-keys
--extended-insert
--lock-tables
--quick
--set-charset

如果没有使用 --optmysqldump 就会把整个结果集装载到内存中,然后导出。如果数据非常大就会导致导出失败。

该选项默认开启,但可以用 --skip-opt 禁用。

--skip-opt

--add-drop-table

在每个 CREATE TABLE 语句前添加 DROP TABLE 语句。

--add-locks

在每个表导出之前增加 LOCK TABLES 并且之后 UNLOCK TABLE。也就是会在 INSERT 语句中前后加上 LOCK TABLE 和 UNLOCK TABLE 语句。其作用是防止在这些记录被导入数据库时其他用户(其它线程)对表进行操作。默认开启,可以使用 --skip-add-locks 禁用

--skip-add-locks

取消在每个表导出之前增加 LOCK TABLES,默认存在锁。

--allow-keywords

允许创建关键字列名,在每个列名前面加上表名前缀。

--all-databases,-A

导出全部的数据库

mysqldump -uroot -pqpw123.com --all-databases

--comments

默认值为 1,转储文件中包含额外的信息,例如,服务器版本、主机信息等;设置为 0,则不会包含这些额外的信息。--comments=0 和选项 --skip-comments 等效。

--skip-comments

取消注释信息,默认存在注释信息

参见 --comments 选项的描述。

--compact

产生更紧凑的输出,并会启用以下选项:

--skip-add-drop-table
--no-set-names
--skip-disable-keys
--skip-comments
--skip-add-locks
--skip-set-charset

--complete-insert,-c

使用包括列名的完整的 INSERT 语句。这么做能提高插入效率,但是可能会受到 max_allowed_packet 参数的影响而导致插入失败。

--compress,-C

压缩在客户端和服务器之间发送的所有信息(如果二者均支持压缩)。

--events,-E

Include Event Scheduler events for the dumped databases in the output. This option requires the EVENT privileges for those databases.

The output generated by using --events contains CREATE EVENT statements to create the events. However, these statements do not include attributes such as the event creation and modification timestamps, so when the events are reloaded, they are created with timestamps equal to the reload time.

If you require events to be created with their original timestamp attributes, do not use --events. Instead, dump and reload the contents of the mysql.event table directly, using a MySQL account that has appropriate privileges for the mysql database.

--compatible

产生与其它数据库系统或旧的 MySQL 服务器更兼容的输出。值可以为 ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options。要使用几个值,用逗号将它们隔开。这些值与设置服务器 SQL 模式的相应选项有相同的含义。

它并不保证能完全兼容,而是尽量兼容。它只启用那些目前能够使转储输出更兼容的 SQL 模式值。例如,--compatible=oracle 不映射Oracle 类型或使用 Oracle 注释语法的数据类型。

--skip-disable-keys,-K

--skip-add-drop-table

取消每个数据表创建之前添加 drop 数据表语句。默认每个表之前存在 drop 语句。

--quick,-q

该选项用于转储大的表。这个选项使得 mysqldump 不会先读取全部的内容到内存中再执行输出,它强制 mysqldump 从服务器一次一行地检索表中的行并直接输出,而不是检索所有行并在输出前将它缓存到内存中。

--skip-quick

–debug-info

输出调试信息并退出

--add-drop-database

在每个 CREATE DATABASE 语句前添加 DROP DATABASE 语句,默认不存在。

--databases,-B

转储多个数据库。通常情况,mysqldump 将命令行中的第1个名字参数解析为数据库名,后面的名字解析为表名。使用该选项,它将所有名字参数解析为数据库名。

CREATE DATABASE IF NOT EXISTS db_nameUSE db_name 语句包含在每个新数据库之前的输出中。

--debug

输出 debug 信息,用于调试。默认值为:d:t:o,/tmp/mysqldump.trace

mysqldump -uroot -p --all-databases --debug
mysqldump -uroot -p --all-databases --debug="d:t,/tmp/debug.trace"

--default-character-set=charset

设置默认字符集,如果没有指定,mysqldump 使用 utf8

--create-option,-a

在 CREATE TABLE 语句中包括所有 MySQL 特性或者 MySQL 表选项。默认为打开状态。

特性包括:AUTO_INCREMENT、ENGINE=InnoDB、DEFAULT CHARSET=utf8 等。

--delayed-insert

采用延时插入方式(INSERT DELAYED)导出数据

mysqldump -uroot -p --all-databases --delayed-insert

--delete-master-logs

备份完成后删除主库上的日志。这个选项会自动打开 --master-data

mysqldump -uroot -p --all-databases --delete-master-logs

--disable-keys,-K

对于每个表,用 /*!40000 ALTER TABLE tbl_name DISABLE KEYS */;/*!40000 ALTER TABLE tbl_name ENABLE KEYS */; 语句引用 INSERT 语句。这样可以更快地导入 dump 出来的文件,因为它是在插入所有行后创建索引的。该选项只适合 MyISAM 表,默认为打开状态。

--extended-insert,-e

Write INSERT statements using multiple-row syntax that includes several VALUES lists. This results in a smaller dump file and speeds up inserts when the file is reloaded.

使用具有多个VALUES列的INSERT语法。这样使导出文件更小,并加速导入时的速度。默认为打开状态,使用 --skip-extended-insert 取消选项。

--flush-logs,-F

Flush the MySQL server log files before starting the dump. This option requires the RELOAD privilege. If you use this option in combination with the --all-databases option, the logs are flushed for each database dumped. The exception is when using --lock-all-tables, --master-data, or --single-transaction: In this case, the logs are flushed only once,corresponding to the moment that all tables are locked by FLUSH TABLES WITH READ LOCK. If you want your dump and the log flush to happen at exactly the same moment, you should use --flush-logs together with --lock-all-tables, --master-data, or --single-transaction.

在开始导出前刷新服务器的日志文件。我不知道为什么叫“刷新”,我也不知道“刷新”日志文件的意义何在?但是每次刷新操作都会生成一个新的二进制日志文件。而且新的文件内容与旧的文件内容不同。

注意,如果你一次性导出很多数据库(使用 --databases--all-databases 选项),导出每个库时都会触发日志刷新。但是当使用了 --lock-all-tables--master-data 时:日志只会被刷新一次,那个时候所有表都会被锁住。所以如果你希望你的导出和日志刷新发生在同一个确定的时刻,你需要使用 --lock-all-tables 或者 --master-data 配合 --flush-logs

--flush-privileges

在转储mysql数据库后,向转储输出添加一个FLUSH PRIVILEGES语句。每当转储包含mysql数据库和任何其他依赖mysql数据库中的数据进行正确恢复的数据库时,都应使用此选项。

--force,-f

在表转储过程中,即使出现 SQL 错误也继续。在导出过程中忽略出现的SQL错误。

--host=host_name,-h host_name

从给定主机的 MySQL 服务器转储数据。默认主机是 localhost

--hex-blob

使用十六进制格式导出二进制字符串字段(例如,‘abc’ 变为0x616263)。如果有二进制数据就必须使用该选项。影响到的字段类型有BINARY、VARBINARY、BLOB。

--lock-all-tables,-x

提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭 --single-transaction--lock-tables 选项。

--lock-tables,-l

开始转储前锁定所有表。用 READ LOCAL 锁定表以允

许并行插入 MyISAM 表。对于事务表例如 InnoDB 和 BDB,--single-transaction 是一个更好的选项,因为它根本不需要锁定表。

请注意当转储多个数据库时,--lock-tables 分别为每个数据库锁定表。因此,该选项不能保证转储文件中的表在数据库之间的逻辑一致性。不同数据库表的转储状态可以完全不同。

--log-error

附加警告和错误信息到给定文件

mysqldump -uroot -p --host=localhost --all-databases --log-error=/tmp/mysqldump_error_log.err

--max_allowed_packet

服务器发送和接受的最大包长度。

mysqldump -uroot -p --host=localhost --all-databases --max_allowed_packet=10240

--net_buffer_length

TCP/IP和socket连接的缓存大小

mysqldump -uroot -p --host=localhost --all-databases --net_buffer_length=1024

--no-autocommit

使用autocommit/commit 语句包裹表

--master-data[=value]

–master-data[=value] Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating after you load the dump file into the slave.

If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the statement is not written as a comment and takes effect when the dump file is reloaded. If no option value is specified, the default value is 1.

This option requires the RELOAD privilege and the binary log must be enabled.

The --master-data option automatically turns off --lock-tables. It also turns on --lock-all-tables, unless --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the beginning of the dump (see the description for --single-transaction). In all cases, any action on logs happens at the exact moment of the dump.

It is also possible to set up a slave by dumping an existing slave of the master, using the --dump-slave option, which overrides --master-data and causes it to be ignored if both options are used.

mysqldump 中使用 --master-data=2,会记录 binlog 文件和 position 的信息 。

--dump-slave

该选项将主的binlog位置和文件名追加到导出数据的文件中(show slave status)。设置为1时,将会以CHANGE MASTER命令输出到数据文件;设置为2时,会在change前加上注释。该选项将会打开–lock-all-tables,除非–single-transaction被指定。该选项会自动关闭–lock-tables选项。默认值为0

--ignore-table

不导出指定表。指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。例如:

--ignore-table=database.table1 --ignore-table=database.table2 ……

示例:

mysqldump -uroot -p --host=localhost --all-databases --ignore-table=mysql.user

--no-create-db,-n

取消创建数据库的 SQL 语句,默认存在该 SQL 语句。

该选项禁用 CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name 语句,如果给出 --databases--all-databases 选项,则包含到输出中。

--no-create-info,-t

不要输出创建每个转储表的 CREATE TABLE 语句,这个选项在您只需要数据而不需要DDL(数据库定义语句)时很方便。默认存在。

--order-by-primary

如果存在主键,或者第一个唯一键,对每个表的记录进行排序。在导出 MyISAM 表到 InnoDB 表时有效,但会使得导出工作花费很长时间。

--no-data,-d

Do not write any table row information (that is, do not dump table contents). This is useful if you want to dump only the CREATE TABLE statement for the table (for example,to create an empty copy of the table by loading the dump file).

不输出表的任何行信息,即不转储表的内容。这个选项使的 mysqldump 命令不写入 INSERT 语句。如果你只想转储表的结构,即您只需要 DDL 语句时,这个选项很有用。默认导出数据。

--password[=password],-p[password]

连接服务器时使用的密码。如果你使用短选项形式(-p),不能在选项和密码之间有一个空格。如果在命令行中,忽略了 --password-p 选项后面的密码值,将提示你输入一个。

--port=port_num,-P port_num

与一台主机连接时使用的 TCP/IP 端口号。

--protocol={TCP | SOCKET | PIPE | MEMORY}

使用的连接协议。

mysqldump -uroot -p --host=localhost --all-databases --protocol=tcp

--dump-date

将导出时间添加到输出文件中。默认为打开状态,使用–skip-dump-date关闭选项。

--result-file,-r

直接输出到指定文件中。该选项应该用在使用回车换行对(\r\n)换行的系统上(例如:DOS,Windows)。该选项确保只有一行被使用。

--replace

使用 REPLACE INTO 取代 INSERT INTO

--quote-names,-Q

Quote identifiers (such as database, table, and column names) within ` characters. If the ANSI_QUOTES SQL mode is enabled, identifiers are quoted within " characters.This option is enabled by default. It can be disabled with --skip-quote-names, but this option should be given after any option such as --compatible that may enable --quote-names.

用 (`)字符引用数据库、表和列名。如果 ANSI_QUOTES SQL 模式被激活,则使用(")作为引用符号。默认启用该选项。可以用“–skip-quote-names”禁用,但该选项应跟在其它选项后面才能生效。

--result-file=file,-r file

将输出转向给定的文件。该选项应用在Windows中,因为它禁止将新行‘\n’字符转换为‘\r\n’回车、返回/新行序列。

--routines,-R

Include stored routines (procedures and functions) for the dumped databases in the output. This option requires the SELECT privilege for the mysql.proc table.

注:
1.stored routines 译为存储程序,包括存储过程和函数。
2.该选项在 MySQL 5.1.2 中添加进来。在此之前,存储程序不转储。

--set-charset

Write SET NAMES default_character_set to the output. This option is enabled by default. To suppress the SET NAMES statement, use --skip-set-charset.

将SET NAMES default_character_set加到输出中。该选项默认启用。要想禁用SET NAMES语句,使用–skip-set-charset。

--single-transaction

This option sets the transaction isolation mode to REPEATABLE READ and sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time when START TRANSACTION was issued without blocking any applications.

When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For example, any MyISAM or MEMORY tables dumped while using this option may still change state.

While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log coordinates), no other connection should use the following statements:ALTER TABLE, CREATE TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.

The --single-transaction option and the --lock-tables option are mutually exclusive because LOCK TABLES causes any pending transactions to be committed implicitly.

To dump large tables, combine the --single-transaction option with the --quick option.

该选项在导出数据之前写入一个 START TRANSACTION SQL 语句,它仅对事务表(如 InnoDB)有用,因为它会在启动事务时转储数据库的一致状态,而不会阻塞任何应用程序。本选项和 --lock-tables 选项是互斥的,因为 LOCK TABLES 会使任何挂起的事务隐含提交。要想导出大表的话,应结合使用 --quick 选项。

通过将导出操作封装在一个事务内来使得导出的数据是一个一致性快照。只有当表使用支持 MVCC 的存储引擎(目前只有 InnoDB)时才可以工作;其他引擎不能保证导出是一致的。当导出开启了 --single-transaction 选项时,要确保导出文件有效(正确的表数据和二进制日志位置),就要保证没有其他连接会执行如下语句:ALTER TABLE、DROP TABLE、RENAME TABLE、TRUNCATE TABLE,这会导致一致性快照失效。这个选项开启后会自动关闭 --lock-tables

--single-transaction 会将隔离级别设置成 repeatable-commited

--all-tablespaces,-Y

导出全部表空间。

mysqldump -uroot -p --all-databases --all-tablespaces

--no-tablespaces,-y

不导出任何表空间信息。

--socket=path,-S path

指定连接 mysql 的 socket 文件位置,默认路径 /tmp/mysql.sock

当连接localhost(为默认主机)时使用的套接字文件

--tab=path,-T path

这个选项将会创建两个文件,一个文件包含 DDL 语句或者表创建语句,另一个文件包含数据。DDL 文件被命名为 tbl_name.sql,数据文件被命名为 tbl_name.txt。路径名是存放这两个文件的目录。目录必须已经存在,并且命令的使用者有该目录的相关权限。

注:tbl_name.txt 的内容相当于使用语句 select * from tbl_name into outfile 生成的数据

--tables

覆盖 --database-B 选项。选项后面的所有参数被看作表名。

--triggers

导出触发器。该选项默认启用,用 --skip-triggers 禁用它。

--tz-utc

在转储文件中加入 SET TIME_ZONE='+00:00' 以便 TIMESTAMP 列可以在具有不同时区的服务器之间转储和重载。(不使用该选项,TIMESTAMP 列在具有本地时区的源服务器和目的服务器之间转储和重载)。该选项也可以保护由于夏令时带来的更改。该选项默认启用。要想禁用它,使用 --skip-tz-utc。该选项在 MySQL 5.1.2 中加入。

--user=user_name,-u user_name

连接服务器时使用的 MySQL 用户名。

--verbose,-v

Print more information about what the program does.

打印有关程序执行的更多信息。

--version,-V

显示版本信息并退出。

--where='where-condition',-w 'where-condition'

Dump only rows selected by the given WHERE condition. Quotes around the condition are mandatory if it contains spaces or other characters that are special to your command interpreter.

只转储给定的WHERE条件选择的记录。如果条件包含空格或特殊的字符(对于命令解释器而言有特殊含义的字符),一定要将条件引用起来。

例如:

--where="user='jimf'"
-w 'userid>100'

--xml,-X

Write dump output as well-formed XML.

以 XML 格式输出

--help,-?

查看使用说明

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值