第一种方法(适合数据量较小的场景,几百、几千条可以用这种方法)
1.cmd进入控制台,输入指令连接到本地数据库
mysql -uroot -p数据库密码
2.选择你要将导入到哪个库,use 数据库名;
3.输入“source sql文件的路径”(注意你的文件路径要是复制来的,要将"\"全部换成“/”),示例
source C:/Users/14544/Desktop/sql/tb_sku1.sql
第二种方法(数据量如果是百万级别,方法一可能需要个把小时,用Navicat等工具导数据,可能内存溢出,那么我们就要改用方法二了)
基本介绍:load data infile语句从一个文本文件中以很高的速度读入一个表中。使用这个命令之前,mysqld进程(服务)必须已经在运行。
注意:
set global local_infile = 1;
mysql --local-infile -u root -p
语法结构
load data [low_priority] [local] infile 'file_name txt' [replace | ignore]
into table tbl_name
[fields
[terminated by't']
[OPTIONALLY] enclosed by '']
[escaped by'\' ]]
[lines terminated by'n']
[ignore number lines]
[(col_name, )]
说明:
○ terminated by 分隔符:意思是以什么字符作为分隔符
○ enclosed by 字段括起字符
○ escaped by 转义字符
○ terminated by 描述字段的分隔符,默认情况下是tab字符(\t)
○ enclosed by 描述的是字段的括起字符。
○ escaped by 描述的转义字符。默认的是反斜杠(backslash:\ )
○ lines 关键字指定了每条记录的分隔符默认为’\n’即为换行符
举例子
1.文件数据(只用少量数据演示,实际上楼主的文件有两百万条数据)
1,100000003145001,华为Meta1,87901,9961,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t5590/64/5811657380/234462/5398e856/5965e173N34179777.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t5590/64/5811657380/234462/5398e856/5965e173N34179777.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,真皮包,viney,白色1,39,0,1
2,100000003145002,华为Meta2,3,9946,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t23998/350/2363990466/222391/a6e9581d/5b7cba5bN0c18fb4f.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t23998/350/2363990466/222391/a6e9581d/5b7cba5bN0c18fb4f.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,拉拉裤,巴布豆,白色2,54,0,1
3,100000003145003,华为Meta3,78903,9993,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,拉杆箱,莎米特,白色3,7,0,1
4,100000003145004,华为Meta4,26604,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/25363/12/2929/274060/5c21df3aE1789bda7/030af31afd116ae0.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,拉杆箱,莎米特,白色4,0,0,1
5,100000003145005,华为Meta5,40805,9996,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t22642/312/2563982615/103706/1398b13d/5b865bb3N0409f0d0.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t22642/312/2563982615/103706/1398b13d/5b865bb3N0409f0d0.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,手机,华为,白色5,4,0,1
6,100000003145006,华为Meta6,89206,9930,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t22642/312/2563982615/103706/1398b13d/5b865bb3N0409f0d0.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t22642/312/2563982615/103706/1398b13d/5b865bb3N0409f0d0.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,手机,华为,白色6,70,0,1
7,100000003145007,华为Meta7,11707,4,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t20707/78/2349564629/130172/50a245d8/5b8e00e2Nf0bcd624.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t20707/78/2349564629/130172/50a245d8/5b8e00e2Nf0bcd624.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,手机,vivo,白色7,1,0,1
8,100000003145008,华为Meta8,99808,9994,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,夕阳红,白色8,6,0,1
9,100000003145009,华为Meta9,41709,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,夕阳红,白色9,0,0,1
10,1000000031450010,华为Meta10,23710,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,夕阳红,白色10,0,0,1
11,1000000031450011,华为Meta11,86111,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t26281/196/340140952/94362/c353173/5b8f6e63N56334c08.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,夕阳红,白色11,0,0,1
12,1000000031450012,华为Meta12,11312,9999,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,ARNO FOCUS ON YOUR EYES,白色12,1,0,1
13,1000000031450013,华为Meta13,99013,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,ARNO FOCUS ON YOUR EYES,白色13,0,0,1
14,1000000031450014,华为Meta14,75314,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/4364/38/240/219619/5b909979E13d29469/7c908af4d61cf659.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t1/4364/38/240/219619/5b909979E13d29469/7c908af4d61cf659.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,ARNO FOCUS ON YOUR EYES,白色14,0,0,1
15,1000000031450015,华为Meta15,69915,10000,100,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,https://m.360buyimg.com/mobilecms/s720x720_jfs/t25867/102/1274759920/233086/96f7fc46/5b909851N02396afd.jpg!q70.jpg.webp,10,2019-05-01,2019-05-01,老花镜,ARNO FOCUS ON YOUR EYES,白色15,0,0,1
2.表结构(提前把表创建好)
CREATE TABLE `tb_sku` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
`sn` varchar(100) NOT NULL COMMENT '商品条码',
`name` varchar(200) NOT NULL COMMENT 'SKU名称',
`price` int(20) NOT NULL COMMENT '价格(分)',
`num` int(10) NOT NULL COMMENT '库存数量',
`alert_num` int(11) DEFAULT NULL COMMENT '库存预警数量',
`image` varchar(200) DEFAULT NULL COMMENT '商品图片',
`images` varchar(2000) DEFAULT NULL COMMENT '商品图片列表',
`weight` int(11) DEFAULT NULL COMMENT '重量(克)',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`category_name` varchar(200) DEFAULT NULL COMMENT '类目名称',
`brand_name` varchar(100) DEFAULT NULL COMMENT '品牌名称',
`spec` varchar(200) DEFAULT NULL COMMENT '规格',
`sale_num` int(11) DEFAULT '0' COMMENT '销量',
`comment_num` int(11) DEFAULT '0' COMMENT '评论数',
`status` char(1) DEFAULT '1' COMMENT '商品状态 1-正常,2-下架,3-删除',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
3.导入命令
load data local infile 'D:/mysqlData/MySQL Server 8.0/Data/tb_sku1.sql' into table `tb_sku` fields terminated by ',' lines terminated by '\n';
200w条测试数据(文件大小700M),40秒左右导入完成。
如果执行load报错,可能mysql读取file的权限没放开,可参考博文
https://blog.csdn.net/weixin_53046747/article/details/125550518
https://blog.csdn.net/m0_47944051/article/details/106402022