使用命令行工具mysqlimport导入数据
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)
使用命令行工具mysqlimport导入数据的更多相关文章
- Sqlite 命令行导出、导入数据(直接支持CSV)
打开命令行 导出数据到data.csv D:\project>sqlite3.exe old.db SQLite version 3.21.0 2017-10-24 18:55:49 Enter ...
- 七个用于数据科学(data science)的命令行工具
七个用于数据科学(data science)的命令行工具 数据科学是OSEMN(和 awesome 相同发音),它包括获取(Obtaining).整理(Scrubbing).探索(Exploring) ...
- mysql命令行工具
mysql包相关命令行工具 [root@manage ~]# rpm -qa|grep mysql mysql-server-5.1.73-5.el6_7.1.x86_64 mysql-5.1.73- ...
- MySQL命令行工具各功能说明(转)
MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...
- MySQL 命令行工具之 mysqldump 深入研究
mysqldump 是MySQL的一个命令行工具,用于逻辑备份.可以将数据库和表的结构,以及表中的数据分别导出成:create database, create table, insert into的 ...
- 转:windows下命令行工具
转自: http://www.cnblogs.com/haochuang/p/5593411.html Windows下CMD不好用,远没有Linux,或者一些SSH工具用起来方便.其实Windows ...
- [MySQL]命令行工具和基本操作
[MySQL]命令行工具和基本操作 一 MySQL命令行工具 (查看帮助 ---help,或 -?) 1)MySQL MySQL是一个简单的SQL外壳(有GNU readline功能).它支持交互式 ...
- 10款Windows命令行工具
Windows下CMD不好用,远没有Linux,或者一些SSH工具用起来方便.其实Windows下,也有一些不错的工具替代CMD: 0.powercmd经过比较,我最终选择了这款,这里补充一下截图:
- Windows下SVN命令行工具使用详解
根据我的记忆,似乎Windows 7下自自带一个svn命令行工具.如果你的机器没有,不必担心.你可以从http://subversion.tigris.org获 取subversion for win ...
随机推荐
- 把系统时间改到以前后,MyEclipse9.1的工程里的JS文件修改完保存但MyEclipse不会将其不会更新。
一个任务中,由于本周数据还没有产生,只好把系统时间修改到上周,利用下上周的数据. 修改内容主要是增加查询子句的一个字段,因此,包含SQL的DAO,前台显示的Table和前台操作的JS都需要相应修改,它 ...
- 免费的Bootstrap等待页面的应用模板
在线演示 本地下载 这是一款适合移动设备的网页模板,它页面干净小巧.有很多新元素在其中,可以自定义动画和特效.非常酷!
- Passing address of non-local object to __autoreleasing parameter for write-back
在希望通过函数的參数返回Objective-C对象的时候.遇到了这个问题 错误代码例如以下: - (void)methodA:(NSString **)string<span style=&qu ...
- C#.NET常见问题(FAQ)-如何清空stringbuilder
就红色的代码可以: System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("hello" ...
- DHTML【9】--Javascript
大家好,好长时间不见了,因为博主最近在驾校学习开车,所以耽误了DHTML的更新日程,对此实感愧疚. 好了,不再得瑟了,接下来该介绍DHTML中比较核心的一个东东—Javascript. 初看Javas ...
- Linux获得命令帮助(学习笔记五)
一.获得命令帮助 1.1.内部命令与外部命令 简单来说,在linux系统中有存储位置的命令为外部命令: 没有存储位置的为内部命令,可以理解为内部命令嵌入在linux的shell中,所以看不到. typ ...
- 分离链接散列表C语言实现实例
/* hash_sep.h */ #ifndef _HASH_SEP_H #define _HASH_SEP_H #define MIN_TABLE_SIZE 5 struct list_node; ...
- zookeeper技术浅析
Zookeeper是hadoop的一个子项目,尽管源自hadoop,可是我发现zookeeper脱离hadoop的范畴开发分布式框架的运用越来越多. 今天我想谈谈zookeeper.本文不谈如何使用z ...
- C++ union使用注意
union在我们敲代码的时候的使用概率远远小于struct.所以我们常常不太关心她.就知道他是使用内存复用技术.同一个时刻,他仅仅能存在一个成员的值. C中,我们在union中能够包括struct的, ...
- openerp用wizard导入excel数据
来自:http://blog.csdn.net/yumingbuzhongyao/article/details/18669183 作为一个quick note吧. OE里的csv导入数据功能形同摆设 ...