天天看點

MySQL INTO OUTFILE/INFILE匯出匯入資料

前幾天開發突然有這麼一個需求,想導一份200多G的mysql資料出來到另一臺機器上,而且時間有點趕,第一時間就想要使用Xtrabackup來全備與增備。但想到之前使用Xtrabackup來備份恢復的時候出現了各種坑,就問了下同僚有什麼好建議來快速匯出匯入資料,後來知道了可以使用select into outfile匯出表

資料

,就冒著嘗試一下的心裡去弄了一下,得到的結果是驚人的,個人感覺速度要比Xtrabackup快很多。

使用select into outfile匯出表資料:

(一個for迴圈定義自己需要操作的資料庫名稱,把資料匯入到/data/tmp目錄下)

for table in `echo oat_inventory_in oat_inventory_out oat_inventory_defective_out oat_reject oat_reject_line oat_goods oat_goods_related oat_order oat_order_line oat_purchase oat_purchase_line oat_invcheck oat_inventory_deal oat_stage_invent oat_inventory oat_receives oat_withdraw oat_deduct oat_order_provide_amount oat_order_coordinate oat_order_distribution oat_refund oat_entity_amount oat_stage_entityfund oat_entity_frozen_detail oat_entity oat_funds_detail`

do

echo $table

mysql -u root -pPassword dbname -e "select * into outfile '/data/tmp/$table.txt' fields terminated by ',' from $table;"

done

匯出表結構:

(因為上述隻是倒入資料,而表的結構則需要使用mysqldump方式去匯出)

/usr/local/mysql/bin/mysqldump -u root -pPassword -d dbname oat_inventory_in oat_inventory_out oat_inventory_defective_out oat_reject oat_reject_line oat_goods oat_goods_related oat_order oat_order_line oat_purchase oat_purchase_line oat_invcheck oat_inventory_deal oat_stage_invent oat_inventory oat_receives oat_withdraw oat_deduct oat_order_provide_amount oat_order_coordinate oat_order_distribution oat_refund oat_entity_amount oat_stage_entityfund oat_entity_frozen_detail oat_entity oat_funds_detail > struct.sql

將匯出的結構與資料檔案scp到目標主機上(建議資料scp之前先壓縮):

scp -P 22 /data/tmp/*.gz [email protected]:/data

在新的資料庫上面匯入表結構:

mysql -u root -pPassword dbname <  struct.sql

使用 load data infile 匯入資料:

for table in `echo oat_inventory_out oat_inventory_defective_out oat_reject oat_reject_line oat_goods oat_goods_related oat_order oat_order_line oat_purchase oat_purchase_line oat_invcheck oat_inventory_deal oat_stage_invent oat_inventory oat_receives oat_withdraw oat_deduct oat_order_provide_amount oat_order_coordinate oat_order_distribution oat_refund oat_entity_amount oat_stage_entityfund oat_entity_frozen_detail oat_entity oat_funds_detail`

mysql -u root -pPassword dbname -e "L

OA

D DATA INFILE '/home/tmp/$table.txt' INTO TABLE $table FIELDS TERMINATED BY ','"

本文永久更新連結位址:

https://www.linuxidc.com/Linux/2018-09/154128.htm

繼續閱讀