Usage: mysqlimport [OPTIONS] database textfile …

mysqlimport 程序是一个将以特定格式存放的文本数据(如通过“select * into OUTFILE from …”所生成的数据文件)导入到指定的MySQL Server 中的工具程序,比如将一个标准的csv 文件导入到某指定数据库的指定表中。mysqlimport 工具实际上也只是“load data infile”命令的一个包装实现。

默认从以下路径中文件读取默认参数

/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf

1、常用选项:

--fields-terminated-by=字符串:设置字符串为字段之间的分隔符,可以为单个或多个字符。默认值为制表符“\t”。

-L, –local:表示从客户端任意路径读取文件导入表中,未设置该选项时,默认只从datadir下同名数据库目录下读取文件导入

--ignore-lines=n:表示可以忽略前n行。

-l, –lock-tables:写入时锁定所有表

-p, –password[=name]:指定用户密码

-u, –user=name:指定登入MySQL用户名

-h, –host=name:指定远程连接的服务器

-c, –columns=name:往表里导入指定字段,如:–columns=’Name,Age,Gender’

-C, –compress:在客户端和服务器之间启用压缩传递所有信息

其它可用选项和默认参数设置可以使用mysqlimport -help查询

2、用法示例:

例1:基本用法

mysql> create table classes3 like classes;

Query OK, 0 rows affected (0.07 sec)

[root@www tmp]# mysqlimport -u root --localhellodb classes3.sql --fields-terminated-by="|"

hellodb.classes3: Records: 10 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from classes3;

+---------+----------------+----------+

| ClassID | Class | NumOfStu |

+---------+----------------+----------+

| 1 | Shaolin Pai | 10 |

| 2 | Emei Pai | 7 |

| 3 | QingCheng Pai | 11 |

| 4 | Wudang Pai | 12 |

| 5 | Riyue Shenjiao | 31 |

| 6 | Lianshan Pai | 27 |

| 7 | Ming Jiao | 27 |

| 8 | Xiaoyao Pai | 15 |

| 9 | HuaShan Pai | 32 |

| 10 | Fuwei Biaoju | 19 |

+---------+----------------+----------+

10 rows in set (0.00 sec)

例2:指定–local选项,可以从本机任意路径导入数据

mysql> create table classes2 likeclasses;

Query OK, 0 rows affected (0.14 sec):

[root@www tmp]# cp classes2.sql /tmp

[root@www tmp]# mysqlimport -u root --localhellodb /tmp/classes2.sql

hellodb.classes2: Records: 10 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from classes2;

+---------+----------------+----------+

| ClassID | Class | NumOfStu |

+---------+----------------+----------+

| 1 | Shaolin Pai | 10 |

| 2 | Emei Pai | 7 |

| 3 | QingCheng Pai | 11 |

| 4 | Wudang Pai | 12 |

| 5 | Riyue Shenjiao | 31 |

| 6 | Lianshan Pai | 27 |

| 7 | Ming Jiao | 27 |

| 8 | Xiaoyao Pai | 15 |

| 9 | HuaShan Pai | 32 |

| 10 | Fuwei Biaoju | 19 |

+---------+----------------+----------+

10 rows in set (0.00 sec)

例3:未指定–local选项,无法从my.cnf中定义的其它路径中往表里导入数据

mysql> delete from classes2;

Query OK, 10 rows affected (0.01 sec)

[root@www ~]# head /tmp/classes2.sql -n 3

1 ShaolinPai 10

2 EmeiPai 7

3 QingChengPai 11

[root@www ~]# mysqlimport -u root hellodb/tmp/classes2.sql

mysqlimport: Error: 29, File '/tmp/classes2.sql'not found (Errcode: 13), when using table: classes2

例4:未指定–local选项,默认只从mysql数据存放路径同名数据库目录下读取文件导入表中,必须指定绝对路径。

mysql> delete from students1;

Query OK, 27 rows affected (2.60 sec)

[root@www tmp]# sed 's/\t/\|/g'students.sql> students1.sql

[root@www tmp]# head -n 2 students1.sql

1|Shi Zhongyu|22|M|2|3

