前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysqldump 命令参数详解

mysqldump 命令参数详解

作者头像
用户14527
修改2022-11-28 14:05:35
3.9K2
修改2022-11-28 14:05:35
举报
文章被收录于专栏:supremesupreme

mysqldump是mysql用于转存储数据库的实用程序。它主要产生一个SQL脚本,其中包含从头重新创建数据库所必需的命令CREATE TABLE INSERT等。对于导出的文件,可使用SOURCE命令导入数据库。

导出表结构:

如下为导出liangcb01库的products表的表结构。-d代表导出表结构。

代码语言:javascript
复制
mysqldump -h 172.16.32.xxx -P 3306 -u liangcb --skip-opt --single-transaction --flush-logs -q   -d liangcb01 products > table.sql  -p

导出表数据:

如下仅导出表数据,-c这样导出的数据行都带有列名字段,不带列名字段的 sql 会被 TDSQL for Percona、MariaDB 拒绝掉。

-t 参数的意义是不导出表结构,只导出表数据。

代码语言:javascript
复制
mysqldump -h 172.16.32.xxx -P 3306  -c -t -u liangcb  --skip-opt --single-transaction --flush-logs -q liangcb01 products > data.sql -p

导出所有数据库:

代码语言:javascript
复制
mysqldump -u root --skip-opt --single-transaction --flush-logs -q --all-databases > all.sql   -p

导出指定数据库:

代码语言:javascript
复制
mysqldump -uroot --skip-opt --single-transaction --flush-logs -q --databases db1 db2 >database.sql -p

添加–q(--quick) 参数时,select出来的结果将不会存放在缓存中,而是直接导出到标准输出中。如果不添加该参数,则会把select的结果放在本地缓存中,然后再输出给客户端。

导出表时默认会加锁,必须运行中的数据库进行备份请添加 --skip-opt选项

--single-transaction 一致性备份

