前言
现网现在流行一个技术为将mysql数据库中表格导出给excel去用,那么如何实现呢?
在数据库中导出:
mysql> select id,name from ttt.aa into outfile '/tmp/name.xls';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv opon so it cannot execute this statement
#错误,导出失败
问题解决
secure-file-priv特性
secure-file-priv参数是用来限制LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE()传到哪个指定目录的。
ure_file_priv的值为null ,表示限制mysqld 不允许导入|导出
当secure_file_priv的值为/tmp/ ,表示限制mysqld 的导入|导出只能发生在/tmp/目录下
当secure_file_priv的值没有具体值时,表示不对mysqld 的导入|导出做限制
如何查看secure-file-priv参数的值:
mysql> show global variables like '%secure_file_priv%'; # 值为null时,不允许操作,所以应该想要将值改为空
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | NULL |
+------------------+-------+
1 row in set (0.01 sec)
mysql> set global secure_file_priv=''; # 想要通过此方式将secure_file_priv设置没有值
ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable
[root@server ~]# vi /etc/my.cnf # 进入主配置文件进行设置
[mysqld]
……
secure_file_priv='' # 将值设为空
[root@server ~]# systemctl restart mysqld.service # 重启服务
[root@server ~]# mysql -uroot -p123456 # 进入mysql数据库
mysql> show global variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | |
+------------------+-------+
1 row in set (0.00 sec)
mysql> select id,name from ttt.aa into outfile '/tmp/name.xls';
Query OK, 2 rows affected (0.02 sec)
[root@server ~]# cat /tmp/name.xls
1 lisi
2 zhansan
windows
修改my.ini 在[mysqld]内加入secure_file_priv =""
linux
修改my.cnf 在[mysqld]内加入secure_file_priv =""
在外面进行数据的导出:
[root@server ~]# mysql -uroot -p123456 -e "select id,name from ttt.aa" > /opt/name1.xls
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@server ~]# cat /opt/name1.xls
id name
1 lisi
2 zhansan