LOAD DATA INFILE & mysqlimport
Posted 青空如璃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LOAD DATA INFILE & mysqlimport相关的知识,希望对你有一定的参考价值。
+++++++++++++++++++++++++++++++++++++++++++++mysqlimport+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
其实如果用mysqlimport是mysql数据库提供的一个命令行程序,从本质来说j load data infile的命令接口,而且大多数的选项都和load data infile语法相同。
shell>mysqlimport [option] db_name testfile1 [testfile2 ...]
select into outfile 注意保持时以表名加.txt,以便恢复。
和load data infile不同的是,mysqlimport可以用来导入多张表,并且通过--user-thread参数并发地导入不同的文件,这里的并发指并发导入多个文件,而不是指mysqlimport可以并发地导入一个文件。
[[email protected] andyxi3306]# mv 1.txt sbtest2.txt
[[email protected] andyxi3306]# mv 2.txt sbtest1.txt
[[email protected] andyxi3306]# mysqlimport -h127.0.0.1 -uroot -p --use-threads=2 andyxi3306 /tmp/andyxi3306/sbtest1.txt /tmp/andyxi3306/sbtest2.txt
Enter password:
andyxi3306.sbtest1: Records: 500000 Deleted: 0 Skipped: 0 Warnings: 0
andyxi3306.sbtest2: Records: 500000 Deleted: 0 Skipped: 0 Warnings: 0
*************************** 1. row ***************************
Id: 29
User: root
Host: localhost:38566
db: andyxi3306
Command: Sleep
Time: 130
State:
Info: NULL
*************************** 2. row ***************************
Id: 37
User: root
Host: localhost:38582
db: NULL
Command: Query
Time: 0
State: starting
Info: show processlist
*************************** 3. row ***************************
Id: 41
User: root
Host: localhost:38590
db: andyxi3306
Command: Query
Time: 15
State: executing
Info: LOAD DATA INFILE ‘/tmp/andyxi3306/sbtest2.txt‘ INTO TABLE `sbtest2` IGNORE 0 LINES
*************************** 4. row ***************************
Id: 42
User: root
Host: localhost:38592
db: andyxi3306
Command: Query
Time: 15
State: executing
Info: LOAD DATA INFILE ‘/tmp/andyxi3306/sbtest1.txt‘ INTO TABLE `sbtest1` IGNORE 0 LINES
4 rows in set (0.00 sec)
mysqlimport恢复时其实用的还是load data infile
++++++++++++++++++++++++++++++++++++++++load data infile+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
如果通过mysqldump加参数--tab=path,或者通过select ... into outfile导出的数据需要恢复,此时可以通过load data infile来进行导入。
语法:
[email protected]:3306 [andyxi3306]>help load data
Name: ‘LOAD DATA‘
Description:
Syntax:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE ‘file_name‘
[REPLACE | IGNORE]
INTO TABLE tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY ‘string‘]
[[OPTIONALLY] ENCLOSED BY ‘char‘]
[ESCAPED BY ‘char‘]
]
[LINES
[STARTING BY ‘string‘]
[TERMINATED BY ‘string‘]
]
[IGNORE number {LINES | ROWS}]
[(col_name_or_user_var
[, col_name_or_user_var] ...)]
[SET col_name={expr | DEFAULT},
[, col_name={expr | DEFAULT}] ...]
要对服务器文件使用load data infile,必须有file权。其中对于导入格式的选项和select into outfile命令完全一样。
ignore number lines选项可以忽略导入的前几行。
测试:
[email protected]:3306 [andyxi3306]>select * into outfile ‘/tmp/andyxi3306/2.txt‘ from sbtest1;
Query OK, 500000 rows affected (2.66 sec)
[email protected]:3306 [andyxi3306]>delete from sbtest1;
Query OK,
500000 rows affected (37.19 sec)
[email protected]:3306 [andyxi3306]>select * from sbtest1;
ERROR 2013 (HY000): Lost connection to MySQL server during query
[email protected]:3306 [andyxi3306]>select * from sbtest1;
ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 29 Current database: andyxi3306
Empty set (0.21 sec)
[email protected]:3306 [andyxi3306]>load data infile ‘/tmp/andyxi3306/2.txt‘ into table sbtest1;
Query OK, 500000 rows affected (43.99 sec) Records: 500000 Deleted: 0 Skipped: 0 Warnings: 0
[email protected]:3306 [andyxi3306]>select * from sbtest1 limit 2G;
*************************** 1. row ***************************
id: 1 k: 252272
c: 76529735171-62619715576-43660157506-14122022461-23499333486-12530790621-96252745722-53101978020-30217711642-99182948190 pad: 78049175839-40449686376-46985343292-10855530210-03527816365
*************************** 2. row ***************************
id: 2 k: 251402
c: 35080843840-11823110763-17669742564-34386700636-31442953158-68823209002-35432290765-12996120436-32406040794-96855478492 pad: 47097503610-43096903937-36148507150-61897060196-12382094341 2 rows in set (0.00 sec)
数据恢复成功
以上是关于LOAD DATA INFILE & mysqlimport的主要内容,如果未能解决你的问题,请参考以下文章
LOAD DATA LOCAL INFILE 在 RDS 中不起作用
Mysql load data infile 导入数据出现:Data truncated for column