首页 > 代码库 > linux下mysql 文件导入导出

linux下mysql 文件导入导出

最近在做mysql的数据导入导出得到的一些经验,记录下。1.首先要开通导入导出的功能,需要设置一个mysql的配置可以在 my.conf 文件的最后增加配置项 secure-file-priv=‘‘用这个命令查到当前的设置 SHOW VARIABLES LIKE ‘%secure_file_priv%‘;如果为 NULL 表示禁止导入导出,如果指定了具体路径,则导入导出必须在这个路径下,如果为空字符串,则可以导出到任意路径(必须要有足够权限写任意的路径权限)如果权限不够则可以默认到mysql的数据文件目录;没有设置的错误信息,错误代码: 1290The MySQL server is running with the --secure-file-priv option so it cannot execute this statement2.权限问题,在windows下权限问题好解决。在linux下 mysql的用户权限有限,所以我只能默认导出的mysql的数据文件下。-- 如果指定路径则需要当前用户具有很高的权限才行,默认的话基本可以导出-- 默认路径为mysql的数据文件存放路径 可以用这个命令查看文件放哪个路径SHOW VARIABLES LIKE ‘%datadir%‘SELECT * INTO OUTFILE ‘2.txt‘ FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘\"‘ LINES TERMINATED BY ‘\n‘ FROM mtdwgl_syn.irms_trans_pipehole; LOAD DATA INFILE ‘/data/mysql/2.txt‘ INTO TABLE mtdwgl_res.tb_irms_trans_pipehole CHARACTER SET utf8 FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘\"‘ LINES TERMINATED BY ‘\n‘;3.经过测试2300W条记录导出花了1分46秒SELECT * INTO OUTFILE ‘2.txt‘ FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘\"‘ LINES TERMINATED BY ‘\n‘ FROM mtdwgl_syn.irms_trans_pipehole; Query OK, 23040110 rows affected (1 min 46.01 sec)导入花了 3分22秒LOAD DATA INFILE ‘/data/mysql/2.txt‘ INTO TABLE mtdwgl_res.tb_irms_trans_pipehole CHARACTER SET utf8 FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘\"‘ LINES TERMINATED BY ‘\n‘; Query OK, 23040110 rows affected (3 min 22.10 sec)Records: 23040110 Deleted: 0 Skipped: 0 Warnings: 0

文件大小,测试的是2.txt文件,4.8G

sudo du -h /data/mysql/*.txt
480M /data/mysql/1.txt
4.8G /data/mysql/2.txt

  

linux下mysql 文件导入导出