在进行mysql文本文件导入导出时遇到报错:
mysql> select * from account into outfile '/Users/cardiac/Documents/DataBase/mysql/ImportData/test.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
使用命令查看参数设置状态:
mysql> show variables like '%secure%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | NULL |
+--------------------------+-------+
3 rows in set (0.01 sec)
因为默认参数secure_file_priv设置是null, 表示不能使用任何目录。因此更改该参数的值。
使用的MAC,安装的mysql 5.7版本,在安装目录中没有my.ini文件,也没有my.cnf文件,所以在目录/etc/下新建配置文件my.cnf,mysql启动时就会读取该配置文件。
[mysqld]
character-set-server=utf8
secure_file_priv=/Users/cardiac/Documents/DataBase/mysql/ImportData/
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启mysql服务,注意是重启mysql server。
再次查看该参数,已经设置有目录
mysql> show variables like '%secure%';
+--------------------------+-----------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /Users/cardiac/Documents/DataBase/mysql/ImportData/ |
+--------------------------+-----------------------------------------------------+
3 rows in set (0.00 sec)
mysql> select * from account into outfile '/Users/cardiac/Documents/DataBase/mysql/ImportData/test.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
依旧没有解决该问题。
再次更改配置文件/etc/my.cnf
sh-3.2# cat /etc/my.cnf
[mysqld]
character-set-server=utf8
secure_file_priv=''
#secure_file_priv=/Users/cardiac/Documents/DataBase/mysql/ImportData/
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
将参数secure_file_priv=''设置为'',再次重启mysql server。
查看参数状态:
mysql> show variables like '%secure%'; +--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | |
+--------------------------+-------+
3 rows in set (0.01 sec)
再次进行文件的导出操作
mysql> select * from account into outfile '/Users/cardiac/Documents/DataBase/mysql/ImportData/test.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
ERROR 1 (HY000): Can't create/write to file '/Users/cardiac/Documents/DataBase/mysql/ImportData/test.csv' (Errcode: 13 - Permission denied)
没有上面参数的报错,这个报错是对目录没有写的操作权限,换个有权限的目录即可
mysql> select * from account into outfile '/Users/cardiac/test/test.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
Query OK, 2 rows affected (0.00 sec)
数据成功导出。
至于设置具体目录后,对目录还是不能操作,还没有发现原因,期待大神指导指导,因为网上都说只要设置目录后,在该目录下就可以进行导入导出操作,所以自己还是折腾了一番,才完成数据的导入导出操作,因此记录一下,免得下次还遇到。