代码语言:javascript
复制
    -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. 
   每个数据库创建之前添加drop数据库语句 
   
   --add-drop-table 
   Add a 'drop table' before each create. 
   每个数据表创建之前添加drop数据表语句。(默认为打开状态,使用--skip-add-drop-table取消选项) 
   
   --add-locks 
   Add locks around insert statements. 
   在每个表导出之前增加LOCK TABLES并且之后UNLOCK  TABLE。(默认为打开状态,使用--skip-add-locks取消选项) 
   
   --allow-keywords 
   Allow creation of column names that are keywords. 
   允许创建是关键词的列名字。 
   
   --character-sets-dir=name 
   Directory where character sets are. 
   字符集文件的目录 
   
   -i, --comments 
   Write additional information. 
   附加注释信息。默认为打开,可以用--skip-comments取消 
   
   --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等。要使用几个值,用逗号将它们隔开。它并不保证能完全兼容,而是尽量兼容。 
   
   --compact 
   Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks 
   导出更少的输出信息(用于调试)。去掉注释和头尾等结构。可以使用选项:--skip-add-drop-table  --skip-add-locks --skip-comments --skip-disable-keys 
   
   -c, --complete-insert 
   Use complete insert statements. 
   使用完整的insert语句(包含列名称)。这么做能提高插入效率,但是可能会受到max_allowed_packet参数的影响而导致插入失败。 
   
   -C, --compress 
   Use compression in server/client protocol. 
   在客户端和服务器之间启用压缩传递所有信息 
   
   --create-options 
   Include all MySQL specific create options. 
   在CREATE TABLE语句中包括所有MySQL特性选项。(默认为打开状态) 
   
   -B, --databases 
   To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output. 
   导出数据库。参数后面所有名字参量都被看作数据库名。 
   
   -#, --debug[=#] 
   This is a non-debug version. Catch this and exit 
   输出debug信息,用于调试。 
   
   --debug-check 
   Check memory and open file usage at exit. 
   检查内存和打开文件使用说明并退出。 
   
   --debug-info 
   Print some debug info at exit. 
   输出调试信息并退出 
   
   --default-character-set=name 
   Set the default character set. 
   设置默认字符集 
   
   --delayed-insert 
   Insert rows with INSERT DELAYED; 
   采用延时插入方式(INSERT DELAYED)导出数据 
   
   --delete-master-logs 
   Delete logs on master after backup. This automatically enables --master-data. 
   master备份后删除日志. 这个参数将自动激活--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. 
   对于每个表,用/*!40000 ALTER TABLE tbl_name DISABLE KEYS */;和/*!40000 ALTER TABLE tbl_name ENABLE KEYS */;语句引用INSERT语句。这样可以更快地导入dump出来的文件,因为它是在插入所有行后创建索引的。该选项只适合MyISAM表,默认为打开状态。 
   
   -E, --events 
   Dump events. 
   导出事件。 
   
   -e, --extended-insert 
   Allows utilization of the new, much faster INSERT syntax. 
   使用具有多个VALUES列的INSERT语法。这样使导出文件更小,并加速导入时的速度。默认为打开状态,使用--skip-extended-insert取消选项。 
   
   --fields-terminated-by=name 
   Fields in the textfile are terminated by ... 
   导出文件中忽略给定字段。与--tab选项一起使用,不能用于--databases和--all-databases选项 
   
   --fields-enclosed-by=name 
   Fields in the importfile are enclosed by ... 
   输出文件中的各个字段用给定字符包裹。与--tab选项一起使用,不能用于--databases和--all-databases选项 
   
   --fields-optionally-enclosed-by=name 
   Fields in the i.file are opt. enclosed by ... 
   输出文件中的各个字段用给定字符选择性包裹。与--tab选项一起使用,不能用于--databases和--all-databases选项 
   
   --fields-escaped-by=name 
   Fields in the i.file are escaped by ... 
   输出文件中的各个字段忽略给定字符。与--tab选项一起使用,不能用于--databases和--all-databases选项 
   
   -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. 
   在导出mysql数据库之后,发出一条FLUSH  PRIVILEGES 语句。为了正确恢复,该选项应该用于导出mysql数据库和依赖mysql数据库数据的任何时候。 

   -f, --force 
   Continue even if we get an sql-error. 
   在导出过程中忽略出现的SQL错误。 
   
   -?, --help 
   Display this help message and exit. 
   显示帮助信息并退出。 
   
   --hex-blob 
   Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format. 
   使用十六进制格式导出二进制字符串字段。如果有二进制数据就必须使用该选项。影响到的字段类型有BINARY、VARBINARY、BLOB。 
   
   -h, --host=name 
   Connect to host. 
   需要导出的主机信息 
   
   --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 
   不导出指定表。指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。例如:--ignore-table=database.table1 --ignore-table=database.table2 …… 
   
   --insert-ignore 
   Insert rows with INSERT IGNORE. 
   在插入行时使用INSERT IGNORE语句. 
   
   --lines-terminated-by=name 
   Lines in the i.file are terminated by ... 
   输出文件的每行用给定字符串划分。与--tab选项一起使用,不能用于--databases和--all-databases选项。 
   
   -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. 
   提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭--single-transaction 和--lock-tables 选项。 
   
   -l, --lock-tables 
   Lock all tables for read. 
   开始导出前,锁定所有表。用READ  LOCAL锁定表以允许MyISAM表并行插入。对于支持事务的表例如InnoDB和BDB,--single-transaction是一个更好的选择,因为它根本不需要锁定表。请注意当导出多个数据库时,--lock-tables分别为每个数据库锁定表。因此,该选项不能保证导出文件中的表在数据库之间的逻辑一致性。不同数据库表的导出状态可以完全不同。 
   
   --log-error=name 
   Append warnings and errors to given file. 
   附加警告和错误信息到给定文件 
   
   --no-autocommit 
   Wrap tables with autocommit/commit statements. 
   使用autocommit/commit 语句包裹表 
   
   -n, --no-create-db 
   'CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name;' will not be put in the output. The above line will be added otherwise, if --databases or --all-databases option was given.}. 
   只导出数据,而不添加CREATE DATABASE 语句 
   
   -t, --no-create-info 
   Don't write table creation info. 
   只导出数据,而不添加CREATE TABLE 语句 
   
   -d, --no-data 
   No row information. 
   不导出任何数据,只导出数据库表结构 
   
   --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. 
   等同于--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. 
   如果存在主键,或者第一个唯一键,对每个表的记录进行排序。在导出MyISAM表到InnoDB表时有效,但会使得导出工作花费很长时间。 
   
   -p, --password[=name] 
   Password to use when connecting to server. If password is not given it's solicited on the tty. 
   连接数据库密码 
   
   -W, --pipe 
   Use named pipes to connect to server. 
   使用命名管道连接mysql(windows系统可用) 
   
   -P, --port=# 
   Port number to use for connection. 
   连接数据库端口号 
   
   --protocol=name 
   The protocol of connection (tcp,socket,pipe,memory). 
   使用的连接协议,包括:tcp, socket, pipe, memory. 
   
   -q, --quick 
   Don't buffer query, dump directly to stdout. 
   不缓冲查询,直接导出到标准输出。默认为打开状态,使用--skip-quick取消该选项。 
   
   -Q, --quote-names 
   Quote table and column names with backticks (`). 
   使用(`)引起表和列名。默认为打开状态,使用--skip-quote-names取消该选项。 
   
   --replace 
   Use REPLACE INTO instead of INSERT INTO. 
   使用REPLACE INTO 取代INSERT INTO. 
   
   -r, --result-file=name 
   Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\n' from being converted to '\r\n' (carriage return + line feed). 
   直接输出到指定文件中。该选项应该用在使用回车换行对(\\r\\n)换行的系统上(例如:DOS,Windows)。该选项确保只有一行被使用。 
   
   -R, --routines 
   Dump stored routines (functions and procedures). 
   导出存储过程以及自定义函数。 
   
   --set-charset 
   Add 'SET NAMES default_character_set' to the output. Enabled by default; suppress with --skip-set-charset. 
   添加'SET NAMES  default_character_set'到输出文件。默认为打开状态,使用--skip-set-charset关闭选项。 
   
   -O, --set-variable=name 
   Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value. 
   设置变量的值 
   
   --dump-date 
   Put a dump date to the end of the output. 
   添加DUMP时间到输出末尾 
   
   --skip-opt 
   Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. 
   禁用–opt选项. 
   
   -S, --socket=name 
   Socket file to use for connection. 
   指定连接mysql的socket文件位置,默认路径/tmp/mysql.sock 
   
   -T, --tab=name 
   Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if mysqldump is run on the same machine as the mysqld daemon. 
   为每个表在给定路径创建tab分割的文本文件。注意:仅仅用于mysqldump和mysqld服务器运行在相同机器上。 
   
   --tables 
   Overrides option --databases (-B). 
   覆盖--databases (-B)参数,指定需要导出的表名。 
   
   --triggers 
   Dump triggers for each dumped table 
   导出触发器。该选项默认启用,用--skip-triggers禁用它。 
   
   --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. 
   在导出顶部设置时区TIME_ZONE='+00:00' ,以保证在不同时区导出的TIMESTAMP 数据或者数据被移动其他时区时的正确性。 
   
   -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. 
   输出mysqldump版本信息并退出 
  
   -w, --where=name 
   Dump only selected records; QUOTES mandatory! 
   只转储给定的WHERE条件选择的记录。请注意如果条件包含命令解释符专用空格或字符,一定要将条件引用起来。 
 
   -X, --xml                               
   Dump a database as well formed XML. 
   导出XML格式.
   
   
   

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mysqldump是mysql用于转存储数据库的实用程序。它主要产生一个SQL脚本,其中包含从头重新创建数据库所必需的命令CREATE TABLE INSERT等。对于导出的文件,可使用SOURCE命令导入数据库。
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档