注意事项1:mysqlimport 导入数据文件到对应表中,数据文件类型支持text,txt,csv,并且数据文件字段数量必须与表字段数量一致,数据文件名字必须和表名一致。
注意事项2:使用mysqlimport时需要开启mysql的local_inflie参数,默认时off。
开启方法1: 进入数据库执行 set global local_infile=1;
查看状态;
mysql> show global variables like 'local_infile';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
1 row in set (0.00 sec)
开启方法2:编辑数据库配置文件my.cnf,在mysqld下增加一行 local_infile=ON;
重启数据库生效;
systemctl restart mysqld
[root@localhost ~]# cat fa.txt
3,zhanglei
4,lisi
[root@localhost ~]# mysqlimport -uroot -p --local test fa.txt --fields-terminated-by=','
Enter password:
test.fa: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
[root@localhost ~]# mysql -uroot -p -e "select * from test.fa;"
Enter password:
+----+----------+
| id | name |
+----+----------+
| 3 | zhanglei |
| 4 | lisi |
+----+----------+
--local test fa.txt test是库名 fa.txt是文件名称
--fields-terminated-by=',' 分隔符,
在对excel文件进行转换为txt或者csv等格式时,编码格式应为UTF-8,分隔符为逗号;txt等文件分隔符就可以自己写了。