一、故障現象:
Mysql可使用 into outfile 參數把表中數據導出到文件,
例如可用以下命令把 test 表的數據導出到 test.txt
SELECT * FROM newdb.test INTO OUTFILE 'test.txt';
以上命令在mysql5.6下運行沒有問題,但在mysql5.7下運行則出現了以下錯誤:
'ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv
option so it cannot execute this statement'
二、查看故障點:
查看官方文檔,secure_file_priv參數 用於限制LOAD DATA, SELECT …OUTFILE,
LOAD_FILE()傳到哪個指定目錄。
secure_file_priv 為 'NULL' 時,表示限制mysqld不允許導入或導出。
secure_file_priv 為 '/tmp' 時,表示限制mysqld只能在/tmp目錄中執行導入導出;
secure_file_priv '沒有值'時,表示不限制mysqld在任意目錄的導入導出。
三、核實故障點:
查看 secure_file_priv 的值,默認為NULL,表示限制不能導入導出。
mysql> show global variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | NULL |
+------------------+-------+
1 row in set (0.00 sec)
因為 secure_file_priv 參數是隻讀參數,'不能使用set global命令修改'。
mysql> # set global secure_file_priv='';
'ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable'
四、解決方法:
打開my.cnf ,加入以下語句後重啟mysql。
# vim /etc/my.cnf
'secure_file_priv='''
查看secure_file_priv修改後的值
mysql> show global variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | |
+------------------+-------+
1 row in set (0.00 sec)
五、查看成果:
修改後再次執行,成功導出。
mysql> SELECT * FROM newdb.test INTO OUTFILE 'test.txt';
1 queries executed, 1 success, 0 errors, 0 warnings
查詢:select * from newdb.test into outfile 'test.txt'
共 2 行受到影響
執行耗時 : 0.044 sec
傳送時間 : 0 sec
總耗時 : 0.045 sec
注意,導出文件,在 /www/server/data/newdb/test.txt
閱讀更多 你永遠得不到的小老弟 的文章