实验环境
Ubuntu16.4;
MySQL5.7
问题描述
准备将数据库中某张表导出来,报如下错误:
mysql> SELECT * FROM runoob_tbl INTO OUTFILE '/tmp/runoob.txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
问题分析
我们可以在命令行查看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.01 sec)
可以看到secure_file_priv
默认值为/var/lib/mysql-files/
,或许这就是导出不成功的原因;
问题解决
找到mysql
的配置文件。查找资料,说是windows
系统下是my.ini
,·linux
下应该在my.ini
文件中。但是笔者实际修改的是/etc/mysql/mysql.conf.d/mysqld.cnf
文件。打开该文件后在最后一行添加secure_file_priv='/'
,如下所示:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
..............
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
secure_file_priv='/'
其实看到该文档第一句话,这便是mysql服务器的配置文件咯!
下来我们执行刚才的导出语句
mysql> select * from runoob_tbl into outfile '/tmp/runoob1.txt';
Query OK, 4 rows affected (0.00 sec)
此时已经成功导出数据。
**注意注意:**笔者这里的导出目录是/tmp
目录下。其实也试过在其他目录下导出,但是会报一些错误,如Can't create/write to file '/home/hadoop/runoob1.txt' (Errcode: 13 - Permission denied)
(有好的办法解决该错误了,欢迎留言)。无奈之下,就先导出在`/tmp’目录下了。