1. 程式人生 > 實用技巧 >【MySQL Database】資料遷移--mysqldump

【MySQL Database】資料遷移--mysqldump

[mysql@wallet01 ~]$ mysqldump --help

  -u, --user=name     指定連線資料庫伺服器使用的使用者
  -p, --password      指定連線資料庫伺服器使用的密碼 
  -P, --port=#        指定連線資料庫伺服器使用的埠
  -h, --host=name     指定連線資料庫伺服器的主機名
  -A, --all-databases 備份全部的資料庫
  -B, --databases     備份指定的資料庫
  -t, --no-create-info 僅備份表記錄                    
  -d, --no-data       僅備份表結構
  -w, --where=name    僅備份表中匹配條件的記錄
  -E, --events        備份資料庫的事件
  -R, --routines      備份資料庫的儲存過程與函式
  --triggers          備份表的觸發器
  -x, --lock-all-tables 鎖定全部資料庫中的全部表。

  -T, --tab=name	Create tab-separated textfile for each table to given path. (Create .sql and .txt files.)     
  --fields-terminated-by=name	Fields in the output file are terminated by the given string.
  --fields-enclosed-by=name	Fields in the output file are enclosed by the given character.
  --lines-terminated-by=name	Lines in the output file are terminated by the given string.

  --dump-slave[=#]    This causes the binary log position and filename of the
                      master to be appended to the dumped data output. Setting
                      the value to 1, will printit as a CHANGE MASTER command
                      in the dumped data output; if equal to 2, that command
                      will be prefixed with a comment symbol. This option will
                      turn --lock-all-tables on, unless --single-transaction is
                      specified too (in which case a global read lock is only
                      taken a short time at the beginning of the dump - don't
                      forget to read about --single-transaction below). In all
                      cases any action on logs will happen at the exact moment
                      of the dump.Option automatically turns --lock-tables off.

  --master-data[=#]   This causes the binary log position and filename to be
                      appended to the output. If equal to 1, will print it as a
                      CHANGE MASTER command; if equal to 2, that command will
                      be prefixed with a comment symbol. This option will turn
                      --lock-all-tables on, unless --single-transaction is
                      specified too (in which case a global read lock is only
                      taken a short time at the beginning of the dump; don't
                      forget to read about --single-transaction below). In all
                      cases, any action on logs will happen at the exact moment
                      of the dump. Option automatically turns --lock-tables
                      off.

  --single-transaction 
                      Creates a consistent snapshot by dumping all tables in a
                      single transaction. Works ONLY for tables stored in
                      storage engines which support multiversioning (currently
                      only InnoDB does); the dump is NOT guaranteed to be
                      consistent for other storage engines. While a
                      --single-transaction dump is in process, to ensure a
                      valid dump file (correct table contents and binary log
                      position), no other connection should use the following
                      statements: ALTER TABLE, DROP TABLE, RENAME TABLE,
                      TRUNCATE TABLE, as consistent snapshot is not isolated
                      from them. Option automatically turns off --lock-tables.

備份指定的庫
[mysql@wallet01 ~]$ mysqldump -uroot -p --databases soe > soe.sql 

備份指定的表
[mysql@wallet01 ~]$ mysqldump -uroot -p soe customer >customer.sql 

僅備份表結構
[mysql@wallet01 ~]$ mysqldump -uroot -p --no-data soe customer >customer.sql 

僅備份表記錄
[mysql@wallet01 ~]$ mysqldump -uroot -p --no-create-info soe customer >customer.sql 

僅備份表中匹配條件的記錄
[mysql@wallet01 ~]$ mysqldump -uroot -p --where="c_state='z3'" soe customer >customer.sql 

還原指定的庫
[mysql@wallet01 ~]$ mysql -uroot -p soe < soe.sql 

還原指定的表
[mysql@wallet01 ~]$ mysql -uroot -p soe < customer.sql 

表結構備份為sql檔案,表記錄備份為文字檔案
[mysql@wallet01 ~]$ mysqldump -uroot -p --tab=/var/lib/mysql-files \
--fields-terminated-by=',' --fields-enclosed-by='"' --lines-terminated-by='\n' soe customer 

[mysql@wallet01 ~]$ cd /var/lib/mysql-files
[mysql@wallet01 mysql-files]$ ls -lh
total 165M
-rw-rw-r-- 1 mysql mysql 2.5K Sep 18 10:08 customer.sql
-rw-rw-rw- 1 mysql mysql 165M Sep 18 10:08 customer.txt

還原表結構
[mysql@wallet01 ~]$ mysql -uroot -p soe < customer.sql 

還原表記錄
[mysql@wallet01 ~]$ mysqlimport -uroot -p soe \
--fields-terminated-by=',' \
--fields-enclosed-by='"' \
--lines-terminated-by='\n' /var/lib/mysql-files/customer.txt
Enter password:  
soe.customer: Records: 300000  Deleted: 0  Skipped: 0  Warnings: 0