2|Shi Potian|22|M|1|7

[root@www tmp]# cd

[root@www ~]# mysqlimport -u mhauser-p888888 hellodb students1.sql --fields-terminated="|"

mysqlimport: Error: 13, Can't get stat of'/var/lib/mysql/hellodb/students1.sql' (Errcode: 2), when using table:students1

未设置–local选项时,默认只从mysql数据存放路径同名数据库目录下读取文件导入

[root@www ~]# mysqlimport -u mhauser-p888888 hellodb /var/lib/mysql/tmp/students1.sql--fields-terminated="|"

hellodb.students1: Records: 27 Deleted: 0 Skipped: 0 Warnings: 0

例5:数据库存放表目录下同名文件导入表中,只需指定文件名

mysql> delete from students1;

Query OK, 27 rows affected (0.47 sec)

[root@www ~]# cd /var/lib/mysql/hellodb/

[root@www hellodb]# cp ../tmp/students1.sql.

将数据移到hellodb目录下,成功导入数据

[root@www hellodb]# mysqlimport -u mhauser-p888888 hellodb students1.sql --fields-terminated="|"

hellodb.students1: Records: 27 Deleted: 0 Skipped: 0 Warnings: 0

--fields-terminated="|":指定字段分隔符

mysql> select * from students1 limit5,3;

+-------+-----------+-----+--------+---------+-----------+

| StuID | Name | Age | Gender | ClassID | TeacherID |

+-------+-----------+-----+--------+---------+-----------+

| 6 | Shi Qing | 46 | M | 5 | NULL |

| 7 | Xi Ren | 19 | F | 3 | NULL |

| 8 | Lin Daiyu | 17 | F | 7 | NULL |

+-------+-----------+-----+--------+---------+-----------+

3 rows in set (0.00 sec)

例6:忽略前5行数据导入表中

[root@www tmp]# mysqlimport -u root --localhellodb classes2.sql --ignore-lines=5

hellodb.classes2: Records: 5 Deleted: 0 Skipped: 0 Warnings: 0

--ignore-lines=n:指定忽略前n行

mysql> select * from classes2;

+---------+--------------+----------+

| ClassID | Class | NumOfStu |

+---------+--------------+----------+

| 6 | Lianshan Pai | 27 |

| 7 | Ming Jiao | 27|

| 8 | Xiaoyao Pai | 15 |

| 9 | HuaShan Pai | 32 |

| 10 | Fuwei Biaoju | 19 |

+---------+--------------+----------+

5 rows in set (0.00 sec)

例7:往非空表中导入数据

[root@www hellodb]# >students1.sql

[root@www hellodb]# vim students1.sql

[root@www hellodb]# mysqlimport -u mhauser-p888888 hellodb students1.sql --fields-terminated="|"

hellodb.students1: Records: 6 Deleted: 0 Skipped: 0 Warnings: 0

[root@www hellodb]# more students1.sql

|Meng Qi D|17|M|2|3

|SuoLong|22|M|1|7

|Xiang Kesi|43|M|2|16

|KaiDuo|52|M|4|4

|JoBa|12|M|3|1

|Nami|18|F|4|1

[root@www hellodb]#

mysql> select * from students1 limit27,6;

+-------+------------+-----+--------+---------+-----------+

| StuID | Name | Age | Gender | ClassID | TeacherID |

+-------+------------+-----+--------+---------+-----------+

| 28 | Meng Qi D | 17 | M | 2 | 3 |

| 29 | SuoLong | 22 | M | 1 | 7 |

| 30 | Xiang Kesi | 43 | M | 2 | 16 |

| 31 | KaiDuo | 52 | M | 4 | 4 |

| 32 | JoBa | 12 | M | 3 | 1 |

| 33 | Nami | 18 | F | 4 | 1 |

+-------+------------+-----+--------+---------+-----------+

6 rows in set (0.17 sec)

mysql> select count(*) from students1 ;

+----------+

| count(*) |

+----------+

| 33 |

+----------+

1 row in set (0.03 sec)

数据会追加在表后

例8、远程连接MySQL服务器导入特定字段

mysql> drop table students1;

