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
指定選項(xiàng) --opt 等同于指定
--add-drop-table
--add-locks
--create-options
--disable-keys
--extended-insert
--lock-tables
--quick
--set-charset
如果沒有使用 --opt,mysqldump 就會把整個結(jié)果集裝載到內(nèi)存中,然后導(dǎo)出。如果數(shù)據(jù)非常大就會導(dǎo)致導(dǎo)出失敗。
該選項(xiàng)默認(rèn)開啟,但可以用 --skip-opt 禁用。
--skip-opt
--add-drop-table
在每個 CREATE TABLE 語句前添加 DROP TABLE 語句。
--add-locks
在每個表導(dǎo)出之前增加 LOCK TABLES 并且之后 UNLOCK TABLE。也就是會在 INSERT 語句中前后加上 LOCK TABLE 和 UNLOCK TABLE 語句。其作用是防止在這些記錄被導(dǎo)入數(shù)據(jù)庫時其他用戶(其它線程)對表進(jìn)行操作。默認(rèn)開啟,可以使用 --skip-add-locks 禁用
--skip-add-locks
取消在每個表導(dǎo)出之前增加 LOCK TABLES,默認(rèn)存在鎖。
--allow-keywords
允許創(chuàng)建關(guān)鍵字列名,在每個列名前面加上表名前綴。
--all-databases,-A
導(dǎo)出全部的數(shù)據(jù)庫
mysqldump -uroot -pqpw123.com --all-databases--comments
默認(rèn)值為 1,轉(zhuǎn)儲文件中包含額外的信息,例如,服務(wù)器版本、主機(jī)信息等;設(shè)置為 0,則不會包含這些額外的信息。--comments=0 和選項(xiàng) --skip-comments 等效。
--skip-comments
取消注釋信息,默認(rèn)存在注釋信息
參見 --comments 選項(xiàng)的描述。
--compact
產(chǎn)生更緊湊的輸出,并會啟用以下選項(xiàng):
--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 參數(shù)的影響而導(dǎo)致插入失敗。
--compress,-C
壓縮在客戶端和服務(wù)器之間發(fā)送的所有信息(如果二者均支持壓縮)。
--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
產(chǎn)生與其它數(shù)據(jù)庫系統(tǒng)或舊的 MySQL 服務(wù)器更兼容的輸出。值可以為 ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options。要使用幾個值,用逗號將它們隔開。這些值與設(shè)置服務(wù)器 SQL 模式的相應(yīng)選項(xiàng)有相同的含義。
它并不保證能完全兼容,而是盡量兼容。它只啟用那些目前能夠使轉(zhuǎn)儲輸出更兼容的 SQL 模式值。例如,--compatible=oracle 不映射Oracle 類型或使用 Oracle 注釋語法的數(shù)據(jù)類型。
--skip-disable-keys,-K
--skip-add-drop-table
取消每個數(shù)據(jù)表創(chuàng)建之前添加 drop 數(shù)據(jù)表語句。默認(rèn)每個表之前存在 drop 語句。
--quick,-q
該選項(xiàng)用于轉(zhuǎn)儲大的表。這個選項(xiàng)使得 mysqldump 不會先讀取全部的內(nèi)容到內(nèi)存中再執(zhí)行輸出,它強(qiáng)制 mysqldump 從服務(wù)器一次一行地檢索表中的行并直接輸出,而不是檢索所有行并在輸出前將它緩存到內(nèi)存中。
--skip-quick
–debug-info
輸出調(diào)試信息并退出
--add-drop-database
在每個 CREATE DATABASE 語句前添加 DROP DATABASE 語句,默認(rèn)不存在。
--databases,-B
轉(zhuǎn)儲多個數(shù)據(jù)庫。通常情況,mysqldump 將命令行中的第1個名字參數(shù)解析為數(shù)據(jù)庫名,后面的名字解析為表名。使用該選項(xiàng),它將所有名字參數(shù)解析為數(shù)據(jù)庫名。
CREATE DATABASE IF NOT EXISTS db_name 和 USE db_name 語句包含在每個新數(shù)據(jù)庫之前的輸出中。
--debug
輸出 debug 信息,用于調(diào)試。默認(rèn)值為: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
設(shè)置默認(rèn)字符集,如果沒有指定,mysqldump 使用 utf8。
--create-option,-a
在 CREATE TABLE 語句中包括所有 MySQL 特性或者 MySQL 表選項(xiàng)。默認(rèn)為打開狀態(tài)。
特性包括:AUTO_INCREMENT、ENGINE=InnoDB、DEFAULT CHARSET=utf8 等。
--delayed-insert
采用延時插入方式(INSERT DELAYED)導(dǎo)出數(shù)據(jù)
mysqldump -uroot -p --all-databases --delayed-insert--delete-master-logs
備份完成后刪除主庫上的日志。這個選項(xiàng)會自動打開 --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 語句。這樣可以更快地導(dǎo)入 dump 出來的文件,因?yàn)樗窃诓迦胨行泻髣?chuàng)建索引的。該選項(xiàng)只適合 MyISAM 表,默認(rèn)為打開狀態(tài)。
--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語法。這樣使導(dǎo)出文件更小,并加速導(dǎo)入時的速度。默認(rèn)為打開狀態(tài),使用 --skip-extended-insert 取消選項(xiàng)。
--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.
在開始導(dǎo)出前刷新服務(wù)器的日志文件。我不知道為什么叫“刷新”,我也不知道“刷新”日志文件的意義何在?但是每次刷新操作都會生成一個新的二進(jìn)制日志文件。而且新的文件內(nèi)容與舊的文件內(nèi)容不同。
注意,如果你一次性導(dǎo)出很多數(shù)據(jù)庫(使用 --databases 或 --all-databases 選項(xiàng)),導(dǎo)出每個庫時都會觸發(fā)日志刷新。但是當(dāng)使用了 --lock-all-tables 或 --master-data 時:日志只會被刷新一次,那個時候所有表都會被鎖住。所以如果你希望你的導(dǎo)出和日志刷新發(fā)生在同一個確定的時刻,你需要使用 --lock-all-tables 或者 --master-data 配合 --flush-logs。
--flush-privileges
在轉(zhuǎn)儲mysql數(shù)據(jù)庫后,向轉(zhuǎn)儲輸出添加一個FLUSH PRIVILEGES語句。每當(dāng)轉(zhuǎn)儲包含mysql數(shù)據(jù)庫和任何其他依賴mysql數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行正確恢復(fù)的數(shù)據(jù)庫時,都應(yīng)使用此選項(xiàng)。
--force,-f
在表轉(zhuǎn)儲過程中,即使出現(xiàn) SQL 錯誤也繼續(xù)。在導(dǎo)出過程中忽略出現(xiàn)的SQL錯誤。
--host=host_name,-h host_name
從給定主機(jī)的 MySQL 服務(wù)器轉(zhuǎn)儲數(shù)據(jù)。默認(rèn)主機(jī)是 localhost。
--hex-blob
使用十六進(jìn)制格式導(dǎo)出二進(jìn)制字符串字段(例如,‘a(chǎn)bc’ 變?yōu)?x616263)。如果有二進(jìn)制數(shù)據(jù)就必須使用該選項(xiàng)。影響到的字段類型有BINARY、VARBINARY、BLOB。
--lock-all-tables,-x
提交請求鎖定所有數(shù)據(jù)庫中的所有表,以保證數(shù)據(jù)的一致性。這是一個全局讀鎖,并且自動關(guān)閉 --single-transaction 和 --lock-tables 選項(xiàng)。
--lock-tables,-l
開始轉(zhuǎn)儲前鎖定所有表。用 READ LOCAL 鎖定表以允
許并行插入 MyISAM 表。對于事務(wù)表例如 InnoDB 和 BDB,--single-transaction 是一個更好的選項(xiàng),因?yàn)樗静恍枰i定表。
請注意當(dāng)轉(zhuǎn)儲多個數(shù)據(jù)庫時,--lock-tables 分別為每個數(shù)據(jù)庫鎖定表。因此,該選項(xiàng)不能保證轉(zhuǎn)儲文件中的表在數(shù)據(jù)庫之間的邏輯一致性。不同數(shù)據(jù)庫表的轉(zhuǎn)儲狀態(tài)可以完全不同。
--log-error
附加警告和錯誤信息到給定文件
mysqldump -uroot -p --host=localhost --all-databases --log-error=/tmp/mysqldump_error_log.err--max_allowed_packet
服務(wù)器發(fā)送和接受的最大包長度。
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
該選項(xiàng)將主的binlog位置和文件名追加到導(dǎo)出數(shù)據(jù)的文件中(show slave status)。設(shè)置為1時,將會以CHANGE MASTER命令輸出到數(shù)據(jù)文件;設(shè)置為2時,會在change前加上注釋。該選項(xiàng)將會打開–lock-all-tables,除非–single-transaction被指定。該選項(xiàng)會自動關(guān)閉–lock-tables選項(xiàng)。默認(rèn)值為0
--ignore-table
不導(dǎo)出指定表。指定忽略多個表時,需要重復(fù)多次,每次一個表。每個表必須同時指定數(shù)據(jù)庫和表名。例如:
--ignore-table=database.table1 --ignore-table=database.table2 ……示例:
mysqldump -uroot -p --host=localhost --all-databases --ignore-table=mysql.user--no-create-db,-n
取消創(chuàng)建數(shù)據(jù)庫的 SQL 語句,默認(rèn)存在該 SQL 語句。
該選項(xiàng)禁用 CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name 語句,如果給出 --databases 或 --all-databases 選項(xiàng),則包含到輸出中。
--no-create-info,-t
不要輸出創(chuàng)建每個轉(zhuǎn)儲表的 CREATE TABLE 語句,這個選項(xiàng)在您只需要數(shù)據(jù)而不需要DDL(數(shù)據(jù)庫定義語句)時很方便。默認(rèn)存在。
--order-by-primary
如果存在主鍵,或者第一個唯一鍵,對每個表的記錄進(jìn)行排序。在導(dǎo)出 MyISAM 表到 InnoDB 表時有效,但會使得導(dǎo)出工作花費(fèi)很長時間。
--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).
不輸出表的任何行信息,即不轉(zhuǎn)儲表的內(nèi)容。這個選項(xiàng)使的 mysqldump 命令不寫入 INSERT 語句。如果你只想轉(zhuǎn)儲表的結(jié)構(gòu),即您只需要 DDL 語句時,這個選項(xiàng)很有用。默認(rèn)導(dǎo)出數(shù)據(jù)。
--password[=password],-p[password]
連接服務(wù)器時使用的密碼。如果你使用短選項(xiàng)形式(-p),不能在選項(xiàng)和密碼之間有一個空格。如果在命令行中,忽略了 --password 或 -p 選項(xiàng)后面的密碼值,將提示你輸入一個。
--port=port_num,-P port_num
與一臺主機(jī)連接時使用的 TCP/IP 端口號。
--protocol={TCP | SOCKET | PIPE | MEMORY}
使用的連接協(xié)議。
mysqldump -uroot -p --host=localhost --all-databases --protocol=tcp--dump-date
將導(dǎo)出時間添加到輸出文件中。默認(rèn)為打開狀態(tài),使用–skip-dump-date關(guān)閉選項(xiàng)。
--result-file,-r
直接輸出到指定文件中。該選項(xiàng)應(yīng)該用在使用回車換行對(\r\n)換行的系統(tǒng)上(例如:DOS,Windows)。該選項(xiàng)確保只有一行被使用。
--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.
用 (`)字符引用數(shù)據(jù)庫、表和列名。如果 ANSI_QUOTES SQL 模式被激活,則使用(")作為引用符號。默認(rèn)啟用該選項(xiàng)。可以用“–skip-quote-names”禁用,但該選項(xiàng)應(yīng)跟在其它選項(xiàng)后面才能生效。
--result-file=file,-r file
將輸出轉(zhuǎn)向給定的文件。該選項(xiàng)應(yīng)用在Windows中,因?yàn)樗箤⑿滦小甛n’字符轉(zhuǎ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 譯為存儲程序,包括存儲過程和函數(shù)。
2.該選項(xiàng)在 MySQL 5.1.2 中添加進(jìn)來。在此之前,存儲程序不轉(zhuǎn)儲。
--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加到輸出中。該選項(xiàng)默認(rèn)啟用。要想禁用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.
該選項(xiàng)在導(dǎo)出數(shù)據(jù)之前寫入一個 START TRANSACTION SQL 語句,它僅對事務(wù)表(如 InnoDB)有用,因?yàn)樗鼤趩邮聞?wù)時轉(zhuǎn)儲數(shù)據(jù)庫的一致狀態(tài),而不會阻塞任何應(yīng)用程序。本選項(xiàng)和 --lock-tables 選項(xiàng)是互斥的,因?yàn)?LOCK TABLES 會使任何掛起的事務(wù)隱含提交。要想導(dǎo)出大表的話,應(yīng)結(jié)合使用 --quick 選項(xiàng)。
通過將導(dǎo)出操作封裝在一個事務(wù)內(nèi)來使得導(dǎo)出的數(shù)據(jù)是一個一致性快照。只有當(dāng)表使用支持 MVCC 的存儲引擎(目前只有 InnoDB)時才可以工作;其他引擎不能保證導(dǎo)出是一致的。當(dāng)導(dǎo)出開啟了 --single-transaction 選項(xiàng)時,要確保導(dǎo)出文件有效(正確的表數(shù)據(jù)和二進(jìn)制日志位置),就要保證沒有其他連接會執(zhí)行如下語句:ALTER TABLE、DROP TABLE、RENAME TABLE、TRUNCATE TABLE,這會導(dǎo)致一致性快照失效。這個選項(xiàng)開啟后會自動關(guān)閉 --lock-tables。
--single-transaction 會將隔離級別設(shè)置成 repeatable-commited
--all-tablespaces,-Y
導(dǎo)出全部表空間。
mysqldump -uroot -p --all-databases --all-tablespaces--no-tablespaces,-y
不導(dǎo)出任何表空間信息。
--socket=path,-S path
指定連接 mysql 的 socket 文件位置,默認(rèn)路徑 /tmp/mysql.sock
當(dāng)連接localhost(為默認(rèn)主機(jī))時使用的套接字文件
--tab=path,-T path
這個選項(xiàng)將會創(chuàng)建兩個文件,一個文件包含 DDL 語句或者表創(chuàng)建語句,另一個文件包含數(shù)據(jù)。DDL 文件被命名為 tbl_name.sql,數(shù)據(jù)文件被命名為 tbl_name.txt。路徑名是存放這兩個文件的目錄。目錄必須已經(jīng)存在,并且命令的使用者有該目錄的相關(guān)權(quán)限。
注:tbl_name.txt 的內(nèi)容相當(dāng)于使用語句 select * from tbl_name into outfile 生成的數(shù)據(jù)
--tables
覆蓋 --database 或 -B 選項(xiàng)。選項(xiàng)后面的所有參數(shù)被看作表名。
--triggers
導(dǎo)出觸發(fā)器。該選項(xiàng)默認(rèn)啟用,用 --skip-triggers 禁用它。
--tz-utc
在轉(zhuǎn)儲文件中加入 SET TIME_ZONE='+00:00' 以便 TIMESTAMP 列可以在具有不同時區(qū)的服務(wù)器之間轉(zhuǎn)儲和重載。(不使用該選項(xiàng),TIMESTAMP 列在具有本地時區(qū)的源服務(wù)器和目的服務(wù)器之間轉(zhuǎn)儲和重載)。該選項(xiàng)也可以保護(hù)由于夏令時帶來的更改。該選項(xiàng)默認(rèn)啟用。要想禁用它,使用 --skip-tz-utc。該選項(xiàng)在 MySQL 5.1.2 中加入。
--user=user_name,-u user_name
連接服務(wù)器時使用的 MySQL 用戶名。
--verbose,-v
Print more information about what the program does.
打印有關(guān)程序執(zhí)行的更多信息。
--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.
只轉(zhuǎn)儲給定的WHERE條件選擇的記錄。如果條件包含空格或特殊的字符(對于命令解釋器而言有特殊含義的字符),一定要將條件引用起來。
例如:
--where="user='jimf'" -w 'userid>100'--xml,-X
Write dump output as well-formed XML.
以 XML 格式輸出
--help,-?
查看使用說明
總結(jié)
以上是生活随笔為你收集整理的MySQL命令之mysqldump的选项详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑到底应该怎样装软件电脑如何装软件
- 下一篇: 微软宣布明年 8 月 31 日停止支持