MySQL INTO OUTFILE/INFILE导出导入数据

栏目: 数据库 · 发布时间: 7年前

内容简介:前几天开发突然有这么一个需求,想导一份200多G的mysql数据出来到另一台机器上,而且时间有点赶,第一时间就想要使用Xtrabackup来全备与增备。但想到之前使用Xtrabackup来备份恢复的时候出现了各种坑,就问了下同事有什么好建议来快速导出导入数据,后来知道了可以使用select into outfile导出表数据,就冒着尝试一下的心里去弄了一下,得到的结果是惊人的,个人感觉速度要比Xtrabackup快很多。(一个for循环定义自己需要操作的数据库名称,把数据导入到/data/tmp目录下)f

前几天开发突然有这么一个需求,想导一份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 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

Linux公社的RSS地址: https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-09/154128.htm


以上所述就是小编给大家介绍的《MySQL INTO OUTFILE/INFILE导出导入数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术(第2卷)

计算机程序设计艺术(第2卷)

Donald E. Knuth / 苏运霖 / 国防工业出版社 / 2002-8 / 98.00元

本书是国内外业界广泛关注的7卷本《计算机程序设计艺术》第2卷的最新版。本卷对半数值算法领域做了全面介绍,分“随机数”和“算术”两章。本卷总结了主要算法范例及这些算法的基本理论,广泛剖析了计算机程序设计与数值分析间的相互联系,其中特别值得注意的是作者对随机数生成程序的重新处理和对形式幂级数计算的讨论。 本书附有大量习题和答案,标明了难易程度及数学概念的使用。 本书内容精辟,语言流畅,引人入胜,可供从......一起来看看 《计算机程序设计艺术(第2卷)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具