Query OK, 0 rows affected (2.89 sec)

mysql> create table students1 likestudents;

Query OK, 0 rows affected (1.57 sec)

[root@test mysql]# more /tmp/students1.sql

Meng Qi D|17|M

SuoLong|22|M

Xiang Kesi|43|M

KaiDuo|52|M

JoBa|12|M|

Nami|18|F|

Luo Bing|25|F

Wu Suopu|20|M

[root@test mysql]# mysqlimport -h192.168.88.131 -u mhauser -p888888 hellodb --local --fields-terminated-by='|' '/tmp/students1.sql'--columns='Name,Age,Gender'

hellodb.students1: Records: 8 Deleted: 0 Skipped: 0 Warnings: 0

--columns=’Name,Age,Gender’:指定导入那些字段

-h 192.168.88.131:指定远程登录主机名

mysql> select * from students1;

+-------+------------+-----+--------+---------+-----------+

| StuID | Name | Age | Gender | ClassID | TeacherID |

+-------+------------+-----+--------+---------+-----------+

| 1 | Meng Qi D | 17 | M | NULL | NULL |

| 2 | SuoLong | 22 | M | NULL | NULL |

| 3 | Xiang Kesi | 43 | M | NULL | NULL |

| 4 | KaiDuo | 52 | M | NULL | NULL |

| 5 | JoBa | 12 | M | NULL | NULL |

| 6 | Nami | 18 | F | NULL | NULL |

| 7 | Luo Bing | 25 | F | NULL | NULL |

| 8 | Wu Suopu | 20 | M | NULL | NULL |

+-------+------------+-----+--------+---------+-----------+

8 rows in set (0.01 sec)

例9、远程连接MySQL服务器导入特定字段,采用压缩传递数据的形式

mysql> drop table students1;

Query OK, 0 rows affected (0.75 sec)

mysql> create table students1 likestudents;

Query OK, 0 rows affected (0.66 sec)

[root@test mysql]# mysqlimport -h192.168.88.131 -u mhauser -p888888 hellodb --local -C --fields-terminated-by='|' '/tmp/students1.sql'--columns='Name,ClassID,Gender'

hellodb.students1: Records: 8 Deleted: 0 Skipped: 0 Warnings: 0

-C:指定压缩方式传递数据

mysql> select * from students1;

+-------+------------+-----+--------+---------+-----------+

| StuID | Name | Age | Gender | ClassID | TeacherID |

+-------+------------+-----+--------+---------+-----------+

| 1 | Meng Qi D | 0 | M | 17 | NULL |

| 2 | SuoLong | 0 | M | 22 | NULL |

| 3 | Xiang Kesi | 0 | M | 43 | NULL |

| 4 | KaiDuo | 0 | M | 52 | NULL |

| 5 | JoBa | 0 | M | 12 | NULL |

| 6 | Nami | 0 | F | 18 | NULL |

| 7 | Luo Bing | 0 | F | 25 | NULL |

| 8 | Wu Suopu | 0 | M | 20 | NULL |

+-------+------------+-----+--------+---------+-----------+

8 rows in set (0.00 sec)

https://www.cnblogs.com/shamo89/tag/mysql/default.html

