MySQL命令之mysqldump的选项详解
文章目錄
- `--opt`
- `--skip-opt`
- `--add-drop-table`
- `--add-locks`
- `--skip-add-locks`
- `--allow-keywords`
- `--all-databases,-A`
- `--comments`
- `--skip-comments`
- `--compact`
- `--complete-insert,-c`
- `--compress,-C`
- `--events,-E`
- `--compatible`
- `--skip-disable-keys,-K`
- `--skip-add-drop-table`
- `--quick,-q`
- `--skip-quick`
- --debug-info
- `--add-drop-database`
- `--databases,-B`
- `--debug`
- `--default-character-set=charset`
- `--create-option,-a`
- `--delayed-insert`
- `--delete-master-logs`
- `--disable-keys,-K`
- `--extended-insert,-e`
- `--flush-logs,-F`
- `--flush-privileges`
- `--force,-f`
- `--host=host_name,-h host_name`
- `--hex-blob`
- `--lock-all-tables,-x`
- `--lock-tables,-l`
- `--log-error`
- `--max_allowed_packet`
- `--net_buffer_length`
- `--no-autocommit`
- `--master-data[=value]`
- `--dump-slave`
- `--ignore-table`
- `--no-create-db,-n`
- `--no-create-info,-t`
- `--order-by-primary`
- `--no-data,-d`
- `--password[=password],-p[password]`
- `--port=port_num,-P port_num`
- `--protocol={TCP | SOCKET | PIPE | MEMORY}`
- `--dump-date`
- `--result-file,-r`
- `--replace`
- `--quote-names,-Q`
- `--result-file=file,-r file`
- `--routines,-R`
- `--set-charset`
- `--single-transaction`
- `--all-tablespaces,-Y`
- `--no-tablespaces,-y`
- `--socket=path,-S path`
- `--tab=path,-T path`
- `--tables`
- `--triggers`
- `--tz-utc`
- `--user=user_name,-u user_name`
- `--verbose,-v`
- `--version,-V`
- `--where='where-condition',-w 'where-condition'`
- `--xml,-X`
- `--help,-?`
--opt
指定選項 --opt 等同于指定
--add-drop-table
--add-locks
--create-options
--disable-keys
--extended-insert
--lock-tables
--quick
--set-charset
如果沒有使用 --opt,mysqldump 就會把整個結果集裝載到內存中,然后導出。如果數據非常大就會導致導出失敗。
該選項默認開啟,但可以用 --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_name 和 USE 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,-?
查看使用說明
總結
以上是生活随笔為你收集整理的MySQL命令之mysqldump的选项详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑到底应该怎样装软件电脑如何装软件
- 下一篇: MySQL的用户表(user)