我试图在脚本中使用以下代码将MySQL脚本的结果写入文本文件。
SELECT p.title, p.content, c.name FROM post p LEFT JOIN category c ON p.category_id=c.id INTO OUTFILE 'D:\MySql\mysqlTest.txt';
但是,我得到以下
错误1290(HY000):MySQL服务器正在使用–secure-file-priv选项运行,因此它无法执行此语句
我该如何解决?
Ubuntu 16.04(EASY):确定允许您在哪里写
mysql> SELECT @@GLOBAL.secure_file_priv; +---------------------------+ | @@GLOBAL.secure_file_priv | +---------------------------+ | /var/lib/mysql-files/ | +---------------------------+ 1 row in set (0.00 sec)
然后在那写
mysql> SELECT * FROM train INTO OUTFILE '/var/lib/mysql-files/test.csv' FIELDS TERMINATED BY ','; Query OK, 992931 rows affected (1.65 sec) mysql>
Mac OSX:通过MAMP安装Mysql
找出你可以写的地方
mysql> SELECT @@GLOBAL.secure_file_priv; +---------------------------+ | @@GLOBAL.secure_file_priv | +---------------------------+ | NULL | +---------------------------+ 1 row in set (0.00 sec)
NULL表示您已不知所措,因此必须创建文件“〜/ .my.cnf”
为通过MAMP(在Mac上)安装的MySQL启用读/写:
$ vi〜/ .my.cnf
[mysqld_safe] [mysqld] secure_file_priv="/Users/russian_spy/"
现在检查是否有效:
一个。启动mysql(默认的MAMP用户是root,密码也是root)
$ /Applications/MAMP/Library/bin/mysql -u root -p
b。在mysql中查看列入白名单的路径
mysql> SELECT @@GLOBAL.secure_file_priv; +---------------------------+ | @@GLOBAL.secure_file_priv | +---------------------------+ | /Users/russian_spy/ | +---------------------------+ 1 row in set (0.00 sec)
C。最后,通过将表导出train到CSV文件进行测试
train
mysql> SELECT * FROM train INTO OUTFILE '/Users/russian_spy/test.csv' FIELDS TERMINATED BY ','; Query OK, 992931 rows affected (1.65 sec) mysql>