mysql导入工具 行提交_使用命令行工具mysqlimport导入数据相关推荐

  1. mysql命令行工具打开文件_使用命令行工具mysqlimport导入数据

    使用命令行工具mysqlimport导入数据 Usage: mysqlimport [OPTIONS] database textfile... 默认从以下路径中文件读取默认参数 /etc/mysql ...

  2. keil使用命令行编译_使用命令行编译C51源码并生成HEX文件的示例

    参考Keil\C51\HLP\C51.pdf以及A51.pdf文档,示例及说明如下: set PATH=%PATH%;G:\develop\keil\C51\BIN;G:\develop\keil\C ...

  3. php解析命令行参数选项,PHP 命令行参数解析工具类的示例代码

    PHP 命令行参数解析工具类的示例代码 /** * 命令行参数解析工具类 * @author guolinchao */ class CommandLine { // 临时记录短选项的选项值 priv ...

  4. php mysql 命令行模式_PHP 的命令行模式

    从版本 4.3.0 开始,PHP 提供了一种新类型的 SAPI(Server Application Programming Interface,服务端应用编程端口)支持,名为 CLI,意为 Comm ...

  5. linux 命令行 过滤,利用linux命令行工具进行文本行过滤

    在日常工作中会遇到需要筛选过滤要查看的日志等文本文件的任务,例如需要查看日志文件的最后几行等. 下面总结一下常见的对文本行过滤的方法. 常用命令行工具 接下来主要使用的是head,tail,sed和a ...

  6. 怎么进入命令行操作mysql数据库_MySQL数据库之如何用命令行进入mysql具体操作步骤...

    本文主要向大家介绍了MySQL数据库之如何用命令行进入mysql具体操作步骤 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 如何用命令行进入mysql?最近无意在论坛上看到有朋 ...

  7. 打开计算机管理窗口命令,Win7如何打开命令行窗口?打开命令行窗口的方法

    有时我们会用到命令行工具,这时就需要打开电脑的命令行窗口,也就是Dos工具,然后再这个命令行窗口中输入可执行的命令完成我们想要的操作,比如打开一个文件夹或者删除文件等.那么Win7如何打开命令行窗口呢 ...

  8. android 启动linux命令行模式,Android用命令行启动应用

    Android是基于Linux内核的操作系统,用Java写的应用程序被Android运行时虚拟机运行. 因为Android是基于Linux的,而Linux执行ELF格式的可执行文件,所以用C++编写的 ...

  9. EFI 分区/恢复分区不可删除?你需要使用命令行了(全命令行操作)

    Windows 系统在安装的时候,会自动为我们的磁盘划分一个恢复分区和一个 EFI 分区.如果后面不打算再用这些分区的时候,却发现无法删除. 本文将提供解决方法. 因为误操作会导致数据丢失,所以我将两 ...

最新文章

  1. MediaPlayer开发全解析
  2. Android上超级好用的前端调试方法(adb reverse)
  3. 详述白盒测试的逻辑覆盖的路径覆盖及其优缺点
  4. Linux Shell脚本入门教程系列之(十三)Shell分支语句case … esac教程
  5. 小孔成像实验探究的软件_小孔成像法观察日食
  6. Linux下SHELL的PS1变量简介
  7. solr4 mysql自动更新_solr7.4 定时增量更新数据-Go语言中文社区
  8. 隐藏桌面图标软件——CoverDesk for Mac免激活版
  9. 使用PDF Converter OCR for Mac如何区分扫描的PDF和普通文件?
  10. android10新特性 视频解码,Android万能视频播放器10-OpenGL ESMediaCodec解码数据t
  11. TI单芯片毫米波雷达代码走读(二十二)—— 距离维CA-CFAR检测与目标统计
  12. 88家上市公司区块链分布图:七成拓展落地应用 互联网公司善于底层基础
  13. sikuli和Robotframework集成若干问题(一):TypeError: exists(): 2nd arg can't be coerced to double
  14. 传说中WM手机工程测试命令
  15. 经典图书介绍:广义相对论--1972讲稿
  16. 转义字符表大全(转)
  17. 如何成为技术领域 KOL?阿里技术专家告诉你
  18. 卡梅隆大学计算机排名,美国卡梅隆大学怎么样
  19. 从概念的角度审视淘宝一淘商品搜索Online系统架构
  20. 微信小程序手写板电子签名实现

热门文章

  1. 发送邮件程序报错454 Authentication failed以及POP3和SMTP简介
  2. 【C语言】第二章 类型、运算符和表达式
  3. 关于ORA-01187: cannot read from file because it failed verification tests 的处理方法
  4. MyBatis 数据持久层
  5. mysql Error 1045(28000)
  6. DNN(DotNetNuke)注册用户终于突破10万人了,其3.0也终于跳票了...
  7. python 元组比较大小_为什么元组比列表更快?
  8. 控制台应用程序换换为窗体应用_Epic为开发者设计了一套iPhone使用的运动捕捉应用程序...
  9. MongoDB数据库的迁移
  10. Jquery Mobile dialog的生命周期