嘗試OUTFIle、INFILE快速導入導出數據

嘗試OUTFIle、INFILE快速導入導出數據

應用場景:


前幾天開發忽然有這麼一個需求,想導一份200多G的mysql數據出來到另外一臺機器上,並且時間有點趕,第一時間就想要使用Xtrabackup來全備與增備。但想到以前使用Xtrabackup來備份恢復的時候出現了各類坑,就問了下同事有什麼好建議來快速導出導入數據,後來知道了能夠使用select into outfile導出表數據,就冒着嘗試一下的內心去弄了一下,獲得的結果是驚人的,我的感受速度要比Xtrabackup快不少。

使用select into outfile導出表數據:

(一個for循環定義本身須要操做的數據庫名稱,把數據導入到/data/tmp目錄下)mysql

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方式去導出)sql

/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 chenmingle@192.168.1.1:/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`
do
echo $table
mysql -u root -pPassword dbname -e "LOAD DATA INFILE '/home/tmp/$table.txt' INTO TABLE $table FIELDS TERMINATED BY ','"
done
相關文章
相關標籤/搜索