-
文件名:text.log,数据案例: xiaogang,12,beijing
xiaohong,16,nanjing
xiaolu,11,beijing
xiaohua,11,heilongjiang
xitong,18,henan
daqiang,16,anyang
dagang,4,zhejiang
longyang,3,jiangshu
longshao,2,xiamen
zhenguo,22,guangzhou
-
建表语句及数据库配置 use ai_proxy;
CREATE TABLE `user_info` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键日志id',
`name` varchar(16) NOT NULL DEFAULT '',
`age` varchar(16) NOT NULL DEFAULT '0',
`address` varchar(1280) NOT NULL DEFAULT '',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
PRIMARY KEY (`id`),
KEY `index_user` (`id`,`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
查看mysql配置,检查是否支持local_infile加载:
命令一 SHOW GLOBAL VARIABLES LIKE 'local_infile';
如果local_infile=OFF,执行一下命令:
命令二 set global local_infile = 1;
执行后请再次查看local_infile配置,确保local_infile=ON。
如果local_infile=OFF,执行load date的时候会报错“Loading local data is disabled; this must be enabled on both the client and server sides”
注:“命令二”在mysql重启后可能就会失效,建议在mysql服务启动时自动加载该配置,修改mysql配置文件 -
Shell脚本
ROOT_DIR="/Users/Downloads"
loadDataFile=$ROOT_DIR"/text.log"
loadCommand="load data local infile '$loadDataFile' into table ai_proxy.user_info fields terminated BY ',' lines terminated BY '\n' (name,age,address);"
mysql --local-infile=1 -uroot -proot -h localhost -P 3306 ai_proxy -e "$loadCommand"
exit 0
|