使用命令行工具mysqlimport导入数据

使用命令行工具mysqlimport导入数据

Usage:  mysqlimport  [OPTIONS]  database  textfile...

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

/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)

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: mysqlimportMySQL提供的一个命令行工具,可以将文本文件导入MySQL数据库中的表中。 使用mysqlimport导入txt文件到表中,需要先创建好目标表,然后在命令行中执行以下命令: mysqlimport -u 用户名 -p 密码 数据库名 表名 文件名 其中,用户名和密码是MySQL数据库的登录信息,数据库名和表名是要导入数据的目标表的名称,文件名是要导入的txt文件的路径和文件名。 执行完毕后,mysqlimport会将txt文件中的数据按照指定的格式导入到目标表中。 ### 回答2: mysqlimportMySQL数据库中一个用于将文本文件导入表的命令行工具。它可以方便地将文本数据导入MySQL数据库,提供了快速、高效和灵活的导入功能。 使用mysqlimport导入txt文件到表需要以下步骤: 首先,打开终端或命令提示符窗口,进入MySQL的安装目录下的bin文件夹。 然后,使用以下命令格式进行导入mysqlimport [选项] 数据库名 表名 文件名 其中,[选项]可以根据需要设置,常用的选项包括: -h 主机名:指定连接到的MySQL服务器的主机名 -u 用户名:指定连接到MySQL服务器所使用的用户名 -p 密码:指定连接到MySQL服务器所使用的密码 -L 日志文件:导入过程的日志将写入指定的文件中 -t 表名:指定导入数据的表名 -c 列名:指定导入数据的列名 -v 显示导入过程的详细信息 例如,我们想将一个名为"example.txt"的文件导入数据库"testdb"中的表"example_table"中,可以使用以下命令: mysqlimport -u root -p testdb example_table example.txt 执行以上命令后,mysqlimport会将example.txt文件中的数据按照指定的列名导入到example_table表中。 导入开始后,mysqlimport会显示导入进度和相关信息,导入完成后,会显示导入的行数和用时等信息。 通过mysqlimport工具,我们可以方便地将大量的文本数据快速导入MySQL数据库中的表中,提高了数据导入的效率和便捷性。 ### 回答3: mysqlimportMySQL数据库的一个命令行工具,用于将文本文件导入MySQL数据库中的表中。通过mysqlimport,可以方便地批量导入大量的数据,提高导入效率。 使用mysqlimport导入txt文件到表的步骤如下: 1. 首先确保已经创建目标表,包含与txt文件对应的列名和数据类型。 2. 打开命令提示符(或终端窗口),并进入MySQL的安装目录下的bin文件夹(例如C:\Program Files\MySQL\MySQL Server 5.7\bin)。 3. 输入以下命令格式来执行导入操作: mysqlimport -u 用户名 -p 密码 数据库名 表名 文件路径 其中,用户名是用于连接数据库的用户名,密码是对应的密码,数据库名是目标数据库的名称,表名是目标表的名称,文件路径是要导入的txt文件的路径。注意,-u参数和-p参数后面应直接接用户名和密码,没有空格。 4. 回车执行命令,导入过程开始进行。 5. 导入完成后,命令行会给出相应的提示信息,如成功导入的记录数量。 需要注意的是,导入的txt文件需要符合MySQL对于导入格式的要求,即每一行数据与表的列对应,并以特定的分隔符分隔各列数据(默认为制表符)。如果txt文件的数据与表的列不对应,或者数据格式有误,导入过程可能出现错误。 总之,使用mysqlimport命令可以方便地将txt文件中的数据批量导入MySQL数据库中的表中,提高导入效率和操作便利性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值