导出Delimited-Text格式文件

MySQL有两种方式导出表的数据到Delimited-Text格式文件中,本文简介这两种方式,并解决测试过程中遇到的报错
第一种是使用 mysqldump命令的--tab选项
第二种是使用 SELECT ... INTO OUTFILE

1. mysqldump命令

之前已经知道,使用mysqldump备份表的话,输出文件中包含了表的定义和insert语句。例如,下面的语句会将数据库test1下的表t1的定义和数据以CREATE语句和
root@db2a:~# mysqldump -pqingsong test1 t1 > test.t1.sql
root@db2a:~# cat test.t1.sql | grep -iv -e '^*' -e '^--' -e '^/' -e '^$'
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `t1` WRITE;
INSERT INTO `t1` VALUES (10,'aaa'),(13,'ddd'),(300,'mesfromdb2a');
UNLOCK TABLES;

mysqldump还可以导出表的内容为 Delimited-Text文件,需要加上--tab选项,加上之后它会在指定目录生成两个文件tablename.sql和tablename.txt。
以下操作目的是将数据输出到/tmp/t1tab/目录下:
root@db2a:~# mkdir /tmp/t1tab/
root@db2a:~# chmod 777 /tmp/t1tab/
root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/tmp/t1tab/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'

遇到报错,现在看一下这个“--secure-file-priv” option的官方说明,它的目地是为了设置secure_file_priv变量:

This option sets the secure_file_priv system variable, which is used to limit the effect of data import and export operations, such as those performed by the LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE() function. For more information, see the description of secure_file_priv.

secure_file_priv变量当前值:
mysql> show variables like 'secure_file_priv';
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.00 sec)

再来看secure_file_priv说明:
This variable is used to limit the effect of data import and export operations, such as those performed by the LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE() function. These operations are permitted only to users who have the FILE privilege.

secure_file_priv may be set as follows:

If empty, the variable has no effect. This is not a secure setting.

If set to the name of a directory, the server limits import and export operations to work only with files in that directory. The directory must exist; the server will not create it.

If set to NULL, the server disables import and export operations. This value is permitted as of MySQL 5.7.6.

也就是说,1. 如果是空的话,对import和export没有限制 2. 如果设置为一个目录,那么import和export操作的文件必须在指定的目录里。 3. 如果为NULL的话,表示不允许有import和export操作

也就是说,至少有三种解决方案:
方案A,指定--tab选项为默认值
root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/var/lib/mysql-files/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
root@db2a:~# ll /var/lib/mysql-files/
total 16
drwxrwx---  2 mysql mysql 4096 Aug  3 05:49 ./
drwxr-xr-x 45 root  root  4096 Jul 12 18:34 ../
-rw-r--r--  1 root  root  1321 Aug  3 05:49 t1.sql
-rw-rw-rw-  1 mysql mysql   30 Aug  3 05:49 t1.txt
root@db2a:~# cat /var/lib/mysql-files/t1.sql | grep -iv -e '^*' -e '^--' -e '^/' -e '^$'
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
root@db2a:~# cat /var/lib/mysql-files/t1.txt
10      aaa
13      ddd
300     mesfromdb2a

请注意一点,t1.sql和t1.txt的owner是不一样的,前者是root,后者是mysql

方案B: 将变量secure_file_priv变为空值,这样在所的目录中都能进行操作了。
由于这个变量是只读的,所以只能在启动mysql的时候指定为空
root@db2a:~# mysqladmin -pqingsong shutdown
root@db2a:~# mysqld --user=mysql --server_id=1 --secure-file-priv='' &

mysql> show variables like 'secure_file_priv';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv |       |
+------------------+-------+
1 row in set (0.00 sec)

root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/tmp/t1tab/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
root@db2a:~# ll /tmp/t1tab/
total 16
drwxrwxrwx 2 root  root  4096 Aug  3 05:50 ./
drwxrwxrwt 7 root  root  4096 Aug  3 05:50 ../
-rw-r--r-- 1 root  root  1321 Aug  3 05:50 t1.sql
-rw-rw-rw- 1 mysql mysql   30 Aug  3 05:50 t1.txt
root@db2a:~# mkdir /tmp/another/
root@db2a:~# chmod 777 /tmp/another/
root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/tmp/another/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
root@db2a:~# ll /tmp/another/
total 16
drwxrwxrwx 2 root  root  4096 Aug  3 05:51 ./
drwxrwxrwt 8 root  root  4096 Aug  3 05:51 ../
-rw-r--r-- 1 root  root  1321 Aug  3 05:51 t1.sql
-rw-rw-rw- 1 mysql mysql   30 Aug  3 05:51 t1.txt
可以看到,输出到/tmp/t1tab/和/tmp/another/都没有问题

方案C: 将变量secure_file_priv变为/tmp/t1tab/,这样只能输出到这个目录中。
root@db2a:~# mysqladmin -pqingsong shutdown
root@db2a:~# mysqld --user=mysql --server_id=1 --secure-file-priv='/tmp/t1tab/' &
root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/tmp/another/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'
root@db2a:~# mysqldump -pqingsong test1 t1 --tab='/tmp/t1tab/'
mysqldump: [Warning] Using a password on the command line interface can be insecure.

可以看到,只能输出到/tmp/t1tab/目录,不能输出到/tmp/another/下。

2. SELECT ... INTO OUTFILE

示例如下:
mysql> select * from t1 into outfile '/var/lib/mysql-files/selectT1.out' ;
Query OK, 3 rows affected (0.06 sec)

每日MySQL之010:导出Delimited-Text格式文件相关推荐

  1. python亿级mysql数据库导出_Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法...

    本文实例讲述了python实现将MySQL数据库表中的数据导出生成csv格式文件的方法.分享给大家供大家参考,具体如下: #!/usr/bin/env python # -*- coding:utf- ...

  2. java导出excel 序号_java web将数据导出为Excel格式文件代码片段

    本文实例为大家分享了java web将数据导出为Excel格式文件的具体代码,供大家参考,具体内容如下 1.jsp代码 2.js代码 function getVerExcel() { window.l ...

  3. 用Allegro导出DXF/DWG格式文件

    用Allegro导出DXF/DWG格式文件 DWG是一种通用格式文件,是AUTOCAD创立的图纸保存格式.DXF(Drawing Exchange File)是一种图形交换文件,用于在AUTOCAD及 ...

  4. NX软件:机器人焊点坐标数据导出为CSV格式文件操作方法

    目录 概述 创建点模型 生成焊点模型 导出焊点数据为CSV文件 焊点数据CSV文件查看 本文已经首发在个人微信公众号:工业机器人仿真与编程(微信号:IndRobSim),欢迎关注! 概述 在使用机器人 ...

  5. 如何使用 MySQL 的 IDE 导出导入数据表文件(以 Navicat for MySQL 导出导入 Excel 文件为例)

    系列文章目录 关于更多 MySQL 数据库以及数据库 IDE 的问题大家可以移步本人专栏--MySQL 数据库. 文章目录 系列文章目录 前言 一.使用 Navicat 导出数据表 1.1.使用&qu ...

  6. DBGridEh导出Excel等格式文件

    uses DBGridEhImpExp; {------------------------------------------------------------------------------ ...

  7. neauscan自带软件scan导出的.avg格式文件如何在matlab里面画图

    新手在处理脑电的时候不可避免的会使用scan这样的商业软件,然后处理完数据以后可能会想导入到matlab进行画图,可是商量软件导出的文件格式可能并不那么自由,这里提供一个将scan处理完的脑电数据导出 ...

  8. 如何将mysql表结构导出成Excel格式的(并带备注)

    方法一: 1.使用一个MySQL管理工具:SQLyog,点击菜单栏"数据库"下拉的最后一项: 导出的格式如下: 2.要想转成Excel格式的只需手动将该表复制到Excel中去. 方 ...

  9. Mysql数据库导入导出数据,将文件内容按格式导入数据库

    实验环境:Linux 安装软件: Mysql 1.导入数据库 1)准备一个权限为mysql的目录 mkdir /myload chonw mysql:mysql /myload 2)修改配置文件 vi ...

最新文章

  1. php path当局者迷,当局者迷_成语故事_有品有墨_品故事 写人生
  2. python tqdm进度条打印
  3. treeset java_Java TreeSet clear()方法与示例
  4. python汉字拼音查询_python获取一组汉字拼音首字母的方法
  5. Jenkins:项目配置
  6. 26章 OOP:宏伟蓝图
  7. 第七届蓝桥杯省赛--方格填数--DFS或全排列
  8. JavaScript高级程序设计(第3版)中文在线阅读,也可以免费下载~
  9. 嵌入式系统应用开发—FPGA开发板—一位全加器仿真测试
  10. 用Python做一个游戏辅助脚本,完整编程思路分享!
  11. 工业电气自动化及电工电子技能考核实训平台(高级版)
  12. Typora(就是个浏览器)自定义设置。附带:Typora免费版链接
  13. 32位操作系统电脑上的打印机如何共享给64位操作系统的电脑想要使用_hudingyin_新浪博客
  14. 自学转行3年经验,终入职阿里!
  15. Hadoop-提高性能(调优)方法
  16. Elasticsearch数据读写过程
  17. 车用计算机内部结构图,汽车电脑的基本结构和工作原理
  18. 德语翻译公司的前景及翻译公司推荐
  19. 数据库cpu飙升,使用pt-kill工具记录慢sql,并杀死sql保障数据库不挂掉
  20. 铁路轨道交通智慧管理系统

热门文章

  1. 【.Net平台下插件开发】-MEF与MAF初步调研
  2. K8s入门-K8s节点notReady状态解决
  3. AI 仿人类人工智能(超级智能)的本质
  4. 【三维CAD设计经验分享】CrownCAD设计:生成工程图
  5. 旷世科技面试题-三个均匀分布x>y>z的概率
  6. minio 上传文件失败报错信息: The difference between the request time and the server‘s time is too large.
  7. spring boot项目:用户中心的商品信息编辑功能
  8. linux打开终端的快捷键放大,linux打开终端的快捷键是什么?
  9. 数据仓库建设---数据建模
  10. 瑞幸咖啡 Luckin Coffee