MySQL将数据导出为Excel

第一步

将数据库中的数据查询并保存到use.xls

select * from ad_works into outfile "/var/lib/mysql-files/use.xls";

在这里插入图片描述
若提示ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
首先使用命令

 show global variables like '%secure_file_priv%';

查看secure_file_priv 对应的值
在这里插入图片描述

mysql>  show global variables like '%secure_file_priv%';
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.24 sec)

  • secure_file_priv 为 NULL 时,表示限制mysqld不允许导入或导出。
  • secure_file_priv 为 /tmp 时,表示限制mysqld只能在/tmp目录中执行导入导出,其他目录不能执行。
  • secure_file_priv 没有值时,表示不限制mysqld在任意目录的导入导出。
    这里我的MySQL输出目录在/var/lib/mysql-files/中,将Excel存入该文件夹即可。

第二步

因为MySQL导出的文件格式一般默认为utf-8形式,而Excel默认打开格式为gbk,所以要将文件格式转换为gbk格式件。

iconv -c -f utf8 -tgb2312 -o use1.xls use.xls

其中use.xls 是转换格式前的文件,use1.xls是转换格式后的Excel文件,该文件可正常打开,不会出现乱码问题

猜你喜欢

转载自blog.csdn.net/weixin_46353366/article/details/114527815