mysql无法导出表内容

在mysql中可以使用 select[列名] from table [where 语句] into outfile '目标文件' [option] 将一张表的内容导出到外部文件中。但是

 select * from pmx.score into outfile '~/score.sql';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

执行时却报错了。。。好了,问题抛出来,下面开始庖丁解牛。

1.mysql服务可以在没有配置文件的情况下运行,此时系统变量为默认值

使用 "show variables like 变量名" 查看具体变量值

2.MAMP中的mysql是没有配置文件的,全局搜索不到my.cnf

3.

mysqld --verbose --help|grep -A 1 'Default options'
2018-08-03 16:12:17 0 [Warning] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path.
2018-08-03 16:12:17 0 [Note] mysqld (mysqld 5.6.38) starting as process 18398 ...
2018-08-03 16:12:17 18398 [Warning] Setting lower_case_table_names=2 because file system for /Applications/MAMP/db/mysql56/ is case insensitive
2018-08-03 16:12:17 18398 [Note] Plugin 'FEDERATED' is disabled.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /Applications/MAMP/conf/my.cnf ~/.my.cnf 
2018-08-03 16:12:17 18398 [Note] Binlog end
2018-08-03 16:12:17 18398 [Note] Shutting down plugin 'MyISAM'
2018-08-03 16:12:17 18398 [Note] Shutting down plugin 'CSV'

注意列出的my.cnf路径,mysqld会按照如上顺序寻找my.cnf。如果四个路径都没有找到,则使用默认值来初始化系统变量

4.根据第二条mamp的mysql没有配置文件,为了自定义系统变量我们可以手动创建一个my.cnf文件,放到任意一个上面的路径下。我选择放到MAMP/conf文件夹下

5.对于不熟悉配置文件的同学来说,为了避免书写有误,最好是能拷贝一份现成的配置文件。MAMP提供了Library/support-files/my-default.cnf文件。可以将该文件拷贝到conf文件夹下,必须重命名为my.cnf

6.

show variables like 'secure_file_priv';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv | NULL  |
+------------------+-------+

 系统变量"secure_file_priv"指定了mysqld能否导出表

  当secure_file_priv值为null,表示禁止导入导出

  当secure_file_priv值为某个文件夹,表示限制导入导出仅能发生在特定文件夹下

  当secure_file_priv值为空,表示随便导,爱咋咋地

7.修改my.cnf内容,在mysqld一栏下,添加内容

8.保存my.cnf,然后必须重启mysqld,使配置生效

原文地址:https://www.cnblogs.com/bibiafa/p/9